94 lines
2.7 KiB
Plaintext
94 lines
2.7 KiB
Plaintext
pub module modules;
|
|
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
use std::fs;
|
|
use std::process;
|
|
|
|
use crate::transpiler::MistConfig;
|
|
|
|
#[derive(Debug)]
|
|
pub class Module {
|
|
pub name String;
|
|
pub path PathBuf;
|
|
pub children Vec<Module>;
|
|
|
|
pub constructor(mist_path PathBuf, config *MistConfig) {
|
|
self.children = Vec::new();
|
|
self.name = mist_path.file_stem()
|
|
.and_then(fn(s) -> s.to_str())
|
|
.expect("Failed to get module name")
|
|
.to_string();
|
|
self.path = mist_path;
|
|
|
|
if self.path.is_dir() {
|
|
let dir = self.path.clone();
|
|
|
|
let pkg_gate = self.path.join("package.mist");
|
|
if pkg_gate.exists() {
|
|
self.path = pkg_gate;
|
|
}
|
|
|
|
self.scan_directory(&dir, config);
|
|
} else if config.should_ignore(&self.path) {
|
|
let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf();
|
|
|
|
self.scan_directory(&dir, config);
|
|
}
|
|
}
|
|
|
|
fn scan_directory(*mut self, search_dir *Path, config *MistConfig) {
|
|
let 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 in entries {
|
|
let entry = match entry {
|
|
Ok[entry] => entry;
|
|
Err[e] => {
|
|
eprintln!("error: failed to read directory entry\n {}", e);
|
|
process::exit(1);
|
|
}
|
|
};
|
|
|
|
let child_path = entry.path();
|
|
|
|
if child_path == self.path || child_path == config.package {
|
|
continue;
|
|
}
|
|
|
|
self.children.push(Module::new(child_path, config));
|
|
}
|
|
}
|
|
|
|
pub fn is_package(*self) bool {
|
|
self.path.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() == Some("package.mist")
|
|
}
|
|
|
|
pub fn output_dir(*self, parent_dir *PathBuf) PathBuf {
|
|
if self.path.is_dir() || self.is_package() {
|
|
parent_dir.join(&self.name)
|
|
} else {
|
|
parent_dir.clone()
|
|
}
|
|
}
|
|
|
|
pub fn output_path(*self, parent_dir *PathBuf, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) PathBuf {
|
|
let name = if let Some[[_, name]] = curr_mod_decl {
|
|
&name.0
|
|
} else {
|
|
&self.name
|
|
};
|
|
|
|
if self.path.is_dir() || self.is_package() {
|
|
parent_dir.join(name).join("mod.rs")
|
|
} else {
|
|
parent_dir.join(name).with_extension("rs")
|
|
}
|
|
}
|
|
}
|