forked from XAMPPRocky/tokei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
81 lines (66 loc) · 2.2 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
#[cfg(feature = "io")] extern crate serde_codegen;
extern crate serde;
extern crate serde_json;
extern crate handlebars;
#[macro_use] extern crate errln;
use serde_json::Value;
use handlebars::Handlebars;
use std::fs::File;
use std::env;
use std::path::{Path, PathBuf};
use std::ffi::OsString;
fn main() {
let out_dir = env::var_os("OUT_DIR").expect("can't get OUT_DIR");
expand(out_dir);
}
#[cfg(feature = "io")]
fn expand(out_dir: OsString) {
use std::thread;
let hbs = render_handlebars(&out_dir);
let builder = thread::Builder::new()
.name(String::from("Build Thread"))
.stack_size(16388608);
let handle = builder.spawn(move || {
let paths = [
(
Path::new("src/language/serde_types.in.rs"),
Path::new(&out_dir).join("language_serde_types.rs"),
),
(
&*hbs,
hbs.to_owned(),
),
(
Path::new("src/serde_types.in.rs"),
Path::new(&out_dir).join("stats_serde_types.in.rs"),
),
];
for &(ref src, ref dst) in &paths {
serde_codegen::expand(src, dst)
.expect(&format!("Can't serde {:?}", src));
}
});
let _ = handle.unwrap().join();
}
#[cfg(not(feature = "io"))]
fn expand(out_dir: OsString) {
render_handlebars(&out_dir);
}
fn render_handlebars(out_dir: &OsString) -> PathBuf {
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
let data: Value = serde_json::from_reader(
File::open(&"languages.json").expect("Can't open JSON")
).expect("Can't parse JSON");
let out = Path::new(&out_dir).join("language_type.rs");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")
.expect("Can't find Template");
let mut output_file = File::create(&out).expect("Can't create output");
if let Err(err) = handlebars.template_renderw2(&mut source_template,
&data,
&mut output_file)
{
panic!("Failed to generate languages! ERROR: {:?}", err);
}
out
}