refactor: refactor main.rs

This commit is contained in:
2022-08-06 16:23:43 +08:00
parent a1f7be1aa4
commit 71cef0d1a9

View File

@@ -28,13 +28,20 @@ fn main() {
let (_, script_sha256) = read_file_and_digest(script_file); let (_, script_sha256) = read_file_and_digest(script_file);
debugging!("File {} -> sha256: {}", script_file, script_sha256); 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 skip_cache = is_env_on("SKIP_CACHE");
let cache_script_bin_name_exists = fs::metadata(&cache_script_bin_name).is_ok(); 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 ")); 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) Command::new(&cache_script_bin_name)
} else { } else {
let mut cmd = Command::new(rust_script); let mut cmd = Command::new(rust_script);
@@ -43,10 +50,10 @@ fn main() {
} }
cmd.args(&["--bin-name", &script_sha256, script_file]); cmd.args(&["--bin-name", &script_sha256, script_file]);
cmd 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); debugging!("Run command: {:?}", run_script_cmd);
let run_command_start = SystemTime::now(); let run_command_start = SystemTime::now();
match util_cmd::run_command_and_wait(&mut run_script_cmd) { 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); 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) { 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);
} }
} }
} }