64 lines
2.5 KiB
Rust
64 lines
2.5 KiB
Rust
#[macro_use]
|
|
extern crate rust_util;
|
|
|
|
use std::path::PathBuf;
|
|
use std::process::Command;
|
|
|
|
fn main() {
|
|
let home = std::env::var("HOME").unwrap_or_else(|_|
|
|
failure_and_exit!("$HOME not found!")
|
|
);
|
|
let rust_script = PathBuf::from(format!("{}/.cargo/bin/rust-script", home));
|
|
if !rust_script.exists() {
|
|
// warning!("rust-script not found, install it...");
|
|
// let mut cargo_install_rust_script = Command::new("cargo");
|
|
// cargo_install_rust_script.args(&["install", "rust-script"]);
|
|
// debugging!("Run command: {:?}", cargo_install_rust_script);
|
|
// let run_result = rust_util::util_cmd::run_command_and_wait(&mut cargo_install_rust_script);
|
|
// if let Err(e) = run_result {
|
|
// failure_and_exit!("Install rust-script failed: {}", e);
|
|
// }
|
|
failure_and_exit!("Need install rust-script tool, https://git.hatter.ink/hatter/runrs/src/branch/master/external/rust-script");
|
|
}
|
|
|
|
let args = std::env::args().skip(1).collect::<Vec<_>>();
|
|
|
|
if args.is_empty() {
|
|
failure_and_exit!("runrs need arguments, e.g.\n\nrunrs <scirpt.ers> [arguments]\n");
|
|
}
|
|
|
|
let script_file = match args.get(0) {
|
|
None => failure_and_exit!("Must assign file name"),
|
|
Some(f) => f,
|
|
};
|
|
let script_content = match std::fs::read_to_string(script_file) {
|
|
Err(e) => failure_and_exit!("Read file: {}, failed: {}", script_file, e),
|
|
Ok(c) => c,
|
|
};
|
|
let script_sha256 = sha256::digest(script_content);
|
|
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
|
|
|
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}",
|
|
home,
|
|
script_sha256);
|
|
|
|
let cache_script_bin_name_exists = std::fs::metadata(&cache_script_bin_name).is_ok();
|
|
debugging!("Bin name: {} {} exists", cache_script_bin_name, iff!(cache_script_bin_name_exists, "", "not"));
|
|
|
|
let mut run_script_cmd = if cache_script_bin_name_exists {
|
|
Command::new(cache_script_bin_name)
|
|
} else {
|
|
let mut cmd = Command::new(rust_script);
|
|
cmd.args(&["--bin-name", &script_sha256, script_file]);
|
|
cmd
|
|
};
|
|
for arg in args.iter().skip(1) {
|
|
run_script_cmd.arg(arg);
|
|
}
|
|
debugging!("Run command: {:?}", run_script_cmd);
|
|
match rust_util::util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
|
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
|
|
Ok(exit_status) => std::process::exit(exit_status.code().unwrap_or_else(|| 0)),
|
|
}
|
|
}
|