-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
180 lines (152 loc) · 5.72 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::collections::HashSet;
use std::io::Write;
const MIGRATIONS_DIR: &str = "migrations";
const DEFAULT_LANG_FILE: &str = "res/lang/en.ftl";
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = std::env::var("OUT_DIR")?;
{
println!("cargo:rerun-if-changed={}", MIGRATIONS_DIR);
let mut out_file =
std::fs::File::create(std::path::Path::new(&out_dir).join("migrations.rs"))?;
writeln!(out_file, "&[")?;
let paths: Result<Vec<_>, _> = std::fs::read_dir(MIGRATIONS_DIR)?.collect();
let mut paths = paths?;
paths.sort_by_cached_key(|entry| entry.file_name());
for entry in paths {
let filename = entry.file_name();
let tag = filename.to_str().unwrap();
let path = entry.path().canonicalize()?;
let path = path.to_str().unwrap();
writeln!(
out_file,
r##"StaticMigration {{ tag: r#"{}"#, up: include_str!(r#"{1}{2}up.sql"#), down: include_str!(r#"{1}{2}down.sql"#) }},"##,
tag,
path,
std::path::MAIN_SEPARATOR
)?;
}
write!(out_file, "]")?;
}
{
println!("cargo:rerun-if-changed={}", DEFAULT_LANG_FILE);
let mut out_file =
std::fs::File::create(std::path::Path::new(&out_dir).join("lang_keys.rs"))?;
let content = std::fs::read_to_string(DEFAULT_LANG_FILE)?;
let ast = match fluent_syntax::parser::parse_runtime(content.as_ref()) {
Ok(ast) => ast,
Err((_, errors)) => {
panic!("Failed to load default lang file: {:?}", errors);
}
};
for entry in ast.body {
if let fluent_syntax::ast::Entry::Message(msg) = entry {
let id = msg.id.name;
let mut args: Vec<&str> = Vec::new();
println!("finding arguments for {:?}", msg.value);
if let Some(value) = msg.value {
discover_args_for_pattern(&mut args, &value);
}
let args: Vec<_> = {
let mut set = HashSet::new();
args.into_iter().filter(|key| set.insert(*key)).collect()
};
if args.is_empty() {
writeln!(
out_file,
"pub const fn {0}() -> PlainLangKey {{ PlainLangKey(\"{0}\") }}",
id
)?;
} else {
write!(out_file, "pub fn {}<'a>(", id)?;
{
let mut first = true;
for arg in &args {
if !first {
write!(out_file, ", ")?;
}
first = false;
write!(out_file, "{}: impl Into<fluent::FluentValue<'a>>", arg)?;
}
}
writeln!(out_file, ") -> LangKeyWithArgs<'a> {{")?;
write!(
out_file,
"LangKeyWithArgs(\"{}\", fluent::fluent_args![",
id
)?;
{
let mut first = true;
for arg in args {
if !first {
write!(out_file, ", ")?;
}
first = false;
write!(out_file, "\"{0}\" => {0}", arg)?;
}
}
writeln!(out_file, "])")?;
writeln!(out_file, "}}")?;
}
}
}
}
Ok(())
}
fn discover_args_for_pattern<'a>(
target: &mut Vec<&'a str>,
pattern: &fluent_syntax::ast::Pattern<&'a str>,
) {
for elem in &pattern.elements {
if let fluent_syntax::ast::PatternElement::Placeable { expression } = elem {
discover_args_for_expression(target, expression);
}
}
}
fn discover_args_for_expression<'a>(
target: &mut Vec<&'a str>,
expr: &fluent_syntax::ast::Expression<&'a str>,
) {
match expr {
fluent_syntax::ast::Expression::Select { selector, variants } => {
discover_args_for_inline_expression(target, selector);
for variant in variants {
discover_args_for_pattern(target, &variant.value);
}
}
fluent_syntax::ast::Expression::Inline(expr) => {
discover_args_for_inline_expression(target, expr)
}
}
}
fn discover_args_for_inline_expression<'a>(
target: &mut Vec<&'a str>,
expr: &fluent_syntax::ast::InlineExpression<&'a str>,
) {
use fluent_syntax::ast::InlineExpression;
match expr {
InlineExpression::StringLiteral { .. }
| InlineExpression::NumberLiteral { .. }
| InlineExpression::MessageReference { .. }
| InlineExpression::TermReference {
arguments: None, ..
} => {}
InlineExpression::FunctionReference { arguments, .. }
| InlineExpression::TermReference {
arguments: Some(arguments),
..
} => {
for arg in &arguments.positional {
discover_args_for_inline_expression(target, arg);
}
for arg in &arguments.named {
discover_args_for_inline_expression(target, &arg.value);
}
}
InlineExpression::Placeable { expression } => {
discover_args_for_expression(target, expression);
}
InlineExpression::VariableReference { id } => {
target.push(id.name);
}
}
}