94 lines
3.6 KiB
Rust
94 lines
3.6 KiB
Rust
#[macro_use]
|
|
extern crate rust_util;
|
|
|
|
use std::{env, fs};
|
|
use std::path::PathBuf;
|
|
use std::process::Command;
|
|
|
|
use rust_util::util_msg;
|
|
use rust_util::util_msg::MessageType;
|
|
|
|
fn main() {
|
|
let home = 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 = env::args().skip(1).collect::<Vec<_>>();
|
|
if args.is_empty() {
|
|
failure_and_exit!(
|
|
"runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [arguments]\n",
|
|
env!("CARGO_PKG_VERSION"),
|
|
);
|
|
}
|
|
let first_argument = match args.get(0) {
|
|
None => failure_and_exit!("Must assign file name"),
|
|
Some(f) => f,
|
|
};
|
|
|
|
if first_argument == "--help" {
|
|
println!(r##"{} v{} - {}
|
|
|
|
Help:
|
|
runrs --help
|
|
|
|
Run Rust Script:
|
|
runrs <script.rs> [arguments]
|
|
"##,
|
|
env!("CARGO_PKG_NAME"),
|
|
env!("CARGO_PKG_VERSION"),
|
|
env!("CARGO_PKG_DESCRIPTION")
|
|
);
|
|
return;
|
|
}
|
|
|
|
let script_file = first_argument;
|
|
let script_content = match 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 = 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 {
|
|
if let Ok(Some(canonicalized_script_file)) = PathBuf::from(script_file)
|
|
/* - */.canonicalize().map(|f| f.to_str().map(|f| f.to_string())) {
|
|
let cache_script_bin_name_src = format!("{}.src", cache_script_bin_name);
|
|
if let Ok(_) = fs::write(&cache_script_bin_name_src, &format!("{}\n", canonicalized_script_file)) {
|
|
debugging!("Add {} to {}", canonicalized_script_file, cache_script_bin_name_src);
|
|
}
|
|
}
|
|
|
|
let mut cmd = Command::new(rust_script);
|
|
if util_msg::is_logger_level_enabled(MessageType::DEBUG) {
|
|
cmd.arg("--cargo-output");
|
|
}
|
|
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)),
|
|
}
|
|
}
|