-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
build.rs
134 lines (119 loc) · 3.85 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Build script.
//!
//! This script collects the assets for serving the Routinator UI and creates
//! a module for them in `$OUT_DIR/ui_assets.rs`.
//!
//! If built without the ui feature, does nothing.
use std::{env, fs, io, process};
use std::path::{PathBuf, Path};
const UI_DIR: &str = "contrib/ui";
const RS_FILE: &str = "ui_assets.rs";
const TYPES: &[(&str, &str)] = &[
("css", "text/css"),
("html", "text/html"),
("js", "text/javascript"),
("svg", "image/svg+xml")
];
struct Asset {
path: PathBuf,
media_type: &'static str,
content: Vec<u8>,
}
#[derive(Default)]
struct Assets(Vec<Asset>);
impl Assets {
fn load_dir(&mut self, path: PathBuf) -> Result<(), String> {
let dir = fs::read_dir(&path).map_err(|err| {
format!("Failed to open directory {}: {}", path.display(), err)
})?;
for entry in dir {
let entry = entry.map_err(|err| {
format!("Failed to read directory {}: {}", path.display(), err)
})?;
let path = entry.path();
if path.is_dir() {
self.load_dir(path)?;
}
else {
let path_ext = match path.extension().and_then(|s| s.to_str()) {
Some(ext) => ext,
None => continue,
};
for (type_ext, media_type) in TYPES {
if path_ext == *type_ext {
self.0.push(Asset {
path: path.strip_prefix(UI_DIR).map_err(|_| {
format!("Asset path {} not under {}",
path.display(), UI_DIR
)
})?.into(),
media_type,
content: fs::read(&path).map_err(|err| {
format!(
"Failed to read UI asset file {}: {}.",
path.display(), err
)
})?
})
}
}
}
}
Ok(())
}
fn write_mod(self, dest: &mut impl io::Write) -> Result<(), io::Error> {
dest.write_all(
r#"
pub struct Asset {
pub path: &'static str,
pub media_type: &'static str,
pub content: &'static [u8],
}
pub static ASSETS: &[Asset] = &[
"#.as_ref()
)?;
for item in self.0 {
writeln!(dest,
"
Asset {{
path: r#\"{}\"#,
media_type: \"{}\",
content: &{:?},
}},
",
item.path.display(),
item.media_type,
item.content.as_slice(),
)?;
}
writeln!(dest, "];")
}
}
fn main() {
if env::var_os("CARGO_FEATURE_UI").is_none() {
return
}
let out_dir = env::var_os("OUT_DIR").unwrap_or_default();
let target_path = Path::new(&out_dir).join(RS_FILE);
let mut target = match fs::File::create(&target_path) {
Ok(target) => io::BufWriter::new(target),
Err(err) => {
eprintln!("Failed to open assets module file {}: {}",
target_path.display(), err
);
process::exit(1);
}
};
let mut assets = Assets::default();
if let Err(err) = assets.load_dir(UI_DIR.into()) {
eprintln!("{}", err);
process::exit(1);
}
if let Err(err) = assets.write_mod(&mut target) {
eprintln!("Failed to write to assets module file {}: {}",
target_path.display(), err
);
process::exit(1)
}
println!("cargo:rerun-if-changed={}", UI_DIR);
}