100 lines
2.9 KiB
Rust
100 lines
2.9 KiB
Rust
#[cfg(not(debug_assertions))]
|
|
use std::{
|
|
env, fs,
|
|
fs::{create_dir_all, DirEntry, File},
|
|
io,
|
|
io::Write,
|
|
path::Path,
|
|
process::Command,
|
|
};
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
|
|
if dir.is_dir() {
|
|
for entry in fs::read_dir(dir)? {
|
|
let entry = entry?;
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
visit_dirs(&path, cb)?;
|
|
} else {
|
|
cb(&entry);
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
fn process_static(file: &DirEntry) {
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
|
let path = file.path();
|
|
|
|
let in_path = path.to_str().unwrap();
|
|
let art_path = format!("{}/{}", out_dir, in_path);
|
|
let art_dir = format!("{}/{}", out_dir, path.parent().unwrap().to_str().unwrap());
|
|
|
|
match path.extension().map(|o| o.to_str()).flatten() {
|
|
Some("ts") => {}
|
|
Some("js") => {
|
|
create_dir_all(art_dir).unwrap();
|
|
|
|
if art_path.ends_with(".min.js") {
|
|
Command::new("cp").arg(in_path).arg(art_path).spawn().expect("Could not copy");
|
|
} else {
|
|
let minified = Command::new("npx")
|
|
.arg("minify")
|
|
.arg(in_path)
|
|
.output()
|
|
.expect("Could not minify");
|
|
|
|
let mut fh = File::create(art_path).expect("Couldn't create file");
|
|
fh.write(&minified.stdout).unwrap();
|
|
}
|
|
}
|
|
Some("css") => {
|
|
create_dir_all(art_dir).unwrap();
|
|
|
|
if art_path.ends_with(".min.css") {
|
|
Command::new("cp").arg(in_path).arg(art_path).spawn().expect("Could not copy");
|
|
} else {
|
|
let minified = Command::new("npx")
|
|
.arg("minify")
|
|
.arg(in_path)
|
|
.output()
|
|
.expect("Could not minify");
|
|
|
|
let mut fh = File::create(art_path).expect("Couldn't create file");
|
|
fh.write(&minified.stdout).unwrap();
|
|
}
|
|
}
|
|
_ => {
|
|
create_dir_all(art_dir).unwrap();
|
|
|
|
Command::new("cp").arg(in_path).arg(art_path).spawn().expect("Could not copy");
|
|
}
|
|
}
|
|
}
|
|
|
|
// fn compile_tsc(file: &DirEntry) {
|
|
// if path.extension() == Some("ts") {
|
|
// let out_dir = env::var("OUT_DIR").unwrap();
|
|
// let path = file.path();
|
|
//
|
|
// Command::new("npx")
|
|
// .arg("tsc")
|
|
// .arg(in_path)
|
|
// .arg(art_path)
|
|
// .spawn()
|
|
// .expect("Could not compile");
|
|
// }
|
|
// }
|
|
|
|
fn main() {
|
|
println!("cargo:rerun-if-changed=migrations");
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
visit_dirs("web/static".as_ref(), &process_static).unwrap();
|
|
|
|
// visit_dirs("web/static".as_ref(), &compile_tsc).unwrap();
|
|
}
|