use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use std::str;
const DEP_LICENSES_PATH: &str = "LICENSES_DEPENDENCIES";
fn main() {
println!("Starting the update process of the dependency licenses file.");
let cargo_bom_might_be_installed = if cfg!(windows) {
Command::new("where.exe")
.args(&["cargo-bom"])
.output()
.expect("`where.exe` unavailable.")
.stdout
} else {
Command::new("which")
.args(&["cargo-bom"])
.output()
.expect("`which` unavailable.")
.stdout
};
let str_path = str::from_utf8(cargo_bom_might_be_installed.as_slice())
.expect("Unable to convert path.")
.trim();
let path = Path::new(str_path);
if !path.exists() {
let installation_code = Command::new("cargo")
.args(&["install", "cargo-bom"])
.status()
.expect("Unable to get status of cargo-bom install.");
if !installation_code.success() {
panic!("Unable to install cargo-bom.");
}
} else {
println!("cargo-bom path found at: {:?}", path);
}
let dep_licenses_in_bytes = Command::new("cargo")
.args(&["bom"])
.output()
.expect(
"Unable to read `cargo bom` output; `cargo-bom` and `cargo` should be in your path!",
)
.stdout;
write_file(DEP_LICENSES_PATH, &dep_licenses_in_bytes);
println!("Completed the update process of the dependency licenses file.");
}
fn write_file(path: &str, contents: &[u8]) {
let mut file = File::create(path).expect("Unable to create dependency license file.");
file.write_all(contents)
.expect("Unable to write license texts to license file.");
}