-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): better version information
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
Showing
3 changed files
with
49 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters