2022-04-29 17:48:41 +10:00
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::{env, fs};
|
|
|
|
|
|
|
|
use anyhow::Context;
|
2022-04-18 10:04:39 +10:00
|
|
|
use proc_macro2::{Ident, Span};
|
|
|
|
|
|
|
|
mod block;
|
2022-04-29 17:48:41 +10:00
|
|
|
mod entity;
|
2022-07-28 00:10:35 +10:00
|
|
|
mod entity_event;
|
2022-04-18 10:04:39 +10:00
|
|
|
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
2022-07-28 00:10:35 +10:00
|
|
|
let generators = [
|
|
|
|
(entity::build as fn() -> _, "entity.rs"),
|
|
|
|
(entity_event::build, "entity_event.rs"),
|
|
|
|
(block::build, "block.rs"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let out_dir = env::var_os("OUT_DIR").context("can't get OUT_DIR env var")?;
|
|
|
|
|
|
|
|
for (g, file_name) in generators {
|
|
|
|
println!("cargo:rerun-if-changed=extracted/{file_name}");
|
2022-04-18 10:04:39 +10:00
|
|
|
|
2022-07-28 00:10:35 +10:00
|
|
|
let path = Path::new(&out_dir).join(file_name);
|
|
|
|
let code = g()?.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();
|
|
|
|
}
|
2022-04-18 10:04:39 +10:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident(s: impl AsRef<str>) -> Ident {
|
|
|
|
let s = s.as_ref().trim();
|
2022-04-29 17:48:41 +10:00
|
|
|
|
2022-07-28 00:10:35 +10:00
|
|
|
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()),
|
|
|
|
}
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|