-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
117 lines (107 loc) · 2.51 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
use std::{
fs::File,
io::Read,
path::{Path, PathBuf},
};
fn main() {
if cfg!(feature = "regenerate_types") {
regenerate_types()
}
}
fn regenerate_types() {
let data_path = Path::new("../data/test/schema/");
let gen_path = Path::new("src/format/gen/");
let _ = std::fs::remove_dir_all(gen_path);
std::fs::create_dir_all(gen_path).unwrap();
gen_types(
data_path.join("entry.json").as_path(),
gen_path.join("entry.rs").as_path(),
Some("EntryJson"),
);
gen_types(
data_path.join("util.json").as_path(),
gen_path.join("util.rs").as_path(),
None,
);
gen_types(
data_path.join("trapshazards.json").as_path(),
gen_path.join("trapshazards.rs").as_path(),
None,
);
gen_types(
data_path.join("bestiary/bestiary.json").as_path(),
gen_path.join("bestiary.rs").as_path(),
None,
);
gen_types(
data_path.join("spells/spells.json").as_path(),
gen_path.join("spells.rs").as_path(),
None,
);
gen_types(
data_path.join("objects.json").as_path(),
gen_path.join("objects.rs").as_path(),
None,
);
gen_types(
data_path.join("items.json").as_path(),
gen_path.join("items.rs").as_path(),
None,
);
gen_mod(gen_path);
rustfmt(gen_path);
replace(
gen_path.join("entry.rs").as_path(),
"EntryJson = serde_json::Value",
"EntryJson = Entry",
);
}
fn replace(file: &Path, from: &str, to: &str) {
let mut f = File::open(file).unwrap();
let mut data = String::new();
f.read_to_string(&mut data).unwrap();
drop(f);
let new_data = data.replace(from, to);
std::fs::write(file, new_data.as_bytes()).unwrap();
}
fn gen_types(input_file: &Path, output_file: &Path, type_name: Option<&str>) {
let entry = schemafy_lib::Generator::builder()
.with_root_name(type_name.map(str::to_owned))
.with_input_file(input_file.to_str().unwrap())
.build()
.generate();
std::fs::write(
output_file,
format!("use serde::{{Deserialize, Serialize}};\nuse super::*;\n{entry}"),
)
.unwrap();
}
fn gen_mod(gen_path: &Path) {
let m = std::fs::read_dir(gen_path)
.unwrap()
.into_iter()
.filter_map(Result::ok)
.map(|t| t.path())
.map(|path: PathBuf| {
format!(
"pub mod {0};\npub use {0}::*;",
path.with_extension("")
.file_name()
.unwrap()
.to_string_lossy()
)
})
.collect::<Vec<_>>()
.join("\n");
std::fs::write(gen_path.join("mod.rs"), m).unwrap();
}
fn rustfmt(gen_path: &Path) {
let paths = std::fs::read_dir(gen_path).unwrap();
for path in paths {
let path = path.unwrap().path();
std::process::Command::new("rustfmt")
.arg(path)
.output()
.unwrap();
}
}