notalawyer_build/
lib.rs

1use std::path::{Path, PathBuf};
2
3pub fn about_hbs() -> Option<PathBuf> {
4    Some(PathBuf::from(file!()).parent()?.parent()?.join("about.hbs"))
5}
6
7pub fn build() {
8    let about_hbs = about_hbs().expect("failed to traverse to about.hbs");
9    build_with_template(about_hbs.to_str().expect("invalid about.hbs path"));
10}
11
12pub fn build_with_template(template: &str) {
13    let out_dir = std::env::var_os("OUT_DIR").unwrap();
14    let license_path = Path::new(&out_dir).join("notalawyer");
15    let mut writer = std::fs::OpenOptions::new()
16        .write(true)
17        .create(true)
18        .truncate(true)
19        .open(&license_path)
20        .expect("failed to open NOTICE file");
21    let mut child = std::process::Command::new("cargo")
22        .args(&["about", "generate", template])
23        .stdout(std::process::Stdio::piped())
24        .spawn()
25        .expect("failed to run cargo-about");
26    let mut stdout = child.stdout.take().unwrap();
27    std::io::copy(&mut stdout, &mut writer).expect("failed to write NOTICE file");
28    let is_success = child
29        .wait()
30        .expect("failed to wait for cargo-about")
31        .success();
32    if !is_success {
33        panic!("cargo-about failed");
34    }
35    println!("cargo:rerun-if-changed={}", template);
36}