This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
162 lines (145 loc) · 5.22 KB
/
build.rs
File metadata and controls
162 lines (145 loc) · 5.22 KB
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
use std::{
collections::BTreeMap,
env,
ffi::OsStr,
fmt::Display,
fs::{copy, create_dir_all, read_dir, write, File},
io::{BufRead, BufReader, BufWriter, Write},
path::Path,
};
use walkdir::WalkDir;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
set_build_env();
remove_license_header_from_templates(&out_dir);
generate_tests(out_dir);
}
fn set_build_env() {
let path = "build.env";
println!("cargo:rerun-if-changed={}", path);
if let Ok(file) = File::open(path) {
for (key, value) in BufReader::new(file)
.lines()
.map(|line| line.unwrap())
.collect::<Vec<_>>()
.iter()
.flat_map(|line| line.split_once('='))
{
println!("cargo:rustc-env={}={}", key, value);
}
}
}
fn remove_license_header_from_templates(out_dir: impl AsRef<Path>) {
let in_dir = "src/datapack_template";
println!("cargo:rerun-if-changed={}", in_dir);
for entry in WalkDir::new(&in_dir) {
let entry = entry.unwrap();
let in_path = entry.path();
let out_path = out_dir.as_ref().join(in_path);
let file_type = entry.file_type();
if file_type.is_dir() {
println!("Creating dir {}", out_path.display());
create_dir_all(out_path).unwrap();
} else if file_type.is_file() {
println!("Creating file {}", out_path.display());
if in_path.extension() == Some(OsStr::new("mcfunction")) {
let reader = BufReader::new(File::open(in_path).unwrap());
let mut writer = BufWriter::new(File::create(out_path).unwrap());
for line in reader
.lines()
.skip_while(|line| line.as_ref().ok().filter(|l| l.starts_with('#')).is_some())
.skip_while(|line| line.as_ref().ok().filter(|l| l.is_empty()).is_some())
{
writer.write_all(line.unwrap().as_bytes()).unwrap();
writer.write_all(&[b'\n']).unwrap();
}
} else {
copy(in_path, out_path).unwrap();
}
}
}
}
const DATAPACKS_PATH: &str = "tests/datapacks";
fn generate_tests(out_dir: impl AsRef<Path>) {
let datapack_path = Path::new(DATAPACKS_PATH).join("mcfd_test");
if !datapack_path.is_dir() {
return;
}
let out_dir = out_dir.as_ref().join("tests");
create_dir_all(&out_dir).unwrap();
for (namespace, tests) in find_tests(&datapack_path) {
let path = out_dir.join(format!("{}.rs", namespace));
let mut contents = tests
.into_iter()
.map(|test| test.to_string())
.collect::<Vec<_>>()
.join("\n");
contents.push('\n');
write(&path, contents).unwrap();
}
let mut writer =
BufWriter::new(File::create(out_dir.join("expand_test_templates.rs")).unwrap());
writer.write_all("{\n".as_bytes()).unwrap();
for entry in WalkDir::new(&datapack_path).sort_by_file_name() {
let entry = entry.unwrap();
if entry.file_type().is_file() {
let relative_path = entry.path().strip_prefix(DATAPACKS_PATH).unwrap();
writeln!(
writer,
" expand_test_template!(\"{}\").await?;",
relative_path
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/")
)
.unwrap();
}
}
writer.write_all("}\n".as_bytes()).unwrap();
}
fn find_tests(datapack_path: impl AsRef<Path>) -> BTreeMap<String, Vec<TestCase>> {
let datapack_path = datapack_path.as_ref();
println!("cargo:rerun-if-changed={}", datapack_path.display());
let data_path = datapack_path.join("data");
let mut tests: BTreeMap<String, Vec<TestCase>> = BTreeMap::new();
for namespace_entry in read_dir(&data_path).unwrap() {
let namespace_entry = namespace_entry.unwrap();
if !namespace_entry.file_type().unwrap().is_dir() {
continue;
}
let namespace = namespace_entry.file_name();
let namespace = namespace.to_str().unwrap();
tests.insert(
namespace.to_string(),
find_tests_in_namespace(datapack_path, namespace),
);
}
tests
}
fn find_tests_in_namespace(datapack_path: impl AsRef<Path>, namespace: &str) -> Vec<TestCase> {
let functions_path = datapack_path
.as_ref()
.join("data")
.join(namespace)
.join("functions");
let mut tests = Vec::new();
for entry in WalkDir::new(&functions_path) {
let entry = entry.unwrap();
if entry.file_type().is_file() && entry.file_name() == OsStr::new("test.mcfunction") {
let parent_file_name = entry.path().parent().unwrap().file_name().unwrap();
tests.push(TestCase {
namespace: namespace.to_string(),
name: parent_file_name.to_str().unwrap().to_string(),
});
}
}
tests
}
struct TestCase {
namespace: String,
name: String,
}
impl Display for TestCase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "test!({}, {});", self.namespace, self.name)
}
}