-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
84 lines (64 loc) · 2.49 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
use {
std::{
env,
io,
path::Path,
fs
},
winresource::WindowsResource,
};
fn main() -> io::Result<()> {
// Apply icon on windows
if env::var_os("CARGO_CFG_WINDOWS").is_some() {
WindowsResource::new()
// This path can be absolute, or relative to your crate root.
.set_icon("assets/icon.ico")
.compile()?;
}
// Embed path to locale files
let locale_dir = Path::new("locales");
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("locale_data.rs");
let mut output = String::new();
// Generate the code for locale
output.push_str(r#"// ! GENERATED BY build.rs - EDIT IN build.rs IF NEEDED
fn get_locale_resources(locale: &str) -> Option<String> {
let mut m = HashMap::new();
// locales discovered by build.rs are put here
"#);
let mut amount = 0;
for entry in fs::read_dir(locale_dir).expect("Failed to read locales directory") {
let path = entry.expect("Failed to read file entry").path();
if let Some(lang ) = path.file_stem().and_then(|s| s.to_str()) {
if !path.metadata().unwrap().is_dir() {
amount += 1; // Increase amount, needs to be known for static locale list
let content = fs::read_to_string(&path).expect("Failed to read locale file");
output.push_str(&format!(" m.insert(\"{}\".to_owned(), r#\"{}\"#.to_owned());", lang, content));
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
output.push_str(r#"
return m.get(locale).cloned()
}
const LOCALES: [&str; "#);
output.push_str(&format!("{} ] = [", amount));
let mut first = true;
// Static list for all locales
for entry in fs::read_dir(locale_dir).expect("Failed to read locales directory") {
let path = entry.expect("Failed to read file entry").path();
if let Some(lang ) = path.file_stem().and_then(|s| s.to_str()) {
if !path.metadata().unwrap().is_dir() {
if first {
first = false;
output.push_str(&format!("\"{}\"", lang)); // First one doesn't have a comma
} else {
output.push_str(&format!(",\"{}\"", lang));
}
}
}
}
output.push_str("];");
fs::write(dest_path, output).expect("Failed to write generated locale data");
Ok(())
}