-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
64 lines (54 loc) · 2.17 KB
/
build.rs
File metadata and controls
64 lines (54 loc) · 2.17 KB
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
63
64
use std::env;
use std::path::Path;
use std::process::Command;
use std::{fs, io};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
// Tell Cargo that if anything in the frontend/ directory changes, it should
// rerun this build script.
println!("cargo::rerun-if-changed=frontend");
// Copy the entire frontend directory into the OUT_DIR. We need to do this
// because building the frontend code requires installing node_modules and
// generating files in dist/, and Cargo will refuse to build if build.rs
// "dirties" the source directory.
copy_dir_all("frontend", &format!("{}/frontend", &out_dir))
.expect("copying frontend code to OUT_DIR failed");
// Run npm install for frontend/ (in the OUT_DIR)
let status = Command::new("npm")
.args(&["install"])
.current_dir(&format!("{}/frontend", &out_dir))
.status()
.expect("failed to execute 'npm install'");
if !status.success() {
panic!("'npm install' failed with exit code: {}", status);
}
// Run npm run build for frontend/ (in the OUT_DIR)
let status = Command::new("npm")
.args(&["run", "build"])
.current_dir(&format!("{}/frontend", &out_dir))
.status()
.expect("failed to execute 'npm run build'");
if !status.success() {
panic!("'npm run build' failed with exit code: {}", status);
}
}
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let file_type = entry.file_type()?;
if file_type.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else if file_type.is_symlink() {
let resolved_target = fs::canonicalize(entry.path())?;
if resolved_target.is_dir() {
copy_dir_all(resolved_target, dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(resolved_target, dst.as_ref().join(entry.file_name()))?;
}
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}