Skip to content

Commit

Permalink
feat(cli): better version information
Browse files Browse the repository at this point in the history
Versions were broken: `cargo hax -V` was displaying the latest tag
attached to the commit. So newer commits on main were tagged
`v0.1.0-alpha`.

Also, if hax is built without git, the version was empty. Now we use
`CARGO_PKG_VERSION` as a fallback.
  • Loading branch information
W95Psp committed Oct 16, 2024
1 parent cf1571e commit f540bf7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
57 changes: 38 additions & 19 deletions hax-types/build.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
macro_rules! set_empty_env_var_with_git {
($var:literal, $args: expr) => {
if let None = option_env!($var) {
println!(
"cargo:rustc-env={}={}",
$var,
std::process::Command::new("git")
.args($args)
.output()
.map(|output| String::from_utf8(output.stdout).unwrap())
.unwrap_or("unknown".into())
);
}
macro_rules! set_empty_env_var_with {
($var:literal, $f: expr) => {{
println!("cargo:rurun-if-env-changed={}", $var);
};
match option_env!($var) {
Some(value) => value.to_string(),
None => {
let value = $f;
println!("cargo:rustc-env={}={}", $var, value);
value
}
}
}};
}

const UNKNOWN: &str = "unknown";

fn git_command(args: &[&str]) -> String {
std::process::Command::new("git")
.args(args)
.output()
.map(|output| String::from_utf8(output.stdout).unwrap().trim().to_string())
.ok()
.filter(|s| !s.is_empty())
.unwrap_or(UNKNOWN.to_string())
}

fn main() {
set_empty_env_var_with_git!(
"HAX_GIT_DESCRIBE",
["describe", "--tags", "--always", "--abbrev=0"]
);
set_empty_env_var_with_git!("HAX_GIT_COMMIT_HASH", ["rev-parse", "HEAD"]);
let commit_hash =
set_empty_env_var_with!("HAX_GIT_COMMIT_HASH", git_command(&["rev-parse", "HEAD"]));

set_empty_env_var_with!("HAX_VERSION", {
if commit_hash == UNKNOWN {
env!("CARGO_PKG_VERSION").into()
} else {
git_command(&["tag", "--contains", &commit_hash])
.lines()
.next()
.and_then(|tag| tag.split_once("hax-v"))
.map(|(_, version)| version.trim().to_string())
.unwrap_or_else(|| format!("untagged-git-rev-{}", &commit_hash[0..10]))
}
});
}
9 changes: 8 additions & 1 deletion hax-types/src/cli_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,14 @@ pub enum ExportBodyKind {

#[derive_group(Serializers)]
#[derive(JsonSchema, Parser, Debug, Clone)]
#[command(author, version = concat!("commit=", env!("HAX_GIT_COMMIT_HASH"), " ", "describe=", env!("HAX_GIT_DESCRIBE")), name = "hax", about, long_about = None)]
#[command(
author,
version = crate::HAX_VERSION,
long_version = concat!("\nversion=", env!("HAX_VERSION"), "\n", "commit=", env!("HAX_GIT_COMMIT_HASH")),
name = "hax",
about,
long_about = None
)]
pub struct ExtensibleOptions<E: Extension> {
/// Replace the expansion of each macro matching PATTERN by their
/// invocation. PATTERN denotes a rust path (i.e. `A::B::c`) in
Expand Down
3 changes: 3 additions & 0 deletions hax-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ pub mod driver_api;
/// The types used to communicate between `cargo-hax` and
/// `hax-engine`.
pub mod engine_api;

/// Compile-time version of hax
pub const HAX_VERSION: &str = env!("HAX_VERSION");

0 comments on commit f540bf7

Please sign in to comment.