feat: add publish date and update time
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
"script_name": "helloworld.dart",
|
"script_name": "helloworld.dart",
|
||||||
"script_length": 74,
|
"script_length": 74,
|
||||||
"script_sha256": "a4ec48f29f109da6bddedd6044362310b15451cd467bfc79594e34e5179393dd",
|
"script_sha256": "a4ec48f29f109da6bddedd6044362310b15451cd467bfc79594e34e5179393dd",
|
||||||
"script_full_url": "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/helloworld-dart/main.dart"
|
"script_full_url": "https://git.hatter.ink/hatter/dart-scripts/raw/branch/main/helloworld-dart/main.dart",
|
||||||
|
"publish_time": 1737272687810,
|
||||||
|
"update_time": 1737272687810
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,8 @@
|
|||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use rust_util::{
|
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 serde::{Deserialize, Serialize};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
@@ -41,7 +42,7 @@ impl ScriptConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ScriptMeta {
|
struct ScriptMeta {
|
||||||
script_name: String,
|
script_name: String,
|
||||||
script_length: u64,
|
script_length: u64,
|
||||||
@@ -49,6 +50,10 @@ struct ScriptMeta {
|
|||||||
script_full_url: String,
|
script_full_url: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
single_script_file: Option<bool>,
|
single_script_file: Option<bool>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
publish_time: Option<u128>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
update_time: Option<u128>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
@@ -71,6 +76,7 @@ fn main() -> XResult<()> {
|
|||||||
);
|
);
|
||||||
debugging!("Script config: {:#?}", script_config);
|
debugging!("Script config: {:#?}", script_config);
|
||||||
|
|
||||||
|
let former_script_meta_map = read_script_meta_map_from_file();
|
||||||
let mut script_meta_map = BTreeMap::new();
|
let mut script_meta_map = BTreeMap::new();
|
||||||
|
|
||||||
let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}");
|
let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}");
|
||||||
@@ -105,6 +111,7 @@ fn main() -> XResult<()> {
|
|||||||
&main_script,
|
&main_script,
|
||||||
false,
|
false,
|
||||||
&script_config,
|
&script_config,
|
||||||
|
&former_script_meta_map,
|
||||||
)?,
|
)?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -142,6 +149,7 @@ fn main() -> XResult<()> {
|
|||||||
&abs_file_entry,
|
&abs_file_entry,
|
||||||
true,
|
true,
|
||||||
&script_config,
|
&script_config,
|
||||||
|
&former_script_meta_map,
|
||||||
)?,
|
)?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -157,6 +165,14 @@ fn main() -> XResult<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_script_meta_map_from_file() -> BTreeMap<String, ScriptMeta> {
|
||||||
|
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
|
// translate filename-ext to filename.ext
|
||||||
fn translate_script_dir_to_script_name(
|
fn translate_script_dir_to_script_name(
|
||||||
script_dir: &str,
|
script_dir: &str,
|
||||||
@@ -180,6 +196,7 @@ fn read_script_meta(
|
|||||||
script_path: &PathBuf,
|
script_path: &PathBuf,
|
||||||
is_simple_script: bool,
|
is_simple_script: bool,
|
||||||
script_config: &ScriptConfig,
|
script_config: &ScriptConfig,
|
||||||
|
former_script_meta_map: &BTreeMap<String, ScriptMeta>,
|
||||||
) -> XResult<ScriptMeta> {
|
) -> XResult<ScriptMeta> {
|
||||||
let script_meta = opt_result!(
|
let script_meta = opt_result!(
|
||||||
script_path.metadata(),
|
script_path.metadata(),
|
||||||
@@ -198,11 +215,31 @@ fn read_script_meta(
|
|||||||
.replace("$NAME", script_dir)
|
.replace("$NAME", script_dir)
|
||||||
};
|
};
|
||||||
let single_script_file = iff!(is_simple_script, Some(true), None);
|
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 {
|
Ok(ScriptMeta {
|
||||||
script_name: script_name.clone(),
|
script_name: script_name.clone(),
|
||||||
script_length: script_meta.len(),
|
script_length: script_meta.len(),
|
||||||
script_sha256,
|
script_sha256,
|
||||||
script_full_url,
|
script_full_url,
|
||||||
single_script_file,
|
single_script_file,
|
||||||
|
publish_time,
|
||||||
|
update_time,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user