#![allow(
clippy::disallowed_methods,
reason = "Build script runs synchronously before any async runtime"
)]
#![allow(
clippy::format_push_string,
reason = "Single push per loop iteration, not hot path"
)]
#![allow(
clippy::expect_used,
reason = "Build scripts run once at compile time; panicking on failure is appropriate"
)]
#![allow(
missing_docs,
reason = "Build script `main` is an implementation detail, not a public API"
)]
use std::{
env, fs,
path::{Path, PathBuf},
};
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let skills_dir = manifest_dir.join("../../skills");
if skills_dir.exists() {
let skills_dir = skills_dir
.canonicalize()
.expect("Failed to canonicalize skills path");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env var"));
let manifest_path = out_dir.join("skills_manifest.rs");
let mut entries: Vec<RelativePath> = Vec::new();
collect_skills(&skills_dir, &skills_dir, &mut entries);
let mut manifest = String::new();
manifest.push_str("// @generated by build.rs - do not edit\n");
manifest.push_str("pub const SKILLS: &[(&str, &[u8])] = &[\n");
for entry in &entries {
let relative = entry.relative.replace('\\', "/");
let absolute = entry.absolute.display().to_string().replace('\\', "/");
manifest.push_str(&format!(
" (\"{relative}\", include_bytes!(\"{absolute}\")),\n"
));
}
manifest.push_str("];\n");
fs::write(&manifest_path, manifest).expect("Failed to write skills manifest");
println!("cargo:rerun-if-changed={}", skills_dir.display());
} else {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env var"));
let manifest_path = out_dir.join("skills_manifest.rs");
fs::write(
&manifest_path,
"// @generated by build.rs - no skills bundled\npub const SKILLS: &[(&str, &[u8])] = \
&[];\n",
)
.expect("Failed to write empty skills manifest");
}
}
struct RelativePath {
relative: String,
absolute: PathBuf,
}
fn collect_skills(dir: &Path, base: &Path, entries: &mut Vec<RelativePath>) {
if let Ok(read_dir) = fs::read_dir(dir) {
for entry in read_dir.flatten() {
let path = entry.path();
let name = entry.file_name();
if name == ".git" || name == "target" {
continue;
}
match (path.strip_prefix(base), path.is_dir()) {
(Ok(_relative), true) => {
collect_skills(&path, base, entries);
}
(Ok(relative), false) => {
entries.push(RelativePath {
relative: relative.to_string_lossy().to_string(),
absolute: path,
});
}
_ => {}
}
}
}
}