use std::path::Path;
use xshell::{cmd, Shell};
use re_build_tools::{compute_file_hash, read_versioning_hash, write_versioning_hash};
const SOURCE_HASH_PATH: &str = "./source_hash.txt";
const FBS_REFLECTION_DEFINITION_PATH: &str = "./definitions/reflection.fbs";
fn should_run() -> bool {
#![allow(clippy::match_same_arms)]
use re_build_tools::Environment;
match Environment::detect() {
Environment::PublishingCrates => false,
Environment::RerunCI | Environment::CondaBuild => false,
Environment::DeveloperInWorkspace => {
Path::new(SOURCE_HASH_PATH).exists()
}
Environment::UsedAsDependency => false,
}
}
fn main() {
if !should_run() {
return;
}
let cur_hash = read_versioning_hash(SOURCE_HASH_PATH);
let new_hash = compute_file_hash(FBS_REFLECTION_DEFINITION_PATH);
eprintln!("cur_hash: {cur_hash:?}");
eprintln!("new_hash: {new_hash:?}");
if cur_hash.is_none() || cur_hash.as_ref() == Some(&new_hash) {
return;
}
let sh = Shell::new().expect("Shell::new() failed");
#[allow(clippy::unwrap_used)] cmd!(
sh,
"flatc -o src/ --rust --gen-onefile --filename-suffix '' {FBS_REFLECTION_DEFINITION_PATH}"
)
.run()
.map_err(|err| eprintln!("flatc failed with error: {err}"))
.unwrap();
cmd!(sh, "cargo fmt").run().ok();
write_versioning_hash(SOURCE_HASH_PATH, new_hash);
}