2022-06-29 00:59:20 +03:00
|
|
|
use std::process::{Command, Stdio};
|
2022-07-24 15:02:14 +03:00
|
|
|
use std::fs::{self, read_dir, create_dir_all, read_to_string};
|
2022-06-29 00:59:20 +03:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
fn compile_blueprint<T: ToString>(path: T) -> Result<String, String> {
|
|
|
|
// python blueprint-compiler/blueprint-compiler.py compile ui/main.blp
|
2022-06-29 10:11:19 +03:00
|
|
|
let output = Command::new("python3")
|
2022-06-29 00:59:20 +03:00
|
|
|
.arg("blueprint-compiler/blueprint-compiler.py")
|
|
|
|
.arg("compile")
|
|
|
|
.arg(path.to_string())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.output();
|
|
|
|
|
|
|
|
match output {
|
|
|
|
Ok(output) => {
|
|
|
|
if output.status.success() {
|
|
|
|
Ok(String::from_utf8(output.stdout).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
Err(String::from_utf8(output.stdout).unwrap())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 15:44:48 +03:00
|
|
|
fn blp_process_dir(dir: String) {
|
|
|
|
let source_dir = format!("assets/ui/{}", &dir).replace("//", "/");
|
|
|
|
let dist_dir = format!("assets/ui/.dist/{}", &dir).replace("//", "/");
|
2022-07-24 15:02:14 +03:00
|
|
|
|
2022-07-29 15:44:48 +03:00
|
|
|
if let Ok(entries) = read_dir(&source_dir) {
|
|
|
|
if let Err(_) = read_dir(&dist_dir) {
|
|
|
|
create_dir_all(&dist_dir).expect("UI dist dir couldn't be created");
|
2022-06-29 00:59:20 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 21:31:28 +03:00
|
|
|
// println!("cargo:rerun-if-changed={}/*.blp", &source_dir);
|
2022-07-24 15:02:14 +03:00
|
|
|
|
2022-06-29 00:59:20 +03:00
|
|
|
for entry in entries {
|
|
|
|
if let Ok(entry) = entry {
|
|
|
|
if let Ok(metadata) = entry.metadata() {
|
2022-07-29 15:44:48 +03:00
|
|
|
let entry_path = entry.path().to_str().unwrap().to_string();
|
|
|
|
let entry_filename = entry.file_name().to_str().unwrap().to_string();
|
2022-06-29 00:59:20 +03:00
|
|
|
|
2022-07-29 15:44:48 +03:00
|
|
|
if metadata.is_file() {
|
|
|
|
let entry_dist_path = format!("{}/{}.ui", &dist_dir, &entry_filename[..entry_filename.len() - 4]);
|
2022-06-29 00:59:20 +03:00
|
|
|
|
|
|
|
match compile_blueprint(&entry_path) {
|
|
|
|
Ok(xml) => {
|
|
|
|
let result = fs::write(entry_dist_path, xml);
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
println!("cargo:warning=Couldn't write compiled XML UI: {}", err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
if Path::new(&entry_dist_path).exists() {
|
|
|
|
fs::remove_file(entry_dist_path).expect("Couldn't remove broken file");
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("cargo:warning=Couldn't compile {}: {}", entry_path, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 15:44:48 +03:00
|
|
|
|
|
|
|
else if metadata.is_dir() && &entry_filename[0..1] != "." {
|
|
|
|
blp_process_dir(format!("{}/{}", &dir, &entry_filename));
|
|
|
|
}
|
2022-06-29 00:59:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 15:44:48 +03:00
|
|
|
|
|
|
|
fn main() {
|
2022-08-02 19:43:07 +03:00
|
|
|
blp_process_dir(String::new());
|
|
|
|
|
2022-07-29 15:44:48 +03:00
|
|
|
if let Ok(_) = read_to_string("assets/resources.xml") {
|
|
|
|
gtk4::gio::compile_resources(
|
|
|
|
"assets",
|
|
|
|
"assets/resources.xml",
|
|
|
|
".assets.gresource",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|