-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
48 lines (43 loc) · 1.76 KB
/
Copy pathbuild.rs
File metadata and controls
48 lines (43 loc) · 1.76 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
use std::{
env, fs,
path::{Path, PathBuf},
};
fn main() {
println!("cargo:rerun-if-changed=static");
let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let static_dir = manifest_dir.join("static");
let mut files = Vec::new();
collect_assets(&static_dir, &static_dir, &mut files);
files.sort_by(|left, right| left.0.cmp(&right.0));
let mut generated =
String::from("pub fn get(path: &str) -> Option<&'static [u8]> {\n match path {\n");
for (relative, absolute) in files {
generated.push_str(&format!(
" {relative:?} => Some(include_bytes!({absolute:?})),\n",
absolute = absolute.to_string_lossy(),
));
}
generated.push_str(" _ => None,\n }\n}\n");
let output = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("embedded_assets.rs");
fs::write(output, generated).expect("write embedded asset index");
}
fn collect_assets(root: &Path, directory: &Path, output: &mut Vec<(String, PathBuf)>) {
for entry in fs::read_dir(directory).expect("read static asset directory") {
let entry = entry.expect("read static asset entry");
let path = entry.path();
if path.is_dir() {
// Templates are embedded by `src/html.rs` and must not be publicly served.
if path != root.join("templates") {
collect_assets(root, &path, output);
}
} else if path.is_file() {
let relative = path
.strip_prefix(root)
.expect("asset is under static directory")
.to_str()
.expect("static asset paths must be UTF-8")
.replace('\\', "/");
output.push((relative, path));
}
}
}