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-04-18 10:04:39 +10:00
|
|
|
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
2022-04-29 17:48:41 +10:00
|
|
|
for file in ["blocks.json", "entities.json"] {
|
2022-04-20 16:45:34 +10:00
|
|
|
println!("cargo:rerun-if-changed=data/{file}");
|
2022-04-18 10:04:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
block::build()?;
|
2022-04-29 17:48:41 +10:00
|
|
|
entity::build()?;
|
2022-04-18 10:04:39 +10:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident(s: impl AsRef<str>) -> Ident {
|
|
|
|
let s = s.as_ref().trim();
|
|
|
|
if s.starts_with(char::is_numeric) {
|
|
|
|
Ident::new(&format!("_{s}"), Span::call_site())
|
|
|
|
} else {
|
|
|
|
Ident::new(s, Span::call_site())
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 17:48:41 +10:00
|
|
|
|
|
|
|
fn write_to_out_path(file_name: impl AsRef<str>, content: impl AsRef<str>) -> anyhow::Result<()> {
|
|
|
|
let out_dir = env::var_os("OUT_DIR").context("can't get OUT_DIR env var")?;
|
|
|
|
let path = Path::new(&out_dir).join(file_name.as_ref());
|
|
|
|
|
|
|
|
fs::write(&path, &content.as_ref())?;
|
|
|
|
|
|
|
|
// Format the output for debugging purposes.
|
|
|
|
// Doesn't matter if rustfmt is unavailable.
|
|
|
|
let _ = Command::new("rustfmt").arg(path).output();
|
|
|
|
Ok(())
|
|
|
|
}
|