Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: copy rust lib only when it changes #50

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading