Function hyper_scripter::util::file_modify_time
source · Examples found in repository?
src/util/main_util.rs (line 386)
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
pub async fn after_script(
entry: &mut RepoEntry<'_>,
path: &Path,
prepare_resp: &Option<PrepareRespond>,
) -> Result {
let mut record_write = true;
match prepare_resp {
None => {
log::debug!("不執行後處理");
}
Some(PrepareRespond { is_new, time }) => {
let modified = super::file_modify_time(path)?;
if time >= &modified {
if *is_new {
log::info!("新腳本未變動,應刪除之");
return Err(Error::EmptyCreate);
} else {
log::info!("舊腳本未變動,不記錄寫事件(只記讀事件)");
record_write = false;
}
}
}
}
if record_write {
entry.update(|info| info.write()).await?;
}
Ok(())
}More examples
src/util/mod.rs (line 292)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
pub fn prepare_script<T: AsRef<str>>(
path: &Path,
script: &ScriptInfo,
sub_type: Option<&ScriptType>,
no_template: bool,
content: &[T],
) -> Result<PrepareRespond> {
log::info!("開始準備 {} 腳本內容……", script.name);
let has_content = !content.is_empty();
let is_new = !path.exists();
if is_new {
let birthplace = get_birthplace()?;
let birthplace_rel = relative_to_home(&birthplace);
let mut file = handle_fs_res(&[path], File::create(&path))?;
let content = content.iter().map(|s| s.as_ref().split('\n')).flatten();
if !no_template {
let content: Vec<_> = content.collect();
let info = json!({
"birthplace_in_home": birthplace_rel.is_some(),
"birthplace_rel": birthplace_rel,
"birthplace": birthplace,
"name": script.name.key().to_owned(),
"content": content,
});
log::debug!("編輯模版資訊:{:?}", info);
// NOTE: 計算 `path` 時早已檢查過腳本類型,這裡直接不檢查了
let template = get_or_create_template(&(&script.ty, sub_type), true, true)?;
handle_fs_res(&[path], write_prepare_script(file, &template, &info))?;
} else {
let mut first = true;
for line in content {
if !first {
writeln!(file, "")?;
}
first = false;
write!(file, "{}", line)?;
}
}
} else {
if has_content {
log::debug!("腳本已存在,往後接上給定的訊息");
let mut file = handle_fs_res(
&[path],
std::fs::OpenOptions::new()
.append(true)
.write(true)
.open(path),
)?;
for content in content.iter() {
handle_fs_res(&[path], writeln!(&mut file, "{}", content.as_ref()))?;
}
}
}
Ok(PrepareRespond {
is_new,
time: file_modify_time(path)?,
})
}