#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
use std::fmt::Write;
use std::fs;
use std::path::PathBuf;
fn main() {
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let workspace_root = manifest_dir.parent().and_then(|p| p.parent());
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set");
let output_path = format!("{out_dir}/llms-full.txt");
let (llms_path, schema_dir) = workspace_root.map_or((None, None), |root| {
let llms = root.join("llms.txt");
let schema = root.join("schema");
if llms.exists() && schema.exists() {
println!("cargo::rerun-if-changed={}", llms.display());
println!("cargo::rerun-if-changed={}", schema.display());
(Some(llms), Some(schema))
} else {
(None, None)
}
});
if let (Some(llms_path), Some(schema_dir)) = (llms_path, schema_dir) {
let mut output = fs::read_to_string(&llms_path).expect("Failed to read llms.txt");
output.push_str("\n## CUE Schema Reference\n");
let mut schemas: Vec<_> = fs::read_dir(&schema_dir)
.expect("schema directory not found")
.filter_map(std::result::Result::ok)
.filter(|e| e.path().extension().is_some_and(|ext| ext == "cue"))
.collect();
schemas.sort_by_key(std::fs::DirEntry::path);
for entry in schemas {
let name = entry.file_name();
let content = fs::read_to_string(entry.path())
.unwrap_or_else(|_| panic!("Failed to read {}", entry.path().display()));
let _ = write!(
output,
"\n### {}\n```cue\n{}\n```\n",
name.to_string_lossy(),
content.trim()
);
}
fs::write(&output_path, output).expect("Failed to write llms-full.txt");
} else {
fs::write(
&output_path,
"# cuenv\n\nFor full documentation, visit https://github.com/cuenv/cuenv\n",
)
.expect("Failed to write llms-full.txt placeholder");
}
}