From 11b4e6c005f99d0ec0a3fad9e2f5fc8d0436e0f3 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 18 Jan 2025 20:30:02 +0800 Subject: [PATCH] feat: v1.1.0, support --script-repo|-R --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/install.rs | 10 +++++----- src/list.rs | 4 ++-- src/main.rs | 7 +++++-- src/script.help.rs.txt | 4 ++-- src/util.rs | 30 +++++++++++++++++++++++------- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f4fbde..b5a11db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -927,7 +927,7 @@ dependencies = [ [[package]] name = "runrs" -version = "1.0.1" +version = "1.1.0" dependencies = [ "argh", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index cc8803d..d819725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "1.0.1" +version = "1.1.0" edition = "2018" license = "MIT/Apache-2.0" description = "A Tool for Run Rust Scripts" diff --git a/src/install.rs b/src/install.rs index 9f147af..183d4b3 100644 --- a/src/install.rs +++ b/src/install.rs @@ -5,17 +5,17 @@ use std::fs; use std::os::unix::fs::PermissionsExt; use std::path::PathBuf; -pub fn install_scripts(arguments: &[String]) { - if arguments.is_empty() { +pub fn install_scripts(script_repo: &Option, scripts: &[String]) { + if scripts.is_empty() { failure_and_exit!("No scripts assigned."); } - let script_meta_map = util::fetch_script_meta_or_die(); - for (i, script_name) in arguments.iter().enumerate() { + let script_meta_map = util::fetch_script_meta_or_die(script_repo); + for (i, script_name) in scripts.iter().enumerate() { information!( "Installing script: {} [ {} / {} ]", script_name, (i + 1), - arguments.len() + scripts.len() ); install_script(script_name, &script_meta_map); } diff --git a/src/list.rs b/src/list.rs index 703a304..e7d7dd6 100644 --- a/src/list.rs +++ b/src/list.rs @@ -2,8 +2,8 @@ use crate::util; use std::fs; use std::path::PathBuf; -pub fn list_scripts(filter: Option<&String>) { - let script_meta_map = util::fetch_script_meta_or_die(); +pub fn list_scripts(script_repo: &Option, filter: Option<&String>) { + let script_meta_map = util::fetch_script_meta_or_die(script_repo); let mut messages = vec![]; for script_meta in script_meta_map.values() { diff --git a/src/main.rs b/src/main.rs index c08d787..4f18075 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,9 @@ struct RunScriptArgs { /// install script #[argh(switch, short = 'i')] install: bool, + /// script repo + #[argh(option, short = 'R')] + script_repo: Option, /// output #[argh(option, short = 'o')] output: Option, @@ -53,11 +56,11 @@ fn main() { return; } if rs_args.list { - list::list_scripts(rs_args.arguments.get(0)); + list::list_scripts(&rs_args.script_repo, rs_args.arguments.get(0)); return; } if rs_args.install { - install::install_scripts(&rs_args.arguments); + install::install_scripts(&rs_args.script_repo, &rs_args.arguments); return; } diff --git a/src/script.help.rs.txt b/src/script.help.rs.txt index 2584e2d..508e8f7 100644 --- a/src/script.help.rs.txt +++ b/src/script.help.rs.txt @@ -7,10 +7,10 @@ Show template: $ runrs -t|--template [--output script.rs] Show scriptions: -$ runrs -l|--list [filter] +$ runrs [-R|--script-repo runrs] -l|--list [filter] Install script: -$ runrs -i|--install +$ runrs [-R|--script-repo runrs] -i|--install Run script: $ runrs [arguments] diff --git a/src/util.rs b/src/util.rs index 32ace6e..a6128ee 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,6 +8,19 @@ use std::process::Command; use std::time::SystemTime; use std::{env, fs, process}; +#[cfg(feature = "switch-rust-lang")] +const RS_SCRIPT_META_URL: &str = + "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta-v2.json"; +#[cfg(feature = "switch-go-lang")] +const GO_SCRIPT_META_URL: &str = + "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta-v2.json"; +#[cfg(feature = "switch-ts-lang")] +const TS_SCRIPT_META_URL: &str = + "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/script-meta-v2.json"; +#[cfg(feature = "switch-dart-lang")] +const DART_SCRIPT_META_URL: &str = + "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/script-meta-v2.json"; + #[allow(dead_code)] #[derive(Deserialize)] pub struct ScriptMeta { @@ -19,15 +32,18 @@ pub struct ScriptMeta { } #[allow(unreachable_code)] -pub fn get_script_meta_url() -> &'static str { +pub fn get_script_meta_url(script_repo: &Option) -> String { + if let Some(script_repo) = script_repo { + return format!("https://hatter.ink/script/{script_repo}/script-meta-v2.action"); + } #[cfg(feature = "switch-rust-lang")] - return "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta-v2.json"; + return RS_SCRIPT_META_URL.to_string(); #[cfg(feature = "switch-go-lang")] - return "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta-v2.json"; + return GO_SCRIPT_META_URL.to_string(); #[cfg(feature = "switch-ts-lang")] - return "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/script-meta-v2.json"; + return TS_SCRIPT_META_URL.to_string(); #[cfg(feature = "switch-dart-lang")] - return "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/script-meta-v2.json"; + return DART_SCRIPT_META_URL.to_string(); panic!("Unknown feature assigned."); } @@ -44,8 +60,8 @@ pub fn get_cmd_name() -> &'static str { panic!("Unknown feature assigned."); } -pub fn fetch_script_meta_or_die() -> BTreeMap { - let file_meta_url = get_script_meta_url(); +pub fn fetch_script_meta_or_die(script_repo: &Option) -> BTreeMap { + let file_meta_url = get_script_meta_url(script_repo); debugging!("Loading URL: {}", file_meta_url); let response = reqwest::blocking::get(file_meta_url).unwrap_or_else(|e| { failure_and_exit!("Get file-meta.json failed: {}", e);