Skip to content

Instantly share code, notes, and snippets.

@SonicZentropy
Last active August 19, 2021 02:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SonicZentropy/7cc5347639d8147777589687e2747c10 to your computer and use it in GitHub Desktop.
Save SonicZentropy/7cc5347639d8147777589687e2747c10 to your computer and use it in GitHub Desktop.
Rust build.rs file for auto copying game dev assets to target build dir
use std::{env, fs, path::{Path, PathBuf}};
fn get_output_path() -> PathBuf {
let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
let build_type = env::var("PROFILE").unwrap();
let path = Path::new(&manifest_dir_string)
.join("target")
.join(build_type);
return PathBuf::from(path);
}
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
fn main() {
let src = Path::join(&env::current_dir().unwrap(), "assets\\");
let dest = Path::join(Path::new(&get_output_path()), "assets\\");
//Since we only run if something's changed, clear out the old to catch deleted files.
fs::remove_dir_all(&dest);
copy_dir_all(src, dest);
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=assets\\");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment