use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use zip::CompressionMethod;
use zip::write::SimpleFileOptions;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let zip_path = out_dir.join("init_assets.zip");
let zip_file = File::create(&zip_path)?;
let mut zip = zip::ZipWriter::new(zip_file);
let init_dir = Path::new("_init");
let mut buffer = Vec::new();
let options = SimpleFileOptions::default().compression_method(CompressionMethod::Zstd);
for entry in WalkDir::new(init_dir) {
let entry = entry?;
let path = entry.path();
if path.ends_with(".DS_Store") {
continue;
}
let name = path.strip_prefix(init_dir)?.to_str().unwrap();
let name = name.replace("\\", "/");
if path.is_file() {
zip.start_file(name, options)?;
let mut f = File::open(path)?;
f.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;
buffer.clear();
} else if !name.is_empty() {
zip.add_directory(name, options)?;
}
}
zip.finish()?;
println!("cargo:rerun-if-changed=_init");
println!("cargo:rustc-env=INIT_ASSETS_ZIP={}", zip_path.display());
Ok(())
}