-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
53 lines (46 loc) · 1.65 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
53
use std::{env, fs, io, path::{Path, PathBuf}};
fn main() {
let copy_folder_list = vec!["optolith-data", "assets"];
let manifest_dir = get_manifest_path();
let out_dir = get_output_path();
for src in copy_folder_list {
let source_dir = manifest_dir.join(src);
let target_dir = out_dir.join(src);
let res = copy_dir(&source_dir, &target_dir);
if res.is_err() {
println!("cargo:warning=copy failed {} -> {}", source_dir.to_str().unwrap(), target_dir.to_str().unwrap());
}
}
}
fn get_output_path() -> PathBuf {
//<root or manifest path>/target/<profile>/
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 get_manifest_path() -> PathBuf {
let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
return PathBuf::from(manifest_dir_string);
}
fn copy_dir(src: &Path, dest: &Path) -> io::Result<()> {
if !dest.exists() {
fs::create_dir(&dest)?;
}
for entry in src.read_dir()? {
let entry = entry?;
let path = entry.path();
let file_name = entry.file_name();
let file_type = entry.file_type()?;
if file_type.is_file() {
fs::copy(path, dest.join(file_name))?;
} else if file_type.is_dir() {
let dest_dir = dest.join(file_name);
fs::create_dir(&dest_dir)?;
copy_dir(&path, &dest_dir)?;
} else {
unreachable!("unknown file type");
}
}
Ok(())
}