-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
73 lines (63 loc) · 1.83 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
use std::{
env,
fs::{read_dir, read_to_string, File},
io::Write,
path::PathBuf,
};
use regex::Regex;
const COMMAND_REGEX: &str = r"pub fn (.*)\(app: &mut Application\) -> Result<\(\)>";
fn main() {
generate_handler();
}
fn generate_handler() {
let out_dir = env::var("OUT_DIR").unwrap();
let out_file_pathbuf = PathBuf::new().join(out_dir).join("handle_map");
let mut out_file = File::create(out_file_pathbuf).unwrap();
out_file
.write(
r"{
let mut handles: HashMap<&'static str, fn(&mut Application) -> Result<()>> = HashMap::new();
"
.as_bytes(),
)
.unwrap();
let expression = Regex::new(COMMAND_REGEX).expect("Failed to compile command matching regex");
let readdir = read_dir("./src/application/handler/").unwrap();
for entry in readdir {
if let Ok(entry) = entry {
let path = entry.path();
let module_name = entry
.file_name()
.into_string()
.unwrap()
.split('.')
.next()
.unwrap()
.to_owned();
let content = read_to_string(path).unwrap();
for captures in expression.captures_iter(&content) {
let function_name = captures.get(1).unwrap().as_str();
write(&mut out_file, &module_name, function_name);
}
}
}
out_file
.write(
r"
handles
}"
.as_bytes(),
)
.unwrap();
}
fn write(output: &mut File, module_name: &str, function_name: &str) {
output
.write(
format!(
" handles.insert(\"{}::{}\", {}::{});\n",
module_name, function_name, module_name, function_name
)
.as_bytes(),
)
.unwrap();
}