get rid of function panics in the build.rs

This commit is contained in:
blek 2023-11-01 01:28:00 +10:00
parent 6302d11acd
commit 6120ce7a30
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 16 additions and 9 deletions

View File

@ -1,5 +1,5 @@
use std::{fs, path::PathBuf, ffi::OsStr, process::Command}; use std::{fs, path::PathBuf, ffi::OsStr, process::Command, error::Error};
use css_minify::optimizations::{Minifier, Level}; use css_minify::optimizations::{Minifier, Level};
@ -17,17 +17,17 @@ fn extfilter(valid: String, x: Option<&OsStr>) -> bool {
ext == valid ext == valid
} }
fn system(cmd: &str, args: &[&str]) -> String { fn system(cmd: &str, args: &[&str]) -> Result<String, Box<dyn Error>> {
let out = Command::new(cmd) let out = Command::new(cmd)
.args(args) .args(args)
.output() .output()
.unwrap(); ?;
if out.stderr.len() != 0 { if out.stderr.len() != 0 {
panic!("Got this while running {cmd} with \"{}\": {}", args.join(" "), String::from_utf8(out.stderr).unwrap()) panic!("Got this while running {cmd} with \"{}\": {}", args.join(" "), String::from_utf8(out.stderr).unwrap())
} }
String::from_utf8(out.stdout).unwrap() Ok(String::from_utf8(out.stdout)?)
} }
fn main() { fn main() {
@ -65,17 +65,24 @@ fn main() {
scripts.iter().for_each(|asset| { scripts.iter().for_each(|asset| {
Command::new("uglifyjs") Command::new("uglifyjs")
.arg("-c")
.arg(asset) .arg(asset)
.arg("-o") .arg("-o")
.arg(asset_path(asset)) .arg(asset_path(asset))
.arg("-c")
.spawn() .spawn()
.unwrap(); .unwrap();
}); });
let commit = system("git", &["rev-parse", "HEAD"]); let commit = system("git", &["rev-parse", "HEAD"]).map_err(|x| x.to_string());
let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]); let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]).map_err(|x| x.to_string());
println!("cargo:rustc-env=COMMIT_HASH={commit}"); match commit {
println!("cargo:rustc-env=COMMIT_BRANCH={branch}"); 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}")
}
} }