diff --git a/rust/xtask/src/install_dependency.rs b/rust/xtask/src/install_dependency.rs index 814db848..d1ee3904 100644 --- a/rust/xtask/src/install_dependency.rs +++ b/rust/xtask/src/install_dependency.rs @@ -156,6 +156,31 @@ pub fn get_godot_executable_path() -> Option { Some(os_url) } +fn copy_if_modified, Q: AsRef>(src: P, dest: Q) -> io::Result<()> { + let src_path = src.as_ref(); + let dest_path = dest.as_ref(); + + // Obtain the metadata of the source and destination file + let metadata_src = fs::metadata(src_path); + let metadata_dest = fs::metadata(dest_path); + + // If both files exist, we compare their modification times + if metadata_src.is_ok() && metadata_dest.is_ok() { + let time_src = metadata_src?.modified()?; + let time_dest = metadata_dest?.modified()?; + + // If the destination file is more recent or equal to the source file, we do not copy + if time_dest >= time_src { + println!("Skip copy, equal file {}", dest_path.to_string_lossy()); + return Ok(()); + } + } + + // If the destination file does not exist or is older, we copy the source file to the destination + fs::copy(src_path, dest_path).map(|_| println!("Copying {}", dest_path.to_string_lossy()))?; + Ok(()) +} + pub fn copy_library(debug_mode: bool) -> Result<(), anyhow::Error> { let os = env::consts::OS; let arch = env::consts::ARCH; @@ -174,14 +199,14 @@ pub fn copy_library(debug_mode: bool) -> Result<(), anyhow::Error> { }; let source_folder = format!("{RUST_LIB_PROJECT_FOLDER}{source_folder}"); - println!("Copying {source_folder:?}"); + let source_file = adjust_canonicalization(fs::canonicalize(source_folder)?.join(file_name.clone())); let lib_folder = format!("{GODOT_PROJECT_FOLDER}lib/"); let destination_file = adjust_canonicalization(fs::canonicalize(lib_folder.as_str())?.join(file_name)); - fs::copy(source_file, destination_file)?; + copy_if_modified(source_file, destination_file)?; copy_ffmpeg_libraries(lib_folder)?; @@ -203,7 +228,7 @@ pub fn copy_ffmpeg_libraries(dest_folder: String) -> Result<(), anyhow::Error> { if file_name.ends_with(".dll") { let dest_path = format!("{dest_folder}{file_name}"); - fs::copy(entry.path(), dest_path)?; + copy_if_modified(entry.path(), dest_path)?; } } }