-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
62 lines (50 loc) · 1.76 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
54
55
56
57
58
59
60
61
62
extern crate walkdir;
use std::env;
use std::fs::{self, DirBuilder};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
// locate executable path even if the project is in workspace
let executable_path = locate_target_dir_from_output_dir(&out_dir)
.expect("failed to find target dir")
.join(env::var("PROFILE").unwrap());
copy(
&manifest_dir.join("assets"),
&executable_path.join("assets"),
);
}
fn locate_target_dir_from_output_dir(mut target_dir_search: &Path) -> Option<&Path> {
loop {
// if path ends with "target", we assume this is the correct dir
if target_dir_search.ends_with("target") {
return Some(target_dir_search);
}
// otherwise, keep going up in tree until we find "target" dir
target_dir_search = match target_dir_search.parent() {
Some(path) => path,
None => break,
}
}
None
}
fn copy(from: &Path, to: &Path) {
let from_path: PathBuf = from.into();
let to_path: PathBuf = to.into();
for entry in WalkDir::new(from_path.clone()) {
let entry = entry.unwrap();
if let Ok(rel_path) = entry.path().strip_prefix(&from_path) {
let target_path = to_path.join(rel_path);
if entry.file_type().is_dir() {
DirBuilder::new()
.recursive(true)
.create(target_path)
.expect("failed to create target dir");
} else {
fs::copy(entry.path(), &target_path)
.expect("failed to copy");
}
}
}
}