From 18282e22233ded776336f129265bdc9fbe4cf748 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 29 Dec 2024 22:30:46 +0800 Subject: [PATCH] feat: v0.2.6 --- Cargo.lock | 2 +- Cargo.toml | 7 ++++-- justfile | 18 ++++++++++++++-- src/help_dart.rs | 19 +++++++++++++++++ src/help_go.rs | 3 --- src/help_js.rs | 19 +++++++++++++++++ src/install.rs | 10 ++++++--- src/list.rs | 10 ++++++--- src/main.rs | 46 +++++++++++++++++++++++++--------------- src/script.template.dart | 5 +++++ src/script.template.js | 3 +++ src/util.rs | 12 +++++++++-- 12 files changed, 121 insertions(+), 33 deletions(-) create mode 100644 src/help_dart.rs create mode 100644 src/help_js.rs create mode 100644 src/script.template.dart create mode 100644 src/script.template.js diff --git a/Cargo.lock b/Cargo.lock index f23a7a3..6e8e84a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -710,7 +710,7 @@ dependencies = [ [[package]] name = "runrs" -version = "0.2.5" +version = "0.2.6" dependencies = [ "reqwest", "rust_util", diff --git a/Cargo.toml b/Cargo.toml index 9de3d0f..eba5dff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "0.2.5" +version = "0.2.6" edition = "2018" license = "MIT/Apache-2.0" description = "A Tool for Run Rust Scripts" @@ -9,8 +9,11 @@ readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = [] +default = ["switch-rust-lang"] +switch-rust-lang = [] switch-go-lang = [] +switch-js-lang = [] +switch-dart-lang = [] [dependencies] reqwest = { version = "0.11.14", features = ["blocking"] } diff --git a/justfile b/justfile index ad3398a..634f884 100644 --- a/justfile +++ b/justfile @@ -4,6 +4,20 @@ _: install: cargo install --path . -build-run-go: - cargo build --release --features switch-go-lang +build-run-rust: + cargo build --release +build-run-go: + cargo build --release --no-default-features --features switch-go-lang + +build-run-js: + cargo build --release --no-default-features --features switch-js-lang + +build-run-dart: + cargo build --release --no-default-features --features switch-dart-lang + +build-all: + @just build-run-rust + @just build-run-go + @just build-run-js + @just build-run-dart \ No newline at end of file diff --git a/src/help_dart.rs b/src/help_dart.rs new file mode 100644 index 0000000..0110e48 --- /dev/null +++ b/src/help_dart.rs @@ -0,0 +1,19 @@ +pub fn print_help() { + println!(r##"rundart v{} - A Tool for Run dart Scripts + +Show help: +$ rundart -h|--help + +Show template: +$ rundart -t|--template + +Show scriptions: +$ rundart -l|--list + +Install script: +$ rundart -i|--install + +"##, + env!("CARGO_PKG_VERSION") + ); +} \ No newline at end of file diff --git a/src/help_go.rs b/src/help_go.rs index f4de7ce..c751ded 100644 --- a/src/help_go.rs +++ b/src/help_go.rs @@ -13,9 +13,6 @@ $ rungo -l|--list Install script: $ rungo -i|--install -Run script: -$ rungo [arguments] - Powered by gorun, know more reference: https://github.com/erning/gorun "##, diff --git a/src/help_js.rs b/src/help_js.rs new file mode 100644 index 0000000..7ec5fc1 --- /dev/null +++ b/src/help_js.rs @@ -0,0 +1,19 @@ +pub fn print_help() { + println!(r##"runjs v{} - A Tool for Run JS Scripts + +Show help: +$ runjs -h|--help + +Show template: +$ runjs -t|--template + +Show scriptions: +$ runjs -l|--list + +Install script: +$ runjs -i|--install + +"##, + env!("CARGO_PKG_VERSION") + ); +} \ No newline at end of file diff --git a/src/install.rs b/src/install.rs index ac1dfbf..278db6a 100644 --- a/src/install.rs +++ b/src/install.rs @@ -5,10 +5,14 @@ use std::path::PathBuf; use rust_util::util_os::get_user_home; use crate::util::{SCRIPT_DOT_EXT, SCRIPT_HYPHEN_EXT}; -#[cfg(not(feature = "switch-go-lang"))] -const SCRIPT_PATTERN: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs"; +#[cfg(feature = "switch-rust-lang")] +const SCRIPT_PATTERN: &str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/$NAME/src/main.rs"; #[cfg(feature = "switch-go-lang")] -const SCRIPT_PATTERN: &'static str = "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/$NAME/main.go"; +const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/$NAME/main.go"; +#[cfg(feature = "switch-js-lang")] +const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/js-scripts/raw/branch/main/$NAME/main.js"; +#[cfg(feature = "switch-dart-lang")] +const SCRIPT_PATTERN: &str = "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/$NAME/main.dart"; pub fn install_script(args: Vec<&String>) { if args.is_empty() { diff --git a/src/list.rs b/src/list.rs index 0aa0919..70f9e30 100644 --- a/src/list.rs +++ b/src/list.rs @@ -5,10 +5,14 @@ use std::fs; use std::path::PathBuf; use crate::util::{SCRIPT_DOT_EXT, SCRIPT_HYPHEN_EXT}; -#[cfg(not(feature = "switch-go-lang"))] -const FILE_META: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta.json"; +#[cfg(feature = "switch-rust-lang")] +const FILE_META: &str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta.json"; #[cfg(feature = "switch-go-lang")] -const FILE_META: &'static str = "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta.json"; +const FILE_META: &str = "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/script-meta.json"; +#[cfg(feature = "switch-js-lang")] +const FILE_META: &str = "https://git.hatter.ink/hatter/js-scripts/raw/branch/main/script-meta.json"; +#[cfg(feature = "switch-dart-lang")] +const FILE_META: &str = "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/script-meta.json"; #[derive(Deserialize)] struct ScriptMeta { diff --git a/src/main.rs b/src/main.rs index 1d63c91..3c18ee5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,33 +5,32 @@ use std::env; use rust_util::util_os::get_user_home; -#[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, }; +#[cfg(feature = "switch-rust-lang")] +mod help_rs; #[cfg(feature = "switch-go-lang")] mod help_go; -#[cfg(not(feature = "switch-go-lang"))] -mod help_rs; +#[cfg(feature = "switch-js-lang")] +mod help_js; +#[cfg(feature = "switch-dart-lang")] +mod help_dart; mod install; mod list; mod util; -#[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"))] +#[cfg(feature = "switch-rust-lang")] const CMD_NAME: &str = "runrs"; #[cfg(feature = "switch-go-lang")] const CMD_NAME: &str = "rungo"; +#[cfg(feature = "switch-js-lang")] +const CMD_NAME: &str = "runjs"; +#[cfg(feature = "switch-dart-lang")] +const CMD_NAME: &str = "rundart"; fn main() { let args = env::args().skip(1).collect::>(); @@ -48,11 +47,25 @@ fn main() { .get(0) .unwrap_or_else(|| failure_and_exit!("Must assign a script file name")); if first_argument == "--help" || first_argument == "-h" { - print_help(); + #[cfg(feature = "switch-rust-lang")] + help_rs::print_help(); + #[cfg(feature = "switch-go-lang")] + help_go::print_help(); + #[cfg(feature = "switch-js-lang")] + help_js::print_help(); + #[cfg(feature = "switch-dart-lang")] + help_dart::print_help(); return; } if first_argument == "--template" || first_argument == "-t" { - println!("{}", SCRIPT_TEMPLATE); + #[cfg(feature = "switch-rust-lang")] + println!("{}", include_str!("script.template.rs")); + #[cfg(feature = "switch-go-lang")] + println!("{}", include_str!("script.template.go")); + #[cfg(feature = "switch-js-lang")] + println!("{}", include_str!("script.template.js")); + #[cfg(feature = "switch-dart-lang")] + println!("{}", include_str!("script.template.dart")); return; } if first_argument == "--list" || first_argument == "-l" { @@ -64,10 +77,9 @@ fn main() { return; } - #[cfg(feature = "switch-go-lang")] + #[cfg(any(feature = "switch-go-lang", feature = "switch-js-lang", feature = "switch-dart-lang"))] if true { - // rungo, and run .go file is not supported - failure_and_exit!("Go script should run by gorun, template for: rungo -t"); + failure_and_exit!("Only rust supports run by runrs."); } let script_file = first_argument; diff --git a/src/script.template.dart b/src/script.template.dart new file mode 100644 index 0000000..15b4cd4 --- /dev/null +++ b/src/script.template.dart @@ -0,0 +1,5 @@ +#!/usr/bin/env dart + +main(List args) { + print("Hello world."); +} \ No newline at end of file diff --git a/src/script.template.js b/src/script.template.js new file mode 100644 index 0000000..faf8007 --- /dev/null +++ b/src/script.template.js @@ -0,0 +1,3 @@ +#!/usr/bin/env -S deno run --allow-env + +console.log("Hello world."); \ No newline at end of file diff --git a/src/util.rs b/src/util.rs index 63423cc..9350209 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,15 +5,23 @@ use std::process::Command; use std::time::SystemTime; use std::{env, fs, process}; -#[cfg(not(feature = "switch-go-lang"))] +#[cfg(feature = "switch-rust-lang")] pub const SCRIPT_HYPHEN_EXT: &str = "-rs"; #[cfg(feature = "switch-go-lang")] pub const SCRIPT_HYPHEN_EXT: &str = "-go"; +#[cfg(feature = "switch-js-lang")] +pub const SCRIPT_HYPHEN_EXT: &str = "-js"; +#[cfg(feature = "switch-dart-lang")] +pub const SCRIPT_HYPHEN_EXT: &str = "-dart"; -#[cfg(not(feature = "switch-go-lang"))] +#[cfg(feature = "switch-rust-lang")] pub const SCRIPT_DOT_EXT: &str = ".rs"; #[cfg(feature = "switch-go-lang")] pub const SCRIPT_DOT_EXT: &str = ".go"; +#[cfg(feature = "switch-js-lang")] +pub const SCRIPT_DOT_EXT: &str = ".js"; +#[cfg(feature = "switch-dart-lang")] +pub const SCRIPT_DOT_EXT: &str = ".dart"; pub fn build_script_command(rust_script: PathBuf, script_file: &str, script_sha256: &str, cache_script_bin_name: &str) -> Command {