set more detailed version information during builds (#58)

This commit is contained in:
bmc-msft
2020-09-30 18:54:25 -04:00
committed by GitHub
parent bee23c56e7
commit 24f43478dd
8 changed files with 165 additions and 37 deletions

View File

@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
@ -6,7 +7,7 @@ use std::process::Command;
fn run_cmd(args: &[&str]) -> Result<String, Box<dyn Error>> {
let cmd = Command::new(args[0]).args(&args[1..]).output()?;
if cmd.status.success() {
Ok(String::from_utf8_lossy(&cmd.stdout).to_string())
Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string())
} else {
Err(From::from("failed"))
}
@ -16,21 +17,40 @@ fn read_file(filename: &str) -> Result<String, Box<dyn Error>> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
contents = contents.trim().to_string();
Ok(contents)
}
fn main() -> Result<(), Box<dyn Error>> {
fn print_version(include_sha: bool) -> Result<(), Box<dyn Error>> {
let mut version = read_file("../../CURRENT_VERSION")?;
let sha = run_cmd(&["git", "rev-parse", "HEAD"])?;
let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() {
"-local_changes"
} else {
""
};
println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes);
let version = read_file("../../CURRENT_VERSION")?;
if include_sha {
version.push('-');
version.push_str(&sha);
// if we're a non-release build, check to see if git has
// unstaged changes
if run_cmd(&["git", "diff", "--quiet"]).is_err() {
version.push('.');
version.push_str("localchanges");
}
}
println!("cargo:rustc-env=GIT_VERSION={}", sha);
println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version);
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
// If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise
// modify it to indicate local build
let include_sha = if let Ok(val) = env::var("GITHUB_REF") {
!val.starts_with("refs/tags/")
} else {
true
};
print_version(include_sha)
}