feat: v0.2.6

This commit is contained in:
2024-12-29 22:30:46 +08:00
parent 3ace53b509
commit 18282e2223
12 changed files with 121 additions and 33 deletions

2
Cargo.lock generated
View File

@@ -710,7 +710,7 @@ dependencies = [
[[package]]
name = "runrs"
version = "0.2.5"
version = "0.2.6"
dependencies = [
"reqwest",
"rust_util",

View File

@@ -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"] }

View File

@@ -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

19
src/help_dart.rs Normal file
View File

@@ -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 <script.dart>
"##,
env!("CARGO_PKG_VERSION")
);
}

View File

@@ -13,9 +13,6 @@ $ rungo -l|--list
Install script:
$ rungo -i|--install <script.go>
Run script:
$ rungo <script.rs> [arguments]
Powered by gorun, know more reference: https://github.com/erning/gorun
"##,

19
src/help_js.rs Normal file
View File

@@ -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 <script.js>
"##,
env!("CARGO_PKG_VERSION")
);
}

View File

@@ -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() {

View File

@@ -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 {

View File

@@ -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::<Vec<_>>();
@@ -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;

5
src/script.template.dart Normal file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env dart
main(List<String> args) {
print("Hello world.");
}

3
src/script.template.js Normal file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env -S deno run --allow-env
console.log("Hello world.");

View File

@@ -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 {