Skip to content

Commit 64970a5

Browse files
committed
Add --version command
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 2c73ee2 commit 64970a5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

build.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
let git_hash = Command::new("git")
5+
.args(["describe", "--always", "--dirty"])
6+
.output()
7+
.map(|output| {
8+
if output.status.success() {
9+
String::from_utf8_lossy(&output.stdout).trim().to_string()
10+
} else {
11+
"unknown".to_string()
12+
}
13+
})
14+
.unwrap_or_else(|_| "unknown".to_string());
15+
16+
let git_date = Command::new("git")
17+
.args(["log", "-1", "--format=%cs"])
18+
.output()
19+
.map(|output| {
20+
if output.status.success() {
21+
String::from_utf8_lossy(&output.stdout).trim().to_string()
22+
} else {
23+
"unknown".to_string()
24+
}
25+
})
26+
.unwrap_or_else(|_| "unknown".to_string());
27+
28+
// Make the git info available to the program
29+
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
30+
println!("cargo:rustc-env=GIT_DATE={}", git_date);
31+
32+
// Re-run build script if git HEAD changes
33+
println!("cargo:rerun-if-changed=.git/HEAD");
34+
}

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ use std::env;
22

33
use cargo_hyperlight::cargo;
44

5+
const VERSION: &str = env!("CARGO_PKG_VERSION");
6+
const GIT_HASH: &str = env!("GIT_HASH");
7+
const GIT_DATE: &str = env!("GIT_DATE");
8+
59
fn main() {
10+
if env::args().any(|arg| arg == "--version" || arg == "-V") {
11+
println!("cargo-hyperlight {} ({} {})", VERSION, GIT_HASH, GIT_DATE);
12+
return;
13+
}
14+
615
let args = env::args_os().enumerate().filter_map(|(i, arg)| {
716
// skip the binary name and the "hyperlight" subcommand if present
817
if i == 0 || (i == 1 && arg == "hyperlight") {

0 commit comments

Comments
 (0)