109 lines
3.5 KiB
Rust
109 lines
3.5 KiB
Rust
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();
|
|
|
|
// Get slug and absolute path of all assets
|
|
// In our actual source:
|
|
// ```
|
|
// let assets: Vec<(String, Vec<u8>)> = vec![]
|
|
// for (slug, path) in ASSET_PATHS {
|
|
// let asset = (slug, include_bytes!(path));
|
|
// assets.push(asset);
|
|
// }
|
|
// db.add_assets(assets).await?
|
|
// ```
|
|
let assets_dir = [
|
|
&env::var("CARGO_MANIFEST_DIR").unwrap(),
|
|
"/src/client/assets",
|
|
]
|
|
.concat();
|
|
println!("cargo:rerun-if-changed={}", assets_dir);
|
|
let pattern = [&assets_dir, "/**/*.*"].concat();
|
|
let paths = glob(&pattern).unwrap();
|
|
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("add_assets.rs");
|
|
let mut file = BufWriter::new(File::create(&path).unwrap());
|
|
|
|
writeln!(
|
|
&mut file,
|
|
r#"{{let mut assets: Vec<(&str, &[u8])> = vec![];"#
|
|
)
|
|
.unwrap();
|
|
let mut ident_name = String::new();
|
|
for path in paths {
|
|
match path {
|
|
Ok(path) => {
|
|
ident_name.push('a');
|
|
let slug_path = path.clone();
|
|
let slug: String = slug_path
|
|
.strip_prefix(&assets_dir)
|
|
.unwrap()
|
|
.as_os_str()
|
|
.to_str()
|
|
.unwrap()
|
|
.into(); // eek
|
|
println!("cargo:warning={}{:?}", &slug, &path);
|
|
writeln!(
|
|
&mut file,
|
|
r#"assets.push(("{slug}",include_bytes!("{}")));"#,
|
|
path.to_str().unwrap()
|
|
)
|
|
.unwrap();
|
|
}
|
|
Err(_) => todo!(),
|
|
}
|
|
}
|
|
writeln!(&mut file, r#"db.add_assets(assets).await?;}}"#).unwrap();
|
|
}
|