This commit is contained in:
2026-03-21 00:16:26 +08:00
parent 62a178f177
commit 2cb4285503
4 changed files with 66 additions and 9 deletions

2
Cargo.lock generated
View File

@@ -1221,7 +1221,7 @@ dependencies = [
[[package]]
name = "runrs"
version = "1.1.10"
version = "1.1.11"
dependencies = [
"argh",
"reqwest",

View File

@@ -1,6 +1,6 @@
[package]
name = "runrs"
version = "1.1.10"
version = "1.1.11"
edition = "2018"
license = "MIT/Apache-2.0"
description = "A Tool for Run Rust/TypeScript Scripts"

View File

@@ -1,3 +1,4 @@
use std::fs;
use crate::resolver::resolve_file;
use crate::{util, verify, RunScriptArgs};
use rust_util::util_env::is_env_on;
@@ -11,6 +12,7 @@ pub fn do_run_script(args: &RunScriptArgs) {
let script_file = resolve_file(script_file, args.force_update)
.unwrap_or_else(|e| failure_and_exit!("Failed to resolve script: {}", e));
let script_file = &script_file;
let script_file_name = get_script_name(script_file);
verify::verify_script(script_file, is_env_on("RUNRS_SKIP_VERIFY") || is_env_on("RSV"));
@@ -19,8 +21,9 @@ pub fn do_run_script(args: &RunScriptArgs) {
let user_home = get_user_home().unwrap_or_else(|| failure_and_exit!("$HOME not found!"));
let rust_script = util::get_run_script_bin_name(&user_home);
let cache_script_bin_name = get_cache_script_bin_name(&user_home, &script_sha256);
let mut run_script_cmd = util::build_script_command(
let script_bin_name = get_cache_script_bin_name(&user_home, &script_sha256);
let cache_script_bin_name = get_cache_script_bin_name_with_rs(&user_home, &script_sha256, &script_file_name);
let (mut run_script_cmd, cached) = util::build_script_command(
rust_script,
script_file,
&script_sha256,
@@ -29,7 +32,19 @@ pub fn do_run_script(args: &RunScriptArgs) {
for arg in args.arguments.iter().skip(1) {
run_script_cmd.arg(arg);
}
util::run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd)
util::run_script_command(script_file, &script_bin_name, &mut run_script_cmd, || {
if !cached {
debugging!("Checking file : {}", &script_bin_name);
if fs::metadata(&script_bin_name).is_ok() {
debugging!("Linking file: {} & {}", &script_bin_name, &cache_script_bin_name);
match std::os::unix::fs::symlink(&script_bin_name, &cache_script_bin_name) {
Ok(_) => debugging!("Link file success"),
Err(e) => failure!("Linked file: {} and {} failed: {}", &script_bin_name, &cache_script_bin_name, &e),
}
}
}
()
})
}
fn get_cache_script_bin_name(user_home: &str, script_sha256: &str) -> String {
@@ -46,3 +61,40 @@ fn get_cache_script_bin_name(user_home: &str, script_sha256: &str) -> String {
);
cache_script_bin_name
}
fn get_cache_script_bin_name_with_rs(user_home: &str, script_sha256: &str, script_file_name: &str) -> String {
#[cfg(target_os = "macos")]
let cache_script_bin_path = format!(
"{}/Library/Caches/rust-script/binaries/release/{}-bin",
user_home, script_sha256
);
// #[cfg(target_os = "linux")]
#[cfg(not(target_os = "macos"))]
let cache_script_bin_path = format!(
"{}/.cache/rust-script/binaries/release/{}-bin",
user_home, script_sha256
);
if fs::metadata(&cache_script_bin_path).is_err() {
debugging!("Creating cache script bin: {}", cache_script_bin_path);
fs::create_dir_all(&cache_script_bin_path).ok();
}
#[cfg(target_os = "macos")]
let cache_script_bin_name = format!(
"{}/{}", cache_script_bin_path, script_file_name
);
// #[cfg(target_os = "linux")]
#[cfg(not(target_os = "macos"))]
let cache_script_bin_name = format!(
"{}/{}", cache_script_bin_path, script_file_name
);
cache_script_bin_name
}
fn get_script_name(script_file: &str) -> String {
let last_part = script_file.split("/").last();
if let Some(last_part) = last_part {
return last_part.to_string();
}
script_file.to_string()
}

View File

@@ -92,7 +92,7 @@ pub fn build_script_command(
script_file: &str,
script_sha256: &str,
cache_script_bin_name: &str,
) -> Command {
) -> (Command, bool) {
let skip_cache = is_env_on("RUNRS_SKIP_CACHE");
let rebuild = is_env_on("RUNRS_REBUILD");
let cache_script_bin_name_exists = fs::metadata(cache_script_bin_name).is_ok();
@@ -102,7 +102,7 @@ pub fn build_script_command(
iff!(cache_script_bin_name_exists, "", "not ")
);
if !rebuild && !skip_cache && cache_script_bin_name_exists {
Command::new(cache_script_bin_name)
(Command::new(cache_script_bin_name), true)
} else {
let mut cmd = Command::new(rust_script);
if !is_env_on("RUNRS_SILENT_BUILD") {
@@ -113,7 +113,7 @@ pub fn build_script_command(
cmd.arg("--force");
}
cmd.args(["--bin-name", script_sha256, script_file]);
cmd
(cmd, false)
}
}
@@ -121,16 +121,21 @@ pub fn run_script_command(
script_file: &str,
cache_script_bin_name: &str,
run_script_cmd: &mut Command,
after_command: impl Fn() -> (),
) -> ! {
debugging!("Run command: {:?}", run_script_cmd);
let run_command_start = SystemTime::now();
match util_cmd::run_command_and_wait(run_script_cmd) {
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
Err(e) => {
after_command();
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());
}
after_command();
process::exit(exit_status.code().unwrap_or(0))
}
}