-
-
Notifications
You must be signed in to change notification settings - Fork 539
/
build.rs
166 lines (136 loc) · 4.92 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
extern crate ignore;
extern crate json5;
extern crate serde_json;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::{cmp, env, error};
use ignore::Walk;
use serde_json::Value;
fn main() -> Result<(), Box<dyn error::Error>> {
let out_dir = env::var_os("OUT_DIR").expect("No OUT_DIR variable.");
generate_languages(&out_dir)?;
generate_tests(&out_dir)?;
Ok(())
}
fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
let mut tera = tera::Tera::default();
let json_string: String = fs::read_to_string("languages.json")?.parse()?;
let mut json: Value = json5::from_str(&json_string)?;
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!("verbatim_quotes");
sort_prop!("multi_line");
}
let output_path = Path::new(&out_dir).join("language_type.rs");
let rust_code = tera.render_str(
&std::fs::read_to_string("src/language/language_type.tera.rs")?,
&tera::Context::from_value(json)?,
)?;
std::fs::write(output_path, rust_code)?;
Ok(())
}
fn compare_json_str_len(a: &Value, b: &Value) -> 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) -> Result<(), Box<dyn error::Error>> {
// Length of string literal below by number of languages
const INITIAL_BUFFER_SIZE: usize = 989 * 130;
let mut string = String::with_capacity(INITIAL_BUFFER_SIZE);
generate_tests_batch("./tests/data", None, &mut string)?;
generate_tests_batch("./tests/embedding", Some("embedding"), &mut string)?;
Ok(fs::write(Path::new(&out_dir).join("tests.rs"), string)?)
}
fn generate_tests_batch(
src_dir: &str,
test_module: Option<&str>,
string: &mut String,
) -> Result<(), Box<dyn error::Error>> {
let walker = Walk::new(src_dir).filter(|p| match p {
Ok(ref p) => {
if let Ok(ref p) = p.metadata() {
p.is_file()
} else {
false
}
}
_ => false,
});
if let Some(test_module) = test_module {
string.push_str(&format!(
r####"
#[cfg(test)]
mod {0} {{
use super::*;
"####,
test_module
));
}
for path in walker {
let path = path?;
let path = path.path();
let root = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let name = path.file_stem().unwrap().to_str().unwrap().to_lowercase();
if name == "jupyter" {
continue;
}
string.push_str(&format!(
r####"
#[test]
fn {0}() {{
const _: &str = include_str!(r###"{2}"###);
let mut languages = Languages::new();
languages.get_statistics(&["{1}"], &[], &Config::default());
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 language = language.summarise();
let contents = fs::read_to_string("{1}").unwrap();
println!("{{}} {1}", name);
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);
let report = language.reports.pop().unwrap();
let stats = report.stats.summarise();
assert_eq!(language.lines(), stats.lines());
assert_eq!(language.code, stats.code);
assert_eq!(language.comments, stats.comments);
assert_eq!(language.blanks, stats.blanks);
}}
"####,
name,
path.to_string_lossy().replace('\\', "/"),
std::fs::canonicalize(root.join(path)).unwrap().display(),
));
}
if test_module.is_some() {
string.push_str("\n}");
}
Ok(())
}