feat: v0.2.5, support go scripts

This commit is contained in:
2024-12-29 17:13:41 +08:00
parent affcb233bd
commit 3ace53b509
11 changed files with 163 additions and 31 deletions

View File

@@ -5,27 +5,48 @@ use std::env;
use rust_util::util_os::get_user_home;
use crate::help::print_help;
#[cfg(not(feature = "switch-go-lang"))]
use crate::help_rs::print_help;
#[cfg(feature = "switch-go-lang")]
use crate::help_go::print_help;
use crate::install::install_script;
use crate::list::list_scripts;
use crate::util::{build_script_command, get_run_script_bin_name, read_file_and_digest, run_script_command};
use crate::util::{
build_script_command, get_run_script_bin_name, read_file_and_digest, run_script_command,
};
mod util;
mod help;
mod list;
#[cfg(feature = "switch-go-lang")]
mod help_go;
#[cfg(not(feature = "switch-go-lang"))]
mod help_rs;
mod install;
mod list;
mod util;
const SCRIPT_TEMPLATE: &'static str = include_str!("script.template");
#[cfg(not(feature = "switch-go-lang"))]
const SCRIPT_TEMPLATE: &'static str = include_str!("script.template.rs");
#[cfg(feature = "switch-go-lang")]
const SCRIPT_TEMPLATE: &'static str = include_str!("script.template.go");
#[cfg(not(feature = "switch-go-lang"))]
const CMD_NAME: &str = "runrs";
#[cfg(feature = "switch-go-lang")]
const CMD_NAME: &str = "rungo";
fn main() {
let user_home = get_user_home().unwrap_or_else(|| failure_and_exit!("$HOME not found!"));
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, `runrs -h` or `runrs --help` for help", env!("CARGO_PKG_VERSION"));
failure_and_exit!(
"{} v{}, need arguments, `{} -h` or `{} --help` for help",
CMD_NAME,
env!("CARGO_PKG_VERSION"),
CMD_NAME,
CMD_NAME,
);
}
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
let first_argument = args
.get(0)
.unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
if first_argument == "--help" || first_argument == "-h" {
print_help();
return;
@@ -43,20 +64,42 @@ fn main() {
return;
}
let script_file = first_argument;
#[cfg(feature = "switch-go-lang")]
if true {
// rungo, and run .go file is not supported
failure_and_exit!("Go script should run by gorun, template for: rungo -t");
}
let script_file = first_argument;
let (_, script_sha256) = read_file_and_digest(script_file);
debugging!("File {} -> sha256: {}", script_file, script_sha256);
#[cfg(target_os = "macos")]
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", user_home, script_sha256);
// #[cfg(target_os = "linux")]
#[cfg(not(target_os = "macos"))]
let cache_script_bin_name = format!("{}/.cache/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);
let user_home = get_user_home().unwrap_or_else(|| failure_and_exit!("$HOME not found!"));
let rust_script = 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 = 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 get_cache_script_bin_name(user_home: &str, script_sha256: &str) -> String {
#[cfg(target_os = "macos")]
let cache_script_bin_name = format!(
"{}/Library/Caches/rust-script/binaries/release/{}",
user_home, script_sha256
);
// #[cfg(target_os = "linux")]
#[cfg(not(target_os = "macos"))]
let cache_script_bin_name = format!(
"{}/.cache/rust-script/binaries/release/{}",
user_home, script_sha256
);
cache_script_bin_name
}