2016-11-27 15:54:35 +00:00
|
|
|
extern crate handlebars;
|
2017-08-10 23:05:17 +00:00
|
|
|
extern crate ignore;
|
|
|
|
extern crate serde_json;
|
2016-11-27 15:54:35 +00:00
|
|
|
|
|
|
|
use std::env;
|
2017-02-15 19:42:08 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
|
|
|
use handlebars::Handlebars;
|
|
|
|
use serde_json::Value;
|
2016-11-27 15:54:35 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let out_dir = env::var_os("OUT_DIR").expect("can't get OUT_DIR");
|
2017-02-15 19:42:08 +00:00
|
|
|
generate_languages(&out_dir);
|
|
|
|
generate_tests(&out_dir);
|
2016-11-27 15:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:42:08 +00:00
|
|
|
fn generate_languages(out_dir: &OsStr) {
|
2017-08-10 23:05:17 +00:00
|
|
|
let handlebars = {
|
|
|
|
let mut h = Handlebars::new();
|
|
|
|
h.register_escape_fn(handlebars::no_escape);
|
|
|
|
h
|
|
|
|
};
|
|
|
|
|
|
|
|
let json: Value = {
|
|
|
|
let json = File::open(&"languages.json").expect("Cant open json");
|
|
|
|
serde_json::from_reader(json).expect("Can't parse json")
|
|
|
|
};
|
|
|
|
|
|
|
|
let output = Path::new(&out_dir).join("language_type.rs");
|
2016-11-27 15:54:35 +00:00
|
|
|
let mut source_template = File::open(&"src/language/language_type.hbs.rs")
|
|
|
|
.expect("Can't find Template");
|
2017-08-10 23:05:17 +00:00
|
|
|
let mut output_file = File::create(&output).expect("Can't create output");
|
2016-11-27 15:54:35 +00:00
|
|
|
|
|
|
|
if let Err(err) = handlebars.template_renderw2(&mut source_template,
|
2017-08-10 23:05:17 +00:00
|
|
|
&json,
|
2016-11-27 15:54:35 +00:00
|
|
|
&mut output_file)
|
|
|
|
{
|
|
|
|
panic!("Failed to generate languages! ERROR: {:?}", err);
|
|
|
|
}
|
2017-02-15 19:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2017-06-05 14:28:52 +00:00
|
|
|
let name = path.file_stem().unwrap().to_str().unwrap().to_lowercase();
|
2017-02-15 19:42:08 +00:00
|
|
|
|
|
|
|
string.push_str(&format!(r#"
|
|
|
|
#[test]
|
|
|
|
fn {0}() {{
|
|
|
|
let mut languages = Languages::new();
|
2018-01-13 17:44:57 +00:00
|
|
|
languages.get_statistics(&["{1}"], Vec::new());
|
2017-06-05 14:28:52 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2017-02-15 19:42:08 +00:00
|
|
|
let mut contents = String::new();
|
|
|
|
File::open("{1}").unwrap().read_to_string(&mut contents).unwrap();
|
|
|
|
|
2017-06-05 14:28:52 +00:00
|
|
|
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);
|
2017-02-15 19:42:08 +00:00
|
|
|
}}
|
|
|
|
"#, name, path.display()));
|
|
|
|
}
|
|
|
|
|
|
|
|
File::create(Path::new(&out_dir).join("tests.rs")).unwrap()
|
|
|
|
.write_all(string.as_bytes()).unwrap();
|
2016-11-27 15:54:35 +00:00
|
|
|
}
|