105 lines
3.5 KiB
Plaintext
105 lines
3.5 KiB
Plaintext
use std::process;
|
|
use std::env;
|
|
use std::fs;
|
|
use mist_api::builder;
|
|
use mist_api::transpiler;
|
|
|
|
fn main() {
|
|
let args Vec<String> = env::args().collect();
|
|
|
|
if (args.len() < 2) {
|
|
print_usage();
|
|
process::exit(1);
|
|
}
|
|
|
|
match (args[1].as_str()) {
|
|
"run" | "build" | "check" | "r" | "b" | "c" => {
|
|
let root = transpiler::transpile();
|
|
|
|
println!("");
|
|
|
|
if (builder::build(args, root)) {
|
|
println!("\x1b[32m\nBuild successful\x1b[0m");
|
|
} else {
|
|
println!("\x1b[31m\nBuild failed\x1b[0m");
|
|
}
|
|
}
|
|
"transpile" | "t" => {
|
|
transpiler::transpile();
|
|
}
|
|
"init" => {
|
|
init_project(args);
|
|
}
|
|
"version" | "--version" | "-v" => {
|
|
println!("mist {}", env!("CARGO_PKG_VERSION"));
|
|
}
|
|
"help" | "--help" | "-h" => {
|
|
print_usage();
|
|
}
|
|
unknown => {
|
|
eprintln!("error: unknown command '{}'\n", unknown);
|
|
print_usage();
|
|
process::exit(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn print_usage() {
|
|
println!("Mist - the mist compiler\n");
|
|
println!(" View our documentation! \x1b[35mhttps://mist.selimaj.dev\x1b[0m");
|
|
println!(" Follow us on github! \x1b[35mhttps://github.com/mist-go\x1b[0m\n");
|
|
println!("usage:");
|
|
println!(" \x1b[36m mist run\x1b[0m,\x1b[36m r\x1b[0m run the project in the current directory\n");
|
|
println!(
|
|
" \x1b[36m mist build\x1b[0m,\x1b[36m b\x1b[0m build the project in the current directory\n"
|
|
);
|
|
println!(
|
|
" \x1b[36m mist transpile\x1b[0m,\x1b[36m t\x1b[0m transpile the project in the current directory\n"
|
|
);
|
|
println!(
|
|
" \x1b[36m mist check\x1b[0m,\x1b[36m c\x1b[0m check the project in the current directory\n"
|
|
);
|
|
println!(
|
|
" \x1b[36m mist init\x1b[0m initialize a project in the current folder\n"
|
|
);
|
|
println!(" \x1b[36m mist version \x1b[0m print the compiler version\n");
|
|
println!(" \x1b[36m mist help\x1b[0m print this message\n");
|
|
}
|
|
|
|
fn init_project(mut args Vec<String>) {
|
|
args.remove(0);
|
|
|
|
process::Command::new("cargo")
|
|
.args(args)
|
|
.stdout(process::Stdio::inherit())
|
|
.stderr(process::Stdio::inherit())
|
|
.stdin(process::Stdio::inherit())
|
|
.spawn()
|
|
.expect("Failed to run cargo").wait().expect("Unable to wait");
|
|
|
|
let is_lib bool = fs::metadata("src/lib.rs").is_ok();
|
|
|
|
if (is_lib) {
|
|
fs::remove_file("src/lib.rs").expect("Failed to remove src/lib.rs");
|
|
fs::write("src/lib.mist", "fn test() {\n}").expect("Failed to create src/lib.mist");
|
|
} else {
|
|
fs::remove_file("src/main.rs").expect("Failed to remove src/main.rs");
|
|
fs::write("src/main.mist", "fn main() {\n println!(\"Hello, World!\");\n}\n").expect("Failed to create src/main.mist");
|
|
}
|
|
|
|
inject_mist_target(is_lib);
|
|
|
|
println!("\n\x1b[34minfo\x1b[0m: Please open Cargo.toml and adjust `\x1b[35mname\x1b[0m = ` as needed, Thank you for using Mist!\n");
|
|
}
|
|
|
|
fn inject_mist_target(is_lib bool) {
|
|
let toml_content String = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml");
|
|
|
|
let inject *str = if (is_lib) {
|
|
"\n[lib]\nname = \"my_lib\"\npath = \".mist/src/lib.rs\"\n"
|
|
} else {
|
|
"\n[[bin]]\nname = \"my_bin\"\npath = \".mist/src/main.rs\"\n"
|
|
};
|
|
|
|
fs::write("Cargo.toml", toml_content+inject).expect("Failed to inject to Cargo.toml");
|
|
} |