-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
95 lines (84 loc) · 3.33 KB
/
Copy pathbuild.rs
File metadata and controls
95 lines (84 loc) · 3.33 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
fn main() {
emit_git_build_metadata();
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let dist_dir = manifest_dir.join("webui").join("dist");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
let output = out_dir.join("embedded_webui.rs");
println!("cargo:rerun-if-changed={}", dist_dir.display());
let mut files = Vec::new();
if dist_dir.exists() {
collect_files(&dist_dir, &dist_dir, &mut files);
files.sort_by(|a, b| a.0.cmp(&b.0));
}
let mut source = String::new();
if files.is_empty() {
source.push_str("pub fn has_assets() -> bool { false }\n");
source.push_str("pub fn get_asset(_path: &str) -> Option<&'static [u8]> { None }\n");
} else {
source.push_str("pub fn has_assets() -> bool { true }\n");
source.push_str("pub fn get_asset(path: &str) -> Option<&'static [u8]> {\n");
source.push_str(" match path {\n");
for (relative, absolute) in files {
source.push_str(&format!(
" \"{}\" => Some(include_bytes!(r#\"{}\"#)),\n",
relative, absolute
));
}
source.push_str(" _ => None,\n");
source.push_str(" }\n");
source.push_str("}\n");
}
fs::write(&output, source).expect("failed to write embedded_webui.rs");
}
fn emit_git_build_metadata() {
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_TAG");
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_COMMIT");
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_DIRTY");
let git_tag = env::var("PD_BUILD_GIT_TAG").unwrap_or_else(|_| {
run_git(["describe", "--tags", "--exact-match"]).unwrap_or_else(|| "untagged".to_string())
});
let git_commit = env::var("PD_BUILD_GIT_COMMIT").unwrap_or_else(|_| {
run_git(["rev-parse", "--short=12", "HEAD"]).unwrap_or_else(|| "unknown".to_string())
});
let git_dirty = env::var("PD_BUILD_GIT_DIRTY").unwrap_or_else(|_| {
match run_git(["status", "--porcelain", "--untracked-files=no"]) {
Some(output) if !output.trim().is_empty() => "true".to_string(),
_ => "false".to_string(),
}
});
println!("cargo:rustc-env=PD_BUILD_GIT_TAG={git_tag}");
println!("cargo:rustc-env=PD_BUILD_GIT_COMMIT={git_commit}");
println!("cargo:rustc-env=PD_BUILD_GIT_DIRTY={git_dirty}");
}
fn run_git<const N: usize>(args: [&str; N]) -> Option<String> {
let output = Command::new("git").args(args).output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8(output.stdout)
.ok()
.map(|value| value.trim().to_string())
}
fn collect_files(base: &Path, dir: &Path, files: &mut Vec<(String, String)>) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_files(base, &path, files);
continue;
}
let Ok(relative) = path.strip_prefix(base) else {
continue;
};
let rel = relative.to_string_lossy().replace('\\', "/");
let abs = path.to_string_lossy().replace('\\', "/");
files.push((rel, abs));
}
}