2023-10-14 05:19:09 +02:00
|
|
|
|
2024-03-09 11:28:31 +01:00
|
|
|
use std::{error::Error, ffi::OsStr, fs, path::PathBuf, process::{self, Command}};
|
2023-10-14 05:19:09 +02:00
|
|
|
|
2023-10-13 16:10:47 +02:00
|
|
|
use css_minify::optimizations::{Minifier, Level};
|
|
|
|
|
2023-10-14 05:19:09 +02:00
|
|
|
fn asset_path(asset: &PathBuf) -> PathBuf {
|
|
|
|
let mut path = asset.components().take(1).collect::<PathBuf>();
|
|
|
|
path.push(asset.components().last().unwrap());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2023-11-06 04:46:05 +01:00
|
|
|
fn template_path(template: &PathBuf) -> PathBuf {
|
|
|
|
let mut path = template.components().take(template.components().count() - 2).collect::<PathBuf>();
|
|
|
|
path.push(template.components().last().unwrap());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2023-10-14 05:19:09 +02:00
|
|
|
fn extfilter(valid: String, x: Option<&OsStr>) -> bool {
|
|
|
|
if x.is_none() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let ext = x.clone().unwrap().to_str().unwrap().to_string();
|
|
|
|
ext == valid
|
|
|
|
}
|
|
|
|
|
2023-10-31 16:28:00 +01:00
|
|
|
fn system(cmd: &str, args: &[&str]) -> Result<String, Box<dyn Error>> {
|
2023-10-29 10:13:27 +01:00
|
|
|
let out = Command::new(cmd)
|
|
|
|
.args(args)
|
|
|
|
.output()
|
2023-10-31 16:28:00 +01:00
|
|
|
?;
|
2023-10-29 10:25:33 +01:00
|
|
|
|
|
|
|
if out.stderr.len() != 0 {
|
|
|
|
panic!("Got this while running {cmd} with \"{}\": {}", args.join(" "), String::from_utf8(out.stderr).unwrap())
|
|
|
|
}
|
|
|
|
|
2023-10-31 16:28:00 +01:00
|
|
|
Ok(String::from_utf8(out.stdout)?)
|
2023-10-29 10:13:27 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 11:28:31 +01:00
|
|
|
fn check_cmd<T: AsRef<OsStr>>(cmd: T) -> bool {
|
|
|
|
Command::new(cmd)
|
|
|
|
.spawn()
|
|
|
|
.is_ok()
|
|
|
|
}
|
|
|
|
|
2023-10-13 16:10:47 +02:00
|
|
|
fn main() {
|
2023-10-20 16:49:21 +02:00
|
|
|
|
2024-03-09 11:28:31 +01:00
|
|
|
let html_exists = check_cmd("html-minifier");
|
|
|
|
let ugjs_exists = check_cmd("uglify-js");
|
|
|
|
|
|
|
|
if ! (html_exists || ugjs_exists) {
|
|
|
|
eprintln!("html-minifier or uglify-js are not installed! Trying to install them via npm...");
|
|
|
|
if ! check_cmd("npm") {
|
|
|
|
eprintln!("npm is not installed! Please install npm and/or html-minifier and uglify-js and build again");
|
|
|
|
process::exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cmd = Command::new("npm");
|
|
|
|
cmd.arg("install")
|
|
|
|
.arg("--global");
|
|
|
|
|
|
|
|
if ! html_exists {
|
|
|
|
cmd.arg("html-minifier");
|
|
|
|
}
|
|
|
|
if ! ugjs_exists {
|
|
|
|
cmd.arg("uglify-js");
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = cmd.spawn().unwrap().wait().unwrap();
|
|
|
|
if res.success() {
|
|
|
|
eprintln!("Successfully installed html-minifier and/or uglify-js");
|
|
|
|
} else {
|
|
|
|
eprintln!("Couldn't install html-minifier and/or uglify-js! Check the log above for details.");
|
|
|
|
process::exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:49:21 +02:00
|
|
|
println!("cargo:rerun-if-changed=static/assets");
|
2023-11-06 04:46:05 +01:00
|
|
|
println!("cargo:rerun-if-changed=templates/source");
|
2023-10-20 16:49:21 +02:00
|
|
|
|
2023-10-13 16:10:47 +02:00
|
|
|
let assets = fs::read_dir("static/assets").unwrap();
|
|
|
|
let assets = assets
|
|
|
|
.map(|x| x.unwrap().path())
|
|
|
|
.filter(|x| x.extension().is_some())
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
|
2023-10-14 05:19:09 +02:00
|
|
|
let styles = assets
|
|
|
|
.iter()
|
|
|
|
.filter(|x| {
|
|
|
|
extfilter("css".into(), x.extension())
|
|
|
|
})
|
|
|
|
.collect::<Vec<&PathBuf>>();
|
|
|
|
|
|
|
|
let scripts = assets
|
|
|
|
.iter()
|
|
|
|
.filter(|x| {
|
|
|
|
extfilter("js".into(), x.extension())
|
|
|
|
})
|
|
|
|
.collect::<Vec<&PathBuf>>();
|
|
|
|
|
|
|
|
styles.iter().for_each(|asset| {
|
2023-10-13 16:10:47 +02:00
|
|
|
let bundled = Minifier::default().minify(
|
|
|
|
String::from_utf8(fs::read(asset).unwrap()).unwrap().as_str(),
|
|
|
|
Level::Zero
|
|
|
|
).unwrap();
|
|
|
|
|
2023-10-14 05:19:09 +02:00
|
|
|
fs::write(asset_path(asset), bundled).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
scripts.iter().for_each(|asset| {
|
|
|
|
Command::new("uglifyjs")
|
|
|
|
.arg(asset)
|
|
|
|
.arg("-o")
|
|
|
|
.arg(asset_path(asset))
|
2023-10-31 16:28:00 +01:00
|
|
|
.arg("-c")
|
2023-10-14 05:19:09 +02:00
|
|
|
.spawn()
|
|
|
|
.unwrap();
|
2023-10-29 10:11:38 +01:00
|
|
|
});
|
|
|
|
|
2023-11-06 04:46:05 +01:00
|
|
|
// precompile templates
|
|
|
|
let templates = fs::read_dir("templates/source").unwrap();
|
|
|
|
let templates = templates
|
|
|
|
.map(|x| x.unwrap().path().canonicalize().unwrap())
|
|
|
|
.filter(|x| extfilter("html".into(), x.extension()))
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
|
|
|
|
templates.iter().for_each(|template| {
|
|
|
|
Command::new("html-minifier")
|
|
|
|
.arg(template.canonicalize().unwrap())
|
|
|
|
.arg("--collapse-whitespace")
|
|
|
|
// .arg("--minify-js")
|
|
|
|
// .arg("--minify-css")
|
|
|
|
// .arg("--keep-closing-slash")
|
|
|
|
.arg("--output")
|
|
|
|
.arg(template_path(template))
|
|
|
|
.spawn()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-10-31 16:28:00 +01:00
|
|
|
let commit = system("git", &["rev-parse", "HEAD"]).map_err(|x| x.to_string());
|
|
|
|
let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]).map_err(|x| x.to_string());
|
2023-10-29 10:14:58 +01:00
|
|
|
|
2023-10-31 16:28:00 +01:00
|
|
|
match commit {
|
|
|
|
Err(err) => panic!("Can't get commit: {}", err),
|
|
|
|
Ok(commit) => println!("cargo:rustc-env=COMMIT_HASH={commit}")
|
|
|
|
}
|
|
|
|
|
|
|
|
match branch {
|
|
|
|
Err(err) => panic!("Can't get commit: {}", err),
|
|
|
|
Ok(branch) => println!("cargo:rustc-env=COMMIT_BRANCH={branch}")
|
|
|
|
}
|
2023-10-13 16:10:47 +02:00
|
|
|
}
|