Files
mist/crates/mist-api/src/modules.mist
T
2026-06-03 16:00:23 +02:00

101 lines
2.7 KiB
Plaintext

use <std::path::Path>;
use <std::path::PathBuf>;
use <std::fs>;
use <std::process>;
#[derive(Debug)]
pub class Module {
pub String name;
pub PathBuf path;
pub Vec<Self> children;
pub constructor(PathBuf mist_path) {
self.children = new Vec();
self.name = mist_path.file_stem()
.and_then(|var s| s.to_str())
.expect("Failed to get module name")
.to_string();
self.path = mist_path;
if (self.path.is_dir()) {
PathBuf dir = self.path.clone();
PathBuf pkg_gate = self.path.join("package.mist");
if (pkg_gate.exists()) {
self.path = pkg_gate;
}
self.scan_directory(&dir);
}
}
void scan_directory(self mut*, Path* search_dir) {
var entries = match (fs::read_dir(search_dir)) {
Ok[entries] => entries;
Err[e] => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
process::exit(1);
}
};
for (entry : entries) {
var entry = match (entry) {
Ok[entry] => entry;
Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1);
}
};
PathBuf child_path = entry.path();
if (child_path == self.path) {
continue;
}
self.children.push(new Module(child_path));
}
}
pub PathBuf output_dir(self*, PathBuf* parent_dir) {
if (self.path.is_dir() || Some("package.mist") == self.path.file_name().unwrap().to_str()) {
parent_dir.join(&self.name)
} else {
parent_dir.clone()
}
}
pub PathBuf output_path(self*, PathBuf* parent_dir) {
if (self.path.is_dir() || Some("package.mist") == self.path.file_name().unwrap().to_str()) {
parent_dir.join(&self.name).join("mod.rs")
} else {
parent_dir.join(&self.name).with_extension("rs")
}
}
}
pub Module master_package(PathBuf mist_path) {
var mut root_mod = new Module(mist_path.clone());
Path* parent_dir = mist_path.parent()
.expect("Failed to get parent directory of root file");
root_mod.scan_directory(parent_dir);
root_mod
}
bool should_skip(Path* source, Path* output) {
var src_time = fs::metadata(source)
.ok()
.and_then(|var v| v.modified().ok());
var out_time = fs::metadata(output)
.ok()
.and_then(|var v| v.modified().ok());
out_time
.map(|var out_time| out_time >= src_time.unwrap())
.unwrap_or_default()
}