| 1 |
use std::borrow::Cow; |
| 2 |
use std::path::Path; |
| 3 |
use std::process::Command; |
| 4 |
|
| 5 |
const MAJOR: &str = env!("CARGO_PKG_VERSION_MAJOR"); |
| 6 |
const MINOR: &str = env!("CARGO_PKG_VERSION_MINOR"); |
| 7 |
const PATCH: &str = env!("CARGO_PKG_VERSION_PATCH"); |
| 8 |
|
| 9 |
fn main() { |
| 10 |
let git_hash = Command::new("git") |
| 11 |
.args(["rev-parse", "HEAD"]) |
| 12 |
.output() |
| 13 |
.ok() |
| 14 |
.filter(|output| output.status.success()) |
| 15 |
.and_then(|x| String::from_utf8(x.stdout).ok()) |
| 16 |
.or_else(|| option_env!("HELIX_NIX_BUILD_REV").map(|s| s.to_string())); |
| 17 |
|
| 18 |
let minor = if MINOR.len() == 1 { |
| 19 |
|
| 20 |
format!("0{MINOR}") |
| 21 |
} else { |
| 22 |
MINOR.to_string() |
| 23 |
}; |
| 24 |
let calver = if PATCH == "0" { |
| 25 |
format!("{MAJOR}.{minor}") |
| 26 |
} else { |
| 27 |
format!("{MAJOR}.{minor}.{PATCH}") |
| 28 |
}; |
| 29 |
let version: Cow<_> = match &git_hash { |
| 30 |
Some(git_hash) => format!("{} ({})", calver, &git_hash[..8]).into(), |
| 31 |
None => calver.into(), |
| 32 |
}; |
| 33 |
|
| 34 |
println!( |
| 35 |
"cargo:rustc-env=BUILD_TARGET={}", |
| 36 |
std::env::var("TARGET").unwrap() |
| 37 |
); |
| 38 |
|
| 39 |
println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version); |
| 40 |
|
| 41 |
if git_hash.is_none() { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
let Some(git_dir): Option<String> = Command::new("git") |
| 51 |
.args(["rev-parse", "--git-dir"]) |
| 52 |
.output() |
| 53 |
.ok() |
| 54 |
.filter(|output| output.status.success()) |
| 55 |
.and_then(|x| String::from_utf8(x.stdout).ok()) |
| 56 |
.map(|x| x.trim().to_string()) |
| 57 |
else { |
| 58 |
return; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
let head = Path::new(&git_dir).join("HEAD"); |
| 63 |
if head.exists() { |
| 64 |
println!("cargo:rerun-if-changed={}", head.display()); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
let Some(head_ref): Option<String> = Command::new("git") |
| 69 |
.args(["symbolic-ref", "HEAD"]) |
| 70 |
.output() |
| 71 |
.ok() |
| 72 |
.filter(|output| output.status.success()) |
| 73 |
.and_then(|x| String::from_utf8(x.stdout).ok()) |
| 74 |
.map(|x| x.trim().to_string()) |
| 75 |
else { |
| 76 |
return; |
| 77 |
}; |
| 78 |
let head_ref = Path::new(&git_dir).join(head_ref); |
| 79 |
if head_ref.exists() { |
| 80 |
println!("cargo:rerun-if-changed={}", head_ref.display()); |
| 81 |
} |
| 82 |
} |