diff --git a/script-meta-v2.json b/script-meta-v2.json index 2940bd6..cb91887 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -3,85 +3,113 @@ "script_name": "bit-address.rs", "script_length": 3877, "script_sha256": "806980fef2db63d42024010bc7bc010664ec616662164fb6490ca9389502d869", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/bit-address-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/bit-address-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "commit-msg.rs": { "script_name": "commit-msg.rs", "script_length": 6618, "script_sha256": "63d1569f1ae8b5b9c14e3be8a79841dbf67e954c16e1bb16f07c2b2f6660c88f", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/commit-msg-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/commit-msg-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "current-time.rs": { "script_name": "current-time.rs", "script_length": 3301, "script_sha256": "51536795cafedf593184a2690f08ee8fc2be9b5dccdd8a52fafa025d66c26e96", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/current-time-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/current-time-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "decrypt.rs": { "script_name": "decrypt.rs", "script_length": 3087, "script_sha256": "7f5bb37855da9b350fb1ca646fd75a3a71533a8f5f7913ac81036ad1b2082f3c", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/decrypt-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/decrypt-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "derparse.rs": { "script_name": "derparse.rs", "script_length": 7368, "script_sha256": "423eb3c6ce57baf7bc8c9f452fdead2f3dc3d86f3724aa8bd7016ece8ce1853b", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/derparse-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/derparse-rs/src/main.rs", + "publish_time": 1737272563312, + "update_time": 1737272563312 }, "helloworld.rs": { "script_name": "helloworld.rs", "script_length": 64, "script_sha256": "b1cf245146e6684986b1f0788d2282fdb1c617eac0961a84bfbd65f386975993", "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/single-scripts/helloworld.rs", - "single_script_file": true + "single_script_file": true, + "publish_time": 1737272563312, + "update_time": 1737272563312 }, "makepassword.rs": { "script_name": "makepassword.rs", "script_length": 1966, "script_sha256": "0ca721d3531a9cc94c95193e7148c0316551fd65d84de7f54bd4b6418ff37f01", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/makepassword-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/makepassword-rs/src/main.rs", + "publish_time": 1737272563310, + "update_time": 1737272563310 }, "myip.rs": { "script_name": "myip.rs", "script_length": 933, "script_sha256": "9756186298cf6a65561760603e6369ee961e3271ea0cf86361b88c2250c3e690", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/myip-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/myip-rs/src/main.rs", + "publish_time": 1737272563310, + "update_time": 1737272563310 }, "osssendfile.rs": { "script_name": "osssendfile.rs", "script_length": 13853, "script_sha256": "218eecef46329fb6d73986d3db1a07dd29fe99269f46290eda165e5f3d663ca9", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/osssendfile-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/osssendfile-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "post.rs": { "script_name": "post.rs", "script_length": 10494, "script_sha256": "fe3571fdcd170608575c610ba51fcaeee7aea0fe82ae4bc22032f61c7787d22f", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/post-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/post-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "rpm-read.rs": { "script_name": "rpm-read.rs", "script_length": 637, "script_sha256": "2338fb99c2803fc36ecdb09ec04eeb9aefb3524f8a68db4875562fdf3c8ba3f8", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/rpm-read-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/rpm-read-rs/src/main.rs", + "publish_time": 1737272563312, + "update_time": 1737272563312 }, "script-sign.rs": { "script_name": "script-sign.rs", "script_length": 16506, "script_sha256": "d8dc82553a5300ecee3364ed04596163295139f8b18e0d1a95b89ea6540ebfe5", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-sign-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/script-sign-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 }, "sync.rs": { "script_name": "sync.rs", "script_length": 1818, "script_sha256": "d947c179820a9d13284de9b8d0173f1738a06fbcacb40473a02aa94417c53454", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/sync-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/sync-rs/src/main.rs", + "publish_time": 1737272563310, + "update_time": 1737272563310 }, "xattr.rs": { "script_name": "xattr.rs", "script_length": 1868, "script_sha256": "e558bc16c70801aaca9bf943a45e6fc90ec925e6fe34a245cea68daca7b45afa", - "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/xattr-rs/src/main.rs" + "script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/xattr-rs/src/main.rs", + "publish_time": 1737272563311, + "update_time": 1737272563311 } } \ No newline at end of file diff --git a/update-meta-rs/src/main.rs b/update-meta-rs/src/main.rs index f14586c..58b61c1 100755 --- a/update-meta-rs/src/main.rs +++ b/update-meta-rs/src/main.rs @@ -9,7 +9,8 @@ //! ``` use rust_util::{ - debugging, failure_and_exit, iff, opt_result, opt_value_result, success, warning, XResult, + debugging, failure_and_exit, iff, opt_result, opt_value_result, success, util_time, warning, + XResult, }; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; @@ -41,7 +42,7 @@ impl ScriptConfig { } } -#[derive(Serialize)] +#[derive(Serialize, Deserialize)] struct ScriptMeta { script_name: String, script_length: u64, @@ -49,6 +50,10 @@ struct ScriptMeta { script_full_url: String, #[serde(skip_serializing_if = "Option::is_none")] single_script_file: Option, + #[serde(skip_serializing_if = "Option::is_none")] + publish_time: Option, + #[serde(skip_serializing_if = "Option::is_none")] + update_time: Option, } fn main() -> XResult<()> { @@ -71,6 +76,7 @@ fn main() -> XResult<()> { ); debugging!("Script config: {:#?}", script_config); + let former_script_meta_map = read_script_meta_map_from_file(); let mut script_meta_map = BTreeMap::new(); let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}"); @@ -105,6 +111,7 @@ fn main() -> XResult<()> { &main_script, false, &script_config, + &former_script_meta_map, )?, ); } @@ -142,6 +149,7 @@ fn main() -> XResult<()> { &abs_file_entry, true, &script_config, + &former_script_meta_map, )?, ); } @@ -157,6 +165,14 @@ fn main() -> XResult<()> { Ok(()) } +fn read_script_meta_map_from_file() -> BTreeMap { + let script_meta_content = match fs::read_to_string(SCRIPT_META_FILE) { + Ok(script_meta_content) => script_meta_content, + Err(_) => return BTreeMap::new(), + }; + serde_json::from_str(&script_meta_content).unwrap_or_default() +} + // translate filename-ext to filename.ext fn translate_script_dir_to_script_name( script_dir: &str, @@ -180,6 +196,7 @@ fn read_script_meta( script_path: &PathBuf, is_simple_script: bool, script_config: &ScriptConfig, + former_script_meta_map: &BTreeMap, ) -> XResult { let script_meta = opt_result!( script_path.metadata(), @@ -198,11 +215,31 @@ fn read_script_meta( .replace("$NAME", script_dir) }; let single_script_file = iff!(is_simple_script, Some(true), None); + let former_script_meta = former_script_meta_map.get(&script_name); + let publish_time = Some( + former_script_meta + .map(|m| m.publish_time) + .flatten() + .unwrap_or_else(|| util_time::get_current_millis()), + ); + let is_file_same = former_script_meta + .map(|m| m.script_sha256 == script_sha256) + .unwrap_or(false); + let update_time = Some(if is_file_same { + former_script_meta + .map(|m| m.update_time) + .flatten() + .unwrap_or_else(|| util_time::get_current_millis()) + } else { + util_time::get_current_millis() + }); Ok(ScriptMeta { script_name: script_name.clone(), script_length: script_meta.len(), script_sha256, script_full_url, single_script_file, + publish_time, + update_time, }) }