Last active
August 19, 2021 02:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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