use std::{env, fs, path::Path, path::PathBuf};
const VENDORED: &str = "perf-matrix.vendored.yaml";
fn inside_aprender_workspace(manifest_dir: &Path) -> bool {
let root = manifest_dir.join("../../Cargo.toml");
match fs::read_to_string(&root) {
Ok(text) => text.contains("[workspace]") && text.contains("crates/aprender-test-lib"),
Err(_) => false,
}
}
fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
let vendored = manifest_dir.join(VENDORED);
let workspace = manifest_dir.join("../../scripts/perf-matrix.yaml");
println!("cargo:rerun-if-changed={}", vendored.display());
let vendored_bytes = fs::read(&vendored).unwrap_or_else(|e| {
panic!(
"PMAT-958: {} must ship with the crate: {e}",
vendored.display()
)
});
let chosen = if inside_aprender_workspace(&manifest_dir) && workspace.is_file() {
println!("cargo:rerun-if-changed={}", workspace.display());
let ws = fs::read(&workspace).expect("read scripts/perf-matrix.yaml");
assert!(
ws == vendored_bytes,
"PMAT-958: {} differs from {}; run `cp scripts/perf-matrix.yaml \
crates/aprender-test-lib/{VENDORED}` so the published crate embeds the \
same bytes the workspace does",
workspace.display(),
vendored.display()
);
ws
} else {
vendored_bytes };
fs::write(out_dir.join("perf-matrix.yaml"), chosen).expect("write OUT_DIR/perf-matrix.yaml");
}