diff --git a/src/main.rs b/src/main.rs index 2087871..4efa3a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,13 +28,20 @@ fn main() { 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/{}", - home, script_sha256); + let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", 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: &String, script_sha256: &String, cache_script_bin_name: &String) -> Command { let skip_cache = is_env_on("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 ")); - let mut run_script_cmd = if !skip_cache && cache_script_bin_name_exists { + if !skip_cache && cache_script_bin_name_exists { Command::new(&cache_script_bin_name) } else { let mut cmd = Command::new(rust_script); @@ -43,10 +50,10 @@ fn main() { } cmd.args(&["--bin-name", &script_sha256, script_file]); cmd - }; - for arg in args.iter().skip(1) { - run_script_cmd.arg(arg); } +} + +fn run_script_command(script_file: &String, cache_script_bin_name: String, 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) { @@ -71,7 +78,7 @@ fn write_script_file_to_src(script_file: &String, cache_script_bin_name: String) } 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!("Write {} to {}", canonicalized_script_file, cache_script_bin_name_src); + debugging!("Add {} to {}", canonicalized_script_file, cache_script_bin_name_src); } } }