Skip to content

Commit

Permalink
chore: copy rust lib only when it changes (#50)
Browse files Browse the repository at this point in the history
* chore: copy rust lib only when it changes

* english comments
  • Loading branch information
kuruk-mm authored Sep 25, 2023
1 parent 9cd4af8 commit 1475eac
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions rust/xtask/src/install_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ pub fn get_godot_executable_path() -> Option<String> {
Some(os_url)
}

fn copy_if_modified<P: AsRef<Path>, Q: AsRef<Path>>(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;
Expand All @@ -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)?;

Expand All @@ -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)?;
}
}
}
Expand Down

0 comments on commit 1475eac

Please sign in to comment.