-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
69 lines (52 loc) · 1.5 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
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
fn main() {
compile_static_resources();
}
fn compile_static_resources() {
let mut resource_path = env::current_dir().unwrap();
resource_path.push("src/resources");
let paths = fs::read_dir(resource_path).unwrap();
let head = "
// Generated by build.rs
use std::collections::HashMap;
pub struct Resource {
pub r: HashMap<&'static str, &'static str>
}
impl Resource {
pub fn new() -> Resource {
let mut res: HashMap<&'static str, &'static str> = HashMap::new();
"
.to_string();
let tail = "
Resource {
r: res
}
}
}"
.to_string();
let mut body = String::new();
for p in paths {
let path = p.unwrap();
if path.file_type().unwrap().is_dir() {
continue;
}
let mut file = File::open(path.path().as_os_str()).unwrap();
let mut buff = String::new();
file.read_to_string(&mut buff).unwrap();
let f_body = buff.replace("\"", "\\\"");
let f_name = path.file_name().into_string().unwrap();
body.push_str(&format!(
" res.insert(\"/resource/{}\", \"{}\");\n",
f_name, f_body
));
}
let mut resource_buff = String::new();
resource_buff.push_str(&head);
resource_buff.push_str(&body);
resource_buff.push_str(&tail);
let mut file = File::create("src/static_r.rs").unwrap();
file.write_all(resource_buff.as_bytes()).unwrap();
}