forked from XAMPPRocky/tokei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
130 lines (106 loc) · 4.03 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
extern crate handlebars;
extern crate ignore;
extern crate serde_json;
use std::env;
use std::fs::File;
use std::path::Path;
use std::ffi::OsStr;
use handlebars::Handlebars;
use serde_json::Value;
fn main() {
let out_dir = env::var_os("OUT_DIR").expect("can't get OUT_DIR");
generate_languages(&out_dir);
generate_tests(&out_dir);
}
fn generate_languages(out_dir: &OsStr) {
let handlebars = {
let mut h = Handlebars::new();
h.register_escape_fn(handlebars::no_escape);
h
};
let mut json: Value = {
let json = File::open(&"languages.json").expect("Cant open json");
serde_json::from_reader(json).expect("Can't parse json")
};
for (_key, ref mut item) in json.get_mut("languages")
.unwrap()
.as_object_mut()
.unwrap()
.iter_mut()
{
macro_rules! sort_prop {
($prop:expr) => {{
if let Some(ref mut prop) = item.get_mut($prop) {
prop.as_array_mut()
.unwrap()
.sort_unstable_by(compare_json_str_len)
}
}}
}
sort_prop!("quotes");
sort_prop!("multi_line");
}
let output = 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(&output).expect("Can't create output");
if let Err(err) = handlebars.render_template_source_to_write(&mut source_template,
&json,
&mut output_file)
{
panic!("Failed to generate languages! ERROR: {:?}", err);
}
}
fn compare_json_str_len(a: &Value, b: &Value) -> ::std::cmp::Ordering {
let a = a.as_array().expect("a as array");
let b = b.as_array().expect("b as array");
let max_a_size = a.iter().map(|e| e.as_str().unwrap().len()).max().unwrap();
let max_b_size = b.iter().map(|e| e.as_str().unwrap().len()).max().unwrap();
max_b_size.cmp(&max_a_size)
}
fn generate_tests(out_dir: &OsStr) {
use std::io::Write;
use ignore::Walk;
let mut string = String::new();
let walker = Walk::new("./tests/data/").filter(|p| {
match p {
&Ok(ref p) => {
if let Ok(ref p) = p.metadata() {
p.is_file()
} else {
false
}
},
_ => false,
}
});
for path in walker {
let path = path.unwrap();
let path = path.path();
let name = path.file_stem().unwrap().to_str().unwrap().to_lowercase();
string.push_str(&format!(r#"
#[test]
fn {0}() {{
let mut languages = Languages::new();
languages.get_statistics(&["{1}"], Vec::new());
if languages.len() != 1 {{
panic!("wrong languages detected: expected just {0}, found {{:?}}",
languages.into_iter().collect::<Vec<_>>());
}}
let (name, language) = languages.into_iter().next().unwrap();
let mut contents = String::new();
File::open("{1}").unwrap().read_to_string(&mut contents).unwrap();
assert_eq!(get_digit!(LINES, contents), language.lines);
println!("{{}} LINES MATCH", name);
assert_eq!(get_digit!(CODE, contents), language.code);
println!("{{}} CODE MATCH", name);
assert_eq!(get_digit!(COMMENTS, contents), language.comments);
println!("{{}} COMMENTS MATCH", name);
assert_eq!(get_digit!(BLANKS, contents), language.blanks);
println!("{{}} BLANKS MATCH", name);
}}
"#, name, path.display()));
}
File::create(Path::new(&out_dir).join("tests.rs")).unwrap()
.write_all(string.as_bytes()).unwrap();
}