-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.rs
135 lines (114 loc) · 5.04 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
use glob::glob;
use std::collections::HashMap;
use tokay; // This is Tokay v0.4
static PATTERN: &str = "src/**/*.rs";
fn main() {
let mut res: HashMap<String, String> = HashMap::new();
// disable any debug inside of this process
std::env::set_var("TOKAY_DEBUG", "0");
std::env::set_var("TOKAY_PARSER_DEBUG", "0");
let mut compiler = tokay::compiler::Compiler::new();
let program = compiler
// todo: use Compiler::compile_str() later here...
.compile(tokay::reader::Reader::new(Box::new(std::io::Cursor::new(
include_str!("build.tok"),
))))
.expect("Tokay compile error");
// This is some little, naive attempt to run the build.tok script on all .rs-source files
// and obtain the module path from the src/-file- and folder-structure. It surely can be done
// better, but suffices the current requirements.
for entry in glob(PATTERN).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
match program.run_from_string(
std::fs::read_to_string(&path)
.expect(&format!("Unable to read {}", path.display())),
) {
Ok(None) => {}
Ok(Some(matches)) => {
println!("cargo:rerun-if-changed={}", path.display());
//let path = path.into_iter().map(|part| part.to_str().unwrap().to_string()).collect::<Vec<String>>();
// Generate result entries from all matches of build.tok
let matches = matches.to_list();
for func in matches {
let func = func.borrow().to_dict();
let kind = func["kind"].borrow().to_string();
let name = func["name"].borrow().to_string();
// Generate module prefix from path...
let module = path
.iter()
.enumerate()
.filter_map(|(i, part)| {
let mut part = part.to_str().unwrap();
//println!("part = {:?}", part);
if i == 0 {
return Some("crate".to_string());
} else if part.ends_with(".rs")
// fixme: can the be done better?
{
// cut away the ".rs" here...
part = &part[..part.len() - 3];
if part == "mod" {
return None;
}
// create "type::Type" here in case it's a method
if kind == "method" {
return Some(format!(
"{}::{}",
part,
func["impl"].borrow().to_string()
));
}
}
Some(part.to_string())
})
.collect::<Vec<String>>()
.join("::");
// Generate full qualified function name
res.insert(
name.clone(),
format!("{}::tokay_{}_{}", module, kind, name.to_lowercase()),
);
}
}
Err(_) => panic!("Error during execution"),
}
}
Err(e) => println!("{:?}", e),
}
}
// Sort keys
let mut keys: Vec<String> = res.keys().map(|key| key.clone()).collect();
keys.sort();
// Generate source
let f = "src/_builtins.rs";
let s = r#"/*! Tokay builtin registry
THIS MODULE IS AUTOMATICALLY GENERATED BY BUILD.RS;
DON'T CHANGE THIS FILE MANUALLY, IT WILL GO AWAY!!!
*/
use crate::builtin::Builtin;
pub static BUILTINS: [Builtin; ##count] = [
##defs];
"#
.replace(
"##defs",
&keys
.into_iter()
.map(|key| {
format!(
" Builtin {{\n name: \"{}\",\n func: {},\n }},\n",
key, res[&key]
)
})
.collect::<Vec<String>>()
.concat(),
)
.replace("##count", &res.len().to_string());
// Exit when file exists and didn't change
if let Ok(c) = std::fs::read_to_string(f) {
if c == s {
std::process::exit(0);
}
}
std::fs::write(f, s).expect(&format!("Unable to write '{}'", f));
}