|
1 |
| -use std::{borrow::Cow, process::Command}; |
| 1 | +use std::{borrow::Cow, env, process::Command}; |
2 | 2 |
|
3 | 3 | /// Generate the `cargo:` key output
|
4 | 4 | pub fn generate_cargo_keys() {
|
5 |
| - let commit = if let Ok(hash) = std::env::var("CLI_GIT_COMMIT_HASH") { |
6 |
| - Cow::from(hash.trim().to_owned()) |
7 |
| - } else { |
8 |
| - // We deliberately set the length here to `11` to ensure that |
9 |
| - // the emitted hash is always of the same length; otherwise |
10 |
| - // it can (and will!) vary between different build environments. |
11 |
| - match Command::new("git") |
12 |
| - .args(["rev-parse", "--short=11", "HEAD"]) |
13 |
| - .output() |
14 |
| - { |
15 |
| - Ok(o) if o.status.success() => { |
16 |
| - let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned(); |
17 |
| - Cow::from(sha) |
18 |
| - } |
19 |
| - Ok(o) => { |
20 |
| - let stderr = String::from_utf8_lossy(&o.stderr).trim().to_owned(); |
21 |
| - println!( |
22 |
| - "cargo:warning=Git command failed with status '{}' with message: '{}'", |
23 |
| - o.status, stderr, |
24 |
| - ); |
25 |
| - Cow::from("unknown") |
26 |
| - } |
27 |
| - Err(err) => { |
28 |
| - println!("cargo:warning=Failed to execute git command: {}", err); |
29 |
| - Cow::from("unknown") |
30 |
| - } |
| 5 | + let output = Command::new("git") |
| 6 | + .args(["rev-parse", "--short", "HEAD"]) |
| 7 | + .output(); |
| 8 | + |
| 9 | + let commit = match output { |
| 10 | + Ok(o) if o.status.success() => { |
| 11 | + let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned(); |
| 12 | + Cow::from(sha) |
| 13 | + } |
| 14 | + Ok(o) => { |
| 15 | + println!("cargo:warning=Git command failed with status: {}", o.status); |
| 16 | + Cow::from("unknown") |
| 17 | + } |
| 18 | + Err(err) => { |
| 19 | + println!("cargo:warning=Failed to execute git command: {}", err); |
| 20 | + Cow::from("unknown") |
31 | 21 | }
|
32 | 22 | };
|
33 | 23 |
|
34 |
| - println!("cargo:rustc-env=CLI_COMMIT_HASH={commit}"); |
35 |
| - println!("cargo:rustc-env=CLI_IMPL_VERSION={}", get_version(&commit)) |
| 24 | + println!("cargo:rustc-env=APP_VERSION={}", get_version(&commit)) |
| 25 | +} |
| 26 | + |
| 27 | +fn get_platform() -> String { |
| 28 | + let env_dash = if env::var("CARGO_CFG_TARGET_ENV").unwrap().is_empty() { |
| 29 | + "" |
| 30 | + } else { |
| 31 | + "-" |
| 32 | + }; |
| 33 | + |
| 34 | + format!( |
| 35 | + "{}-{}{}{}", |
| 36 | + env::var("CARGO_CFG_TARGET_ARCH").unwrap(), |
| 37 | + env::var("CARGO_CFG_TARGET_OS").unwrap(), |
| 38 | + env_dash, |
| 39 | + env::var("CARGO_CFG_TARGET_ENV").unwrap_or(String::from("")), |
| 40 | + ) |
36 | 41 | }
|
37 | 42 |
|
38 | 43 | fn get_version(impl_commit: &str) -> String {
|
39 | 44 | let commit_dash = if impl_commit.is_empty() { "" } else { "-" };
|
40 | 45 |
|
41 | 46 | format!(
|
42 |
| - "{}{}{}", |
| 47 | + "{}{}{}-{}", |
43 | 48 | std::env::var("CARGO_PKG_VERSION").unwrap_or_default(),
|
44 | 49 | commit_dash,
|
45 |
| - impl_commit |
| 50 | + impl_commit, |
| 51 | + get_platform(), |
46 | 52 | )
|
47 | 53 | }
|
48 | 54 |
|
|
0 commit comments