154 lines
6.1 KiB
Rust
154 lines
6.1 KiB
Rust
#[macro_use]
|
|
extern crate rust_util;
|
|
|
|
use std::{env, fs, process};
|
|
use std::path::PathBuf;
|
|
use std::process::Command;
|
|
use std::time::SystemTime;
|
|
|
|
use rust_util::{util_cmd, util_msg};
|
|
use rust_util::util_msg::MessageType;
|
|
|
|
fn main() {
|
|
let user_home = get_user_home();
|
|
let rust_script = get_run_script_bin_name(&user_home);
|
|
|
|
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 = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
|
|
if first_argument == "--help" {
|
|
print_help();
|
|
return;
|
|
}
|
|
let script_file = first_argument;
|
|
|
|
let (_, script_sha256) = read_file_and_digest(script_file);
|
|
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
|
|
|
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", user_home, script_sha256);
|
|
let mut run_script_cmd = build_script_command(rust_script, script_file, &script_sha256, &cache_script_bin_name);
|
|
for arg in args.iter().skip(1) {
|
|
run_script_cmd.arg(arg);
|
|
}
|
|
run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd)
|
|
}
|
|
|
|
fn build_script_command(rust_script: PathBuf, script_file: &str, script_sha256: &str, cache_script_bin_name: &str) -> Command {
|
|
let skip_cache = is_env_on("RUNRS_SKIP_CACHE");
|
|
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 "));
|
|
if !skip_cache && cache_script_bin_name_exists {
|
|
Command::new(&cache_script_bin_name)
|
|
} else {
|
|
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
|
|
}
|
|
}
|
|
|
|
fn run_script_command(script_file: &str, cache_script_bin_name: &str, mut run_script_cmd: &mut Command) -> ! {
|
|
debugging!("Run command: {:?}", run_script_cmd);
|
|
let run_command_start = SystemTime::now();
|
|
match util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
|
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
|
|
Ok(exit_status) => {
|
|
write_script_file_to_src(script_file, cache_script_bin_name);
|
|
if let Ok(run_command_cost) = SystemTime::now().duration_since(run_command_start) {
|
|
debugging!("Run command cost: {}ms", run_command_cost.as_millis());
|
|
}
|
|
process::exit(exit_status.code().unwrap_or_else(|| 0))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn write_script_file_to_src(script_file: &str, cache_script_bin_name: &str) {
|
|
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);
|
|
let src_content = fs::read_to_string(&cache_script_bin_name_src).ok();
|
|
if src_content.as_ref().map(|c| c.contains(&canonicalized_script_file)).unwrap_or_else(|| false) {
|
|
return;
|
|
}
|
|
let new_src_content = src_content.unwrap_or_else(|| "".to_string()) + &format!("{}\n", canonicalized_script_file);
|
|
if let Ok(_) = fs::write(&cache_script_bin_name_src, &new_src_content) {
|
|
debugging!("Add {} to {}", canonicalized_script_file, cache_script_bin_name_src);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn print_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")
|
|
);
|
|
}
|
|
|
|
fn read_file_and_digest(script_file: &str) -> (String, String) {
|
|
let default_max_script_len = 1024 * 1024;
|
|
let max_script_len: u64 = env::var("RUNRS_MAX_SCRIPT_LEN")
|
|
.map(|len| len.parse().unwrap_or_else(|| default_max_script_len)).unwrap_or_else(|| default_max_script_len);
|
|
match fs::metadata(script_file) {
|
|
Err(_) => failure_and_exit!("Script file not exists: {}", script_file),
|
|
Ok(metadata) => if metadata.is_dir() {
|
|
failure_and_exit!("Assigned input is dir: {}", script_file);
|
|
} else if metadata.len() > max_script_len {
|
|
failure_and_exit!("Script file: {} too large: {}", script_file, metadata.len());
|
|
}
|
|
}
|
|
let script_content = fs::read_to_string(script_file).unwrap_or_else(|e|
|
|
failure_and_exit!("Read file: {}, failed: {}", script_file, e)
|
|
);
|
|
let script_sha256 = sha256::digest(&script_content);
|
|
|
|
(script_content, script_sha256)
|
|
}
|
|
|
|
fn get_run_script_bin_name(home: &str) -> PathBuf {
|
|
if let Ok(rust_script) = env::var("RUNRS_RUST_SCRIPT") {
|
|
let rust_script_path_buf = PathBuf::from(&rust_script);
|
|
if !rust_script_path_buf.exists() {
|
|
warning!("RUST_SCRIPT={} not exists", &rust_script);
|
|
}
|
|
return rust_script_path_buf;
|
|
}
|
|
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", "--git", "https://git.hatter.ink/hatter/runrs", "rust-script"]);
|
|
debugging!("Run command: {:?}", cargo_install_rust_script);
|
|
match util_cmd::run_command_and_wait(&mut cargo_install_rust_script) {
|
|
Err(e) => failure_and_exit!("Install rust-script failed: {}", e),
|
|
Ok(exist_status) => if !exist_status.success() {
|
|
failure!("Install rust-script not success: {}", exist_status);
|
|
},
|
|
}
|
|
}
|
|
rust_script
|
|
}
|
|
|
|
fn is_env_on(env: &str) -> bool {
|
|
env::var(env).map(|v| {
|
|
let v = v.to_lowercase();
|
|
v == "true" || v == "on" || v == "yes" || v == "1"
|
|
}).unwrap_or_else(|_| false)
|
|
}
|
|
|
|
fn get_user_home() -> String {
|
|
env::var("HOME").unwrap_or_else(|_|
|
|
failure_and_exit!("$HOME not found!")
|
|
)
|
|
} |