-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
60 lines (52 loc) · 1.97 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
use std::env;
use std::fs;
use std::path;
fn main() -> Result<(), failure::Error> {
use std::io::Write;
println!("cargo:rerun-if-changed=src/parser/tin.lalrpop");
lalrpop::process_root().unwrap();
let out_dir_str = env::var_os("OUT_DIR").unwrap();
let out_dir_path = path::Path::new(&out_dir_str);
let integration_test_ok_path = out_dir_path.join("integration_test_ok.rs");
let mut integration_test_ok_file = fs::File::create(integration_test_ok_path)?;
find_test_files("testdata/ok", |path, name| {
writeln!(
integration_test_ok_file,
"integration_test_ok!(ok_{}, {:?});",
name, path
)?;
Ok(())
})?;
let integration_test_err_path = out_dir_path.join("integration_test_err.rs");
let mut integration_test_err_file = fs::File::create(integration_test_err_path)?;
find_test_files("testdata/err", |path, name| {
let err_path = path.with_extension("err.txt");
writeln!(
integration_test_err_file,
"integration_test_err!(err_{}, {:?}, {:?});",
name, path, err_path
)?;
Ok(())
})?;
Ok(())
}
fn find_test_files<P, F>(base_path: P, mut callback: F) -> Result<(), failure::Error>
where
P: AsRef<path::Path>,
F: FnMut(&path::Path, &str) -> Result<(), failure::Error>,
{
let name_replace_pattern = regex::Regex::new(r"[^a-zA-Z0-9_]+").unwrap();
let base_path = base_path.as_ref();
println!("cargo:rerun-if-changed={}", base_path.to_string_lossy());
for dir_entry in walkdir::WalkDir::new(base_path) {
let dir_entry = dir_entry?;
let path = dir_entry.path();
println!("cargo:rerun-if-changed={}", path.to_string_lossy());
let name = path.strip_prefix(base_path)?.to_string_lossy();
if name.ends_with(".tn") {
let name = name_replace_pattern.replace_all(&name[..name.len() - ".tn".len()], "_");
callback(path, &*name)?;
}
}
Ok(())
}