From 8a109afee46c72e93d8075a4426122db2f8b79d8 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 18 Oct 2021 20:21:22 +0100 Subject: [PATCH] Use a cache of the changed file --- agb-sound-converter/src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/agb-sound-converter/src/lib.rs b/agb-sound-converter/src/lib.rs index 59977271..c778d5ad 100644 --- a/agb-sound-converter/src/lib.rs +++ b/agb-sound-converter/src/lib.rs @@ -3,6 +3,7 @@ use proc_macro::TokenStream; use quote::quote; use siphasher::sip::SipHasher; use std::{ + fs, fs::File, hash::{Hash, Hasher}, io::Write, @@ -38,11 +39,26 @@ pub fn include_wav(input: TokenStream) -> TokenStream { let out_file_path = Path::new(&out_dir).with_file_name(&out_filename); - let mut out_file = File::create(&out_file_path).expect("Failed to open file for writing"); + let out_file_mtime = fs::metadata(&out_file_path) + .and_then(|metadata| metadata.modified()) + .ok(); + let in_file_mtime = fs::metadata(&path) + .and_then(|metadata| metadata.modified()) + .ok(); - out_file - .write_all(&samples.collect::>()) - .expect("Failed to write to temporary file"); + let should_write = match (out_file_mtime, in_file_mtime) { + (Some(out_file_mtime), Some(in_file_mtime)) => out_file_mtime <= in_file_mtime, + _ => true, + }; + + if should_write { + let mut out_file = + File::create(&out_file_path).expect("Failed to open file for writing"); + + out_file + .write_all(&samples.collect::>()) + .expect("Failed to write to temporary file"); + } out_file_path }