substrate_build_script_utils/
version.rs1use std::{borrow::Cow, process::Command};
19
20pub fn generate_cargo_keys() {
22 let commit = if let Ok(hash) = std::env::var("SUBSTRATE_CLI_GIT_COMMIT_HASH") {
23 Cow::from(hash.trim().to_owned())
24 } else {
25 match Command::new("git").args(["rev-parse", "--short=11", "HEAD"]).output() {
29 Ok(o) if o.status.success() => {
30 let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
31 Cow::from(sha)
32 },
33 Ok(o) => {
34 let stderr = String::from_utf8_lossy(&o.stderr).trim().to_owned();
35 println!(
36 "cargo:warning=Git command failed with status '{}' with message: '{}'",
37 o.status, stderr,
38 );
39 Cow::from("unknown")
40 },
41 Err(err) => {
42 println!("cargo:warning=Failed to execute git command: {}", err);
43 Cow::from("unknown")
44 },
45 }
46 };
47
48 println!("cargo:rustc-env=SUBSTRATE_CLI_COMMIT_HASH={commit}");
49 println!("cargo:rustc-env=SUBSTRATE_CLI_IMPL_VERSION={}", get_version(&commit))
50}
51
52fn get_version(impl_commit: &str) -> String {
53 let commit_dash = if impl_commit.is_empty() { "" } else { "-" };
54
55 format!(
56 "{}{}{}",
57 std::env::var("CARGO_PKG_VERSION").unwrap_or_default(),
58 commit_dash,
59 impl_commit
60 )
61}