-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
52 lines (44 loc) · 1.62 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
fn get_build_profile_name() -> String {
// The profile name is always the 3rd last part of the path (with 1 based indexing).
// e.g. /code/core/target/cli/build/my-build-info-9f91ba6f99d7a061/out
env::var("OUT_DIR")
.unwrap()
.split(std::path::MAIN_SEPARATOR)
.nth_back(3)
.unwrap_or("unknown")
.to_string()
}
fn main() {
// Get the build profile (either "debug" or "release")
let profile = get_build_profile_name();
let target_dir = PathBuf::from("target").join(&profile); // Points to "target/debug" or "target/release"
// Define the source and destination paths
let source_dir = PathBuf::from("resources");
let dest_dir = target_dir.join("resources");
// Copy the entire directory recursively
if let Err(e) = fs::create_dir_all(&dest_dir) {
panic!("Failed to create destination directory: {}", e);
}
copy_recursively(&source_dir, &dest_dir).unwrap();
// Inform Cargo to rerun the build script if anything in the resources folder changes
// println!("cargo:rerun-if-changed=resources");
}
// Helper function to recursively copy files
fn copy_recursively(src: &PathBuf, dst: &Path) -> std::io::Result<()> {
for entry in fs::read_dir(src)? {
let entry = entry?;
let path = entry.path();
let dest_path = dst.join(entry.file_name());
if path.is_dir() {
fs::create_dir_all(&dest_path)?;
copy_recursively(&path, &dest_path)?;
} else {
fs::copy(&path, &dest_path)?;
}
}
Ok(())
}