diff --git a/Cargo.lock b/Cargo.lock index 702e01a..6f4fbde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -927,7 +927,7 @@ dependencies = [ [[package]] name = "runrs" -version = "1.0.0" +version = "1.0.1" dependencies = [ "argh", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index b7be9f1..cc8803d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "1.0.0" +version = "1.0.1" edition = "2018" license = "MIT/Apache-2.0" description = "A Tool for Run Rust Scripts" diff --git a/src/help.rs b/src/help.rs index feaa53f..acce37d 100644 --- a/src/help.rs +++ b/src/help.rs @@ -9,7 +9,7 @@ pub fn print_help() { #[allow(unreachable_code)] fn get_help() -> &'static str { #[cfg(feature = "switch-rust-lang")] - return include_str!("script.help.rust.txt"); + return include_str!("script.help.rs.txt"); #[cfg(feature = "switch-go-lang")] return include_str!("script.help.go.txt"); #[cfg(feature = "switch-ts-lang")] diff --git a/src/main.rs b/src/main.rs index b78cf86..c08d787 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,17 +2,12 @@ extern crate rust_util; use argh::FromArgs; -use rust_util::util_os::get_user_home; use std::env; -use crate::list::list_scripts; -use crate::util::{ - build_script_command, get_run_script_bin_name, read_file_and_digest, run_script_command, -}; - mod help; mod install; mod list; +mod run_rs; mod template; mod util; @@ -41,11 +36,9 @@ struct RunScriptArgs { fn main() { let rs_args: RunScriptArgs = argh::from_env(); - // println!("{:#?}", rs_args); - - let cmd_name = util::get_cmd_name(); let cmd_version = env!("CARGO_PKG_VERSION"); if env::args().len() == 1 { + let cmd_name = util::get_cmd_name(); failure_and_exit!( "{cmd_name} v{cmd_version}, need arguments, `{cmd_name} -h` or `{cmd_name} --help-message` for help" ); @@ -60,7 +53,7 @@ fn main() { return; } if rs_args.list { - list_scripts(rs_args.arguments.get(1)); + list::list_scripts(rs_args.arguments.get(0)); return; } if rs_args.install { @@ -68,48 +61,15 @@ fn main() { return; } - #[cfg(any( - feature = "switch-go-lang", - feature = "switch-ts-lang", - feature = "switch-dart-lang" - ))] - if true { - failure_and_exit!("Only rust supports run by runrs."); - } - - if rs_args.arguments.is_empty() { - failure_and_exit!("Must assign a script file name"); - } - let script_file = &rs_args.arguments[0]; - let (_, script_sha256) = read_file_and_digest(script_file); - debugging!("File {} -> sha256: {}", script_file, script_sha256); - - 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 rs_args.arguments.iter().skip(1) { - run_script_cmd.arg(arg); - } - run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd) + do_run_script(&rs_args); } -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 +#[allow(unreachable_code)] +fn do_run_script(rs_args: &RunScriptArgs) { + #[cfg(feature = "switch-rust-lang")] + { + run_rs::do_run_script(&rs_args); + return; + } + failure_and_exit!("Only rust script supported."); } diff --git a/src/run_rs.rs b/src/run_rs.rs new file mode 100644 index 0000000..e2dcf24 --- /dev/null +++ b/src/run_rs.rs @@ -0,0 +1,40 @@ +use crate::{util, RunScriptArgs}; +use rust_util::util_os::get_user_home; + +pub fn do_run_script(rs_args: &RunScriptArgs) { + if rs_args.arguments.is_empty() { + failure_and_exit!("Must assign a script file name"); + } + let script_file = &rs_args.arguments[0]; + let (_, script_sha256) = util::read_file_and_digest(script_file); + debugging!("File {} -> sha256: {}", script_file, script_sha256); + + 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( + rust_script, + script_file, + &script_sha256, + &cache_script_bin_name, + ); + for arg in rs_args.arguments.iter().skip(1) { + run_script_cmd.arg(arg); + } + util::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 +} diff --git a/src/script.help.dart.txt b/src/script.help.dart.txt index ab7d45f..84ff061 100644 --- a/src/script.help.dart.txt +++ b/src/script.help.dart.txt @@ -7,7 +7,7 @@ Show template: $ rundart -t|--template [--output script.dart] Show scriptions: -$ rundart -l|--list +$ rundart -l|--list [filter] Install script: $ rundart -i|--install diff --git a/src/script.help.go.txt b/src/script.help.go.txt index 81ee72a..26213e5 100644 --- a/src/script.help.go.txt +++ b/src/script.help.go.txt @@ -7,7 +7,7 @@ Show template: $ rungo -t|--template [--output script.go] Show scriptions: -$ rungo -l|--list +$ rungo -l|--list [filter] Install script: $ rungo -i|--install diff --git a/src/script.help.rust.txt b/src/script.help.rs.txt similarity index 98% rename from src/script.help.rust.txt rename to src/script.help.rs.txt index 195274a..2584e2d 100644 --- a/src/script.help.rust.txt +++ b/src/script.help.rs.txt @@ -7,7 +7,7 @@ Show template: $ runrs -t|--template [--output script.rs] Show scriptions: -$ runrs -l|--list +$ runrs -l|--list [filter] Install script: $ runrs -i|--install diff --git a/src/script.help.ts.txt b/src/script.help.ts.txt index f6ec471..24361d2 100644 --- a/src/script.help.ts.txt +++ b/src/script.help.ts.txt @@ -7,7 +7,7 @@ Show template: $ runts -t|--template [--output script.ts] Show scriptions: -$ runts -l|--list +$ runts -l|--list [filter] Install script: $ runts -i|--install