evie/build.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2024-11-13 21:41:12 -05:00
use std::{
env,
fs::File,
io::Write,
io::{BufWriter, Read},
path::Path,
};
use dotenvy::{dotenv, dotenv_iter};
use glob::glob;
use phf_codegen::Map;
fn main() {
println!("cargo:rerun-if-changed={}", dotenv().unwrap().display());
for item in dotenv_iter().unwrap() {
let (key, value) = item.unwrap();
println!("cargo:rustc-env={key}={value}");
}
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let template_dir = [&env::var("CARGO_MANIFEST_DIR").unwrap(), "/src/templates"].concat();
println!("cargo:rerun-if-changed={}", template_dir);
let pattern = [&template_dir, "/**/*.html"].concat();
let mut map: Map<String> = phf_codegen::Map::new();
let paths = glob(&pattern).unwrap();
for path in paths {
match path {
Ok(path) => {
let slug_path = path.clone();
let slug: String = slug_path
.strip_prefix(&template_dir)
.unwrap()
.as_os_str()
.to_str()
.unwrap()
.strip_suffix(".html")
.unwrap()
.into(); // eek
let mut file = File::open(path.clone()).unwrap();
let mut content = "r####\"".to_string();
file.read_to_string(&mut content).unwrap();
content.push_str("\"####");
println!("cargo:warning={}\n{}", &slug, &content);
map.entry(slug, &content);
}
Err(_) => todo!(),
}
}
writeln!(
&mut file,
"static TEMPLATES: phf::Map<&'static str, &'static str> = {};",
map.build()
)
.unwrap();
}