forked from amethyst/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
56 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
//! Reads the `templates/` directory and includes all versions' templates as
//! part of the binary to reduce installation footprint
use std::{
env,
fs::{read_dir, File},
io::Write,
path::PathBuf,
};
use ron::de::from_reader;
fn path(env: &str, s: &str) -> PathBuf {
PathBuf::from(env::var(env).unwrap()).join(s)
}
fn read_template_index<P: Into<PathBuf>>(p: P) -> Vec<String> {
let mut path = PathBuf::new();
path.push(p.into());
path.push("index.ron");
from_reader(File::open(&path).expect("Failed to open index.ron"))
.expect("Failed to parse template index")
}
fn main() {
let f = path("CARGO_MANIFEST_DIR", "templates");
let indices = read_dir(&f).unwrap().map(Result::unwrap).map(|v| {
(
v.file_name().into_string().unwrap(),
read_template_index(v.path()),
)
});
let mut source_code = String::from(
"use std::collections::HashMap;
pub fn template_files() -> HashMap<&'static str, Vec<(&'static str, &'static str)>> {
let mut map = HashMap::new();
",
);
for (version, index) in indices {
source_code.push_str(&format!(" map.insert({:?}, ", version));
source_code.push_str(&index.iter().fold("vec![".to_owned(), |s, file| {
format!(
"{}({:?}, include_str!(concat!(env!(\"CARGO_MANIFEST_DIR\"),\
concat!(\"/templates/\", concat!(concat!({:?}, \"/\"), {:?}))))), ",
s, file, version, file,
)
}));
source_code.push_str("]);\n")
}
source_code += " map\n}\n";
File::create(&path("OUT_DIR", "_template_files.rs"))
.expect("Failed to embed template files")
.write_all(source_code.as_bytes())
.expect("Failed to write to destination");
}