diff --git a/build.rs b/build.rs index 0dd9656..a2c4431 100644 --- a/build.rs +++ b/build.rs @@ -1,16 +1,19 @@ use serde_json::Value; use std::error::Error; -use std::fs::File; +use std::fs::{self, File}; use std::io::{BufRead, BufReader, Write}; - use std::path::Path; -fn write_version<'a>(f: &'a mut File, doc: &'a Value) -> Result<&'a str, Box> { - let package_version = doc - .get("version") - .ok_or("No version found")? - .as_str() - .unwrap(); + +fn package_version(json: &Value) -> Result<&str, Box> { + json + .get("version") + .ok_or("No version found")? + .as_str() + .ok_or("Version is not a string".into()) +} + +fn write_version<'a>(f: &'a mut File, package_version: &'a str) -> Result<&'a str, Box> { f.write_all( format!( "pub const PACKAGE_VERSION: &'static str = \"{}\";\n", @@ -63,60 +66,49 @@ fn main() { let json: Value = include_str!("resources/rootasrole.json") .parse() .expect("Failed to parse rootasrole.json"); + let package_version = package_version(&json).expect("Failed to get package version"); let dest_path = std::path::Path::new("src").join("version.rs"); + if dest_path.exists() && fs::read_to_string(&dest_path).unwrap().ends_with(&format!("\"{}\";\n",package_version)) { + return; + } let mut f = File::create(dest_path).unwrap(); f.write_all(b"// This file is generated by build.rs\n") .unwrap(); f.write_all(b"// Do not edit this file directly\n").unwrap(); f.write_all(b"// Instead edit build.rs and run cargo build\n") .unwrap(); - match write_version(&mut f, &json) { - Ok(package_version) => { - if let Err(err) = set_cargo_version(package_version, "Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - //if folder capable/ exists - if Path::new("capable/capable").is_dir() { - if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - if let Err(err) = - set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml") - { - eprintln!("cargo:warning={}", err); - } - if let Err(err) = - set_cargo_version(package_version, "capable/capable-common/Cargo.toml") - { - eprintln!("cargo:warning={}", err); - } - } - if Path::new("xtask").is_dir() { - if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - } - if Path::new("rar-common").is_dir() { - if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - } - if let Err(err) = set_readme_version(package_version, "README.md") { - eprintln!("cargo:warning={}", err); - } - //if let Err(err) = set_pkgbuild_version(package_version, "PKGBUILD") { - //eprintln!("cargo:warning={}", err); - //} + write_version(&mut f, &package_version).expect("Failed to write version"); + if let Err(err) = set_cargo_version(package_version, "Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + //if folder capable/ exists + if Path::new("capable/capable").is_dir() { + if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + if let Err(err) = + set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml") + { + eprintln!("cargo:warning={}", err); } - Err(err) => { + if let Err(err) = + set_cargo_version(package_version, "capable/capable-common/Cargo.toml") + { eprintln!("cargo:warning={}", err); } } - - // let xml = include_str!("resources/rootasrole.xml"); - // if let Err(err) = write_dtd(&mut f, xml) { - // eprintln!("cargo:warning={}", err); - // } - + if Path::new("xtask").is_dir() { + if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + } + if Path::new("rar-common").is_dir() { + if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + } + if let Err(err) = set_readme_version(package_version, "README.md") { + eprintln!("cargo:warning={}", err); + } f.flush().unwrap(); }