Skip to content

Commit 08d6640

Browse files
authored
Expose version info in CLI tool with build-time obtained git hash (paritytech#787)
* version info with built-time obtained git hash * clippy * rerun-if-changed properly and handle git command failing * cargo fmt
1 parent c9f414f commit 08d6640

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ subxt-codegen = { version = "0.25.0", path = "../codegen" }
2121
# perform node compatibility
2222
subxt-metadata = { version = "0.25.0", path = "../metadata" }
2323
# parse command line args
24-
clap = { version = "4.0.8", features = ["derive"] }
24+
clap = { version = "4.0.8", features = ["derive", "cargo"] }
2525
# colourful error reports
2626
color-eyre = "0.6.1"
2727
# serialize the metadata

cli/build.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::{
2+
borrow::Cow,
3+
process::Command,
4+
};
5+
6+
fn main() {
7+
// Make git hash available via GIT_HASH build-time env var:
8+
output_git_short_hash();
9+
}
10+
11+
fn output_git_short_hash() {
12+
let output = Command::new("git")
13+
.args(["rev-parse", "--short=11", "HEAD"])
14+
.output();
15+
16+
let git_hash = match output {
17+
Ok(o) if o.status.success() => {
18+
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
19+
Cow::from(sha)
20+
}
21+
Ok(o) => {
22+
println!("cargo:warning=Git command failed with status: {}", o.status);
23+
Cow::from("unknown")
24+
}
25+
Err(err) => {
26+
println!("cargo:warning=Failed to execute git command: {}", err);
27+
Cow::from("unknown")
28+
}
29+
};
30+
31+
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
32+
println!("cargo:rerun-if-changed=../.git/HEAD");
33+
println!("cargo:rerun-if-changed=../.git/refs");
34+
println!("cargo:rerun-if-changed=build.rs");
35+
}

cli/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
pub mod codegen;
66
pub mod compatibility;
77
pub mod metadata;
8+
pub mod version;

cli/src/commands/version.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use clap::Parser as ClapParser;
2+
3+
/// Prints version information
4+
#[derive(Debug, ClapParser)]
5+
pub struct Opts {}
6+
7+
pub fn run(_opts: Opts) -> color_eyre::Result<()> {
8+
let git_hash = env!("GIT_HASH");
9+
println!(
10+
"{} {}-{}",
11+
clap::crate_name!(),
12+
clap::crate_version!(),
13+
git_hash
14+
);
15+
Ok(())
16+
}

cli/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum Command {
1313
Metadata(commands::metadata::Opts),
1414
Codegen(commands::codegen::Opts),
1515
Compatibility(commands::compatibility::Opts),
16+
Version(commands::version::Opts),
1617
}
1718

1819
#[tokio::main]
@@ -24,5 +25,6 @@ async fn main() -> color_eyre::Result<()> {
2425
Command::Metadata(opts) => commands::metadata::run(opts).await,
2526
Command::Codegen(opts) => commands::codegen::run(opts).await,
2627
Command::Compatibility(opts) => commands::compatibility::run(opts).await,
28+
Command::Version(opts) => commands::version::run(opts),
2729
}
2830
}

0 commit comments

Comments
 (0)