use std::{env, fs};
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=proto/pb.proto");
println!("cargo:rerun-if-changed=proto/grpc.proto");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
let mut cfg = prost_build::Config::new();
cfg.out_dir(&out_dir);
cfg.compile_protos(&["proto/pb.proto", "proto/grpc.proto"], &["proto"]).expect("Failed to compile proto files");
let entries = fs::read_dir(&out_dir).expect("Failed to read OUT_DIR");
let mut modules: Vec<(String, String)> = Vec::new();
for entry in entries {
let path = entry.expect("bad entry").path();
if path.extension().map(|e| e == "rs").unwrap_or(false) {
let fname = path.file_name().unwrap().to_string_lossy().to_string();
let module = fname.trim_end_matches(".rs").replace('.', "_");
modules.push((module, fname));
}
}
let wrapper_path = out_dir.join("generated.rs");
let mut wrapper = String::new();
for (module, filename) in modules {
wrapper.push_str(&format!(
"pub mod {module} {{ include!(concat!(env!(\"OUT_DIR\"), \"/{filename}\")); }}\n"
));
}
fs::write(&wrapper_path, wrapper).expect("Failed to write generated wrapper");
}