Working transpiler
This commit is contained in:
@@ -16,11 +16,12 @@ pub PathBuf build(bool force) {
|
|||||||
PathBuf src = root.join("src");
|
PathBuf src = root.join("src");
|
||||||
PathBuf out = root.join(".mist/src");
|
PathBuf out = root.join(".mist/src");
|
||||||
|
|
||||||
|
// build_dir(&root, &src, &src, &out, force);
|
||||||
build_dir(&root, &src, &src, &out, force);
|
|
||||||
|
|
||||||
var master = modules::master_package(src.join("main.mist"), out.join("main.rs"));
|
var master = modules::master_package(src.join("main.mist"), out.join("main.rs"));
|
||||||
|
|
||||||
|
master.transpile(&out);
|
||||||
|
|
||||||
var elapsed = start.elapsed();
|
var elapsed = start.elapsed();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
@@ -31,70 +32,39 @@ pub PathBuf build(bool force) {
|
|||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
|
||||||
void build_dir(Path* root, Path* base_src, Path* current, Path* out, bool force) {
|
impl modules::Module {
|
||||||
var entries = match (fs::read_dir(current)) {
|
void transpile(self*, PathBuf* parent_dir) {
|
||||||
Ok[entries] => entries;
|
if (self.path.is_dir()) {
|
||||||
Err[e] => {
|
PathBuf dir = parent_dir.join(&self.name);
|
||||||
eprintln!(
|
|
||||||
"error: failed to read directory {}\n {}",
|
|
||||||
current.display(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
|
|
||||||
process::exit(1);
|
for (child : &self.children) {
|
||||||
|
child.transpile(&dir);
|
||||||
}
|
}
|
||||||
};
|
} else {
|
||||||
|
if (Some("package.mist") == self.path.file_name().unwrap().to_str()) {
|
||||||
|
PathBuf dir = parent_dir.join(&self.name);
|
||||||
|
PathBuf output_file = dir.join("mod.rs");
|
||||||
|
|
||||||
for (entry : entries) {
|
transpile_file(&self.path, &output_file);
|
||||||
var entry = match (entry) {
|
|
||||||
Ok[entry] => entry;
|
|
||||||
Err[e] => {
|
|
||||||
eprintln!("error: failed to read directory entry\n {}", e);
|
|
||||||
|
|
||||||
process::exit(1);
|
for (child : &self.children) {
|
||||||
|
child.transpile(&dir);
|
||||||
}
|
}
|
||||||
};
|
} else {
|
||||||
|
PathBuf output_file = parent_dir.join(&self.name).with_extension("rs");
|
||||||
|
|
||||||
PathBuf path = entry.path();
|
transpile_file(&self.path, &output_file);
|
||||||
|
|
||||||
if (path.is_dir()) {
|
for (child : &self.children) {
|
||||||
build_dir(root, base_src, &path, out, force);
|
child.transpile(parent_dir);
|
||||||
continue;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Path* relative = path.strip_prefix(base_src).expect("Failed to strip base src from path");
|
void transpile_file(PathBuf* path, PathBuf* output_file) {
|
||||||
|
String source = match (fs::read_to_string(path)) {
|
||||||
if (path.extension().and_then(|var e| e.to_str()) != Some("mist")) {
|
|
||||||
PathBuf dest = out.join(relative);
|
|
||||||
|
|
||||||
Option<Path*> parent = dest.parent();
|
|
||||||
|
|
||||||
if (parent.is_some()) {
|
|
||||||
fs::create_dir_all(parent.unwrap()).expect("failed to create output directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!force && should_skip(&path, &dest)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::copy(&path, dest).expect("Failed to copy non-mist file");
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
PathBuf output_file = out.join(relative).with_extension("rs");
|
|
||||||
|
|
||||||
if (!force && should_skip(&path, &output_file)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Option<Path*> parent = output_file.parent();
|
|
||||||
|
|
||||||
if (parent.is_some()) {
|
|
||||||
fs::create_dir_all(parent.unwrap()).expect("failed to create output directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
String source = match (fs::read_to_string(&path)) {
|
|
||||||
Ok[s] => s;
|
Ok[s] => s;
|
||||||
Err[e] => {
|
Err[e] => {
|
||||||
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
||||||
@@ -149,7 +119,6 @@ void build_dir(Path* root, Path* base_src, Path* current, Path* out, bool force)
|
|||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool should_skip(Path* source, Path* output) {
|
bool should_skip(Path* source, Path* output) {
|
||||||
var src_time = fs::metadata(source)
|
var src_time = fs::metadata(source)
|
||||||
|
|||||||
Reference in New Issue
Block a user