diff --git a/Cargo.lock b/Cargo.lock index 49367f3..eb3c6fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -634,18 +634,18 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -710,10 +710,12 @@ dependencies = [ [[package]] name = "runrs" -version = "0.2.3" +version = "0.2.4" dependencies = [ "reqwest", "rust_util", + "serde", + "serde_json", "sha256", ] @@ -794,18 +796,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.193" +version = "1.0.208" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.208" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" dependencies = [ "proc-macro2", "quote", @@ -814,11 +816,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -890,9 +893,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.39" +version = "2.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 68b06d2..86440d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "0.2.3" +version = "0.2.4" edition = "2018" license = "MIT/Apache-2.0" description = "A Tool for Run Rust Scripts" @@ -12,3 +12,5 @@ readme = "README.md" reqwest = { version = "0.11.14", features = ["blocking"] } rust_util = "0.6.41" sha256 = "1.0.3" +serde = { version = "1.0.193", features = ["derive"] } +serde_json = "1.0.125" diff --git a/src/list.rs b/src/list.rs index 3f8555c..33d4401 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,4 +1,17 @@ -const FILE_META: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/file-meta.txt"; +use rust_util::util_os::get_user_home; +use serde::Deserialize; +use std::collections::BTreeMap; +use std::fs; +use std::path::PathBuf; + +const FILE_META: &'static str = "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-meta.json"; + +#[derive(Deserialize)] +struct ScriptMeta { + script_name: String, + script_length: u64, + script_sha256: String, +} pub fn list_scripts() { debugging!("Loading URL: {}", FILE_META); @@ -14,18 +27,35 @@ pub fn list_scripts() { }); debugging!("Response text: {}", &text); - let mut scripts: Vec<&str> = text.trim().split("\n").collect(); - scripts.sort(); + let script_meta_map: BTreeMap = serde_json::from_str(&text).expect("Parse script-meta.json failed."); - success!("Found {} script(s):\n{}", - scripts.len(), - scripts.iter().map(|script| { - let script = if script.ends_with("-rs") { - script.chars().take(script.len() - 3).collect::() + ".rs" - } else { - script.to_string() - }; - format!("- {}", script) - }).collect::>().join("\n") - ); + let mut messages = vec![]; + messages.push(format!("Found {} script(s):", script_meta_map.len())); + for (_, script_meta) in &script_meta_map { + let script_name = &script_meta.script_name; + let real_script_name = if script_name.ends_with("-rs") { + script_name.chars().take(script_name.len() - 3).collect::() + ".rs" + } else { + script_name.to_string() + }; + let user_home = get_user_home().expect("Get user home failed!"); + let full_script_path = PathBuf::from(&user_home).join("bin").join(&real_script_name); + let is_script_exists = full_script_path.is_file(); + let script_sha256 = if is_script_exists { + let script_content = fs::read(&full_script_path).unwrap_or(vec![]); + sha256::digest(&script_content) + } else { + "".to_string() + }; + let is_script_matches = script_sha256 == script_meta.script_sha256; + let script_size = rust_util::util_size::get_display_size(script_meta.script_length as i64); + let padding_length = 40 - real_script_name.len(); + messages.push(format!("- {} {} {} {}", + iff!(is_script_exists, iff!(is_script_matches, "✅", "✔️ "), "❌"), + real_script_name, + ".".repeat(iff!(padding_length < 1, 1, padding_length)), + script_size, + )); + } + success!("{}", messages.join("\n")); } \ No newline at end of file diff --git a/src/util.rs b/src/util.rs index 685d1c0..b875cef 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,9 +1,9 @@ -use std::{env, fs, process}; +use rust_util::util_cmd; +use rust_util::util_env::is_env_on; use std::path::PathBuf; use std::process::Command; use std::time::SystemTime; -use rust_util::util_cmd; -use rust_util::util_env::is_env_on; +use std::{env, fs, process}; pub fn build_script_command(rust_script: PathBuf, script_file: &str, script_sha256: &str, cache_script_bin_name: &str) -> Command {