2022-11-14 01:10:42 +11:00
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::{env, fs};
|
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
use proc_macro2::{Ident, Span};
|
|
|
|
|
|
|
|
mod block;
|
|
|
|
mod enchant;
|
|
|
|
mod item;
|
2023-04-01 08:58:47 +11:00
|
|
|
mod packet_id;
|
2023-02-15 21:36:21 +11:00
|
|
|
mod sound;
|
2022-11-28 00:12:08 +11:00
|
|
|
mod translation_key;
|
2022-11-14 01:10:42 +11:00
|
|
|
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
2022-12-31 01:18:24 +11:00
|
|
|
println!("cargo:rerun-if-changed=../../extracted/");
|
2022-11-14 01:10:42 +11:00
|
|
|
|
|
|
|
let generators = [
|
|
|
|
(block::build as fn() -> _, "block.rs"),
|
|
|
|
(enchant::build, "enchant.rs"),
|
2022-11-28 00:12:08 +11:00
|
|
|
(item::build, "item.rs"),
|
2023-02-15 21:36:21 +11:00
|
|
|
(sound::build, "sound.rs"),
|
2022-11-28 00:12:08 +11:00
|
|
|
(translation_key::build, "translation_key.rs"),
|
2023-04-01 08:58:47 +11:00
|
|
|
(packet_id::build, "packet_id.rs"),
|
2022-11-14 01:10:42 +11:00
|
|
|
];
|
|
|
|
|
|
|
|
let out_dir = env::var_os("OUT_DIR").context("failed to get OUT_DIR env var")?;
|
|
|
|
|
|
|
|
for (gen, file_name) in generators {
|
|
|
|
let path = Path::new(&out_dir).join(file_name);
|
|
|
|
let code = gen()?.to_string();
|
|
|
|
fs::write(&path, code)?;
|
|
|
|
|
|
|
|
// Format the output for debugging purposes.
|
|
|
|
// Doesn't matter if rustfmt is unavailable.
|
|
|
|
let _ = Command::new("rustfmt").arg(path).output();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident(s: impl AsRef<str>) -> Ident {
|
|
|
|
let s = s.as_ref().trim();
|
|
|
|
|
|
|
|
match s.as_bytes() {
|
|
|
|
// TODO: check for the other rust keywords.
|
|
|
|
[b'0'..=b'9', ..] | b"type" => Ident::new(&format!("_{s}"), Span::call_site()),
|
|
|
|
_ => Ident::new(s, Span::call_site()),
|
|
|
|
}
|
|
|
|
}
|