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
use std::collections::LinkedList;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::fs;
pub static mut COMPILER_PATH: Option<PathBuf> = None;
pub static mut GENERATED_PREFIX: Option<String> = None;
#[cfg(feature = "downloader")]
mod downloader;
#[cfg(feature = "downloader")]
pub use downloader::*;
pub fn build_schema_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) {
println!(
"cargo:rerun-if-changed={}/mod.rs",
destination.as_ref().to_str().unwrap()
);
if !destination.as_ref().exists() {
fs::create_dir_all(destination.as_ref()).unwrap();
}
fs::read_dir(destination.as_ref())
.unwrap()
.filter_map(|entry| {
let entry = entry.unwrap();
let name = entry.file_name().to_str().unwrap().to_string();
if entry.file_type().unwrap().is_file() && name != "mod.rs" {
Some(name)
} else {
None
}
})
.for_each(|file| fs::remove_file(PathBuf::from(destination.as_ref()).join(file)).unwrap());
let files = recurse_schema_dir(source, destination.as_ref());
fs::write(
PathBuf::from(destination.as_ref()).join("mod.rs"),
&files
.into_iter()
.map(|mut schema_name| {
schema_name.insert_str(0, "pub mod ");
schema_name.push(';');
schema_name.push('\n');
schema_name
})
.collect::<String>(),
)
.unwrap();
}
pub fn build_schema(schema: impl AsRef<Path>, destination: impl AsRef<Path>) {
let (schema, destination) = (schema.as_ref(), destination.as_ref());
let compiler_path = compiler_path();
println!("cargo:rerun-if-changed={}", compiler_path.to_str().unwrap());
println!("cargo:rerun-if-changed={}", schema.to_str().unwrap());
println!("cargo:rerun-if-changed={}", destination.to_str().unwrap());
let output = Command::new(compiler_path)
.arg("--files")
.arg(schema)
.arg("--rust")
.arg(destination.to_str().unwrap())
.output()
.expect("Could not run bebopc");
if !(output.status.success()) {
println!(
"cargo:warning=Failed to build schema {}",
schema.to_str().unwrap()
);
for line in String::from_utf8(output.stdout).unwrap().lines() {
println!("cargo:warning=STDOUT: {}", line);
}
for line in String::from_utf8(output.stderr).unwrap().lines() {
println!("cargo:warning=STDERR: {}", line);
}
panic!("Failed to build schema!");
}
}
fn recurse_schema_dir(dir: impl AsRef<Path>, dest: impl AsRef<Path>) -> LinkedList<String> {
let mut list = LinkedList::new();
for dir_entry in fs::read_dir(&dir).unwrap() {
let dir_entry = dir_entry.unwrap();
let file_type = dir_entry.file_type().unwrap();
let file_path = PathBuf::from(dir.as_ref()).join(dir_entry.file_name());
if file_type.is_dir() {
if dir_entry.file_name() == "ShouldFail" {
} else {
list.append(&mut recurse_schema_dir(&file_path, dest.as_ref()));
}
} else if file_type.is_file()
&& file_path
.extension()
.map(|s| s.to_str().unwrap())
.unwrap_or("")
== "bop"
{
let fname = format!(
"{}{}",
unsafe { GENERATED_PREFIX.as_deref().unwrap_or_else(|| "".into()) },
file_stem(file_path.as_path())
);
build_schema(
canonicalize(file_path.to_str().unwrap()),
canonicalize(&dest).join(fname.clone() + ".rs"),
);
list.push_back(fname);
} else {
}
}
list
}
fn file_stem(path: impl AsRef<Path>) -> String {
path.as_ref()
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string()
}
fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
let p = path
.as_ref()
.canonicalize()
.unwrap()
.to_str()
.unwrap()
.to_string();
if p.starts_with(r"\\?\") {
p.strip_prefix(r"\\?\").unwrap()
} else {
&p
}
.into()
}
fn compiler_path() -> PathBuf {
(unsafe { COMPILER_PATH.clone() }).unwrap_or_else(|| canonicalize("bebopc"))
}