|
| 1 | +use std::env; |
| 2 | +use std::process::Command; |
| 3 | + |
| 4 | +fn main() { |
| 5 | + println!( |
| 6 | + "cargo:rustc-env=CARGO_PKG_VERSION={}", |
| 7 | + env!("CARGO_PKG_VERSION") |
| 8 | + ); |
| 9 | + |
| 10 | + let build_time = std::time::SystemTime::now() |
| 11 | + .duration_since(std::time::UNIX_EPOCH) |
| 12 | + .unwrap() |
| 13 | + .as_secs(); |
| 14 | + let build_time_str = format!("{}", build_time); |
| 15 | + println!("cargo:rustc-env=BUILD_TIME={}", build_time_str); |
| 16 | + |
| 17 | + let git_commit = get_git_commit_hash(); |
| 18 | + println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit); |
| 19 | + |
| 20 | + let git_branch = get_git_branch(); |
| 21 | + println!("cargo:rustc-env=GIT_BRANCH={}", git_branch); |
| 22 | + |
| 23 | + let git_dirty = get_git_dirty(); |
| 24 | + println!("cargo:rustc-env=GIT_DIRTY={}", git_dirty); |
| 25 | + |
| 26 | + println!("cargo:rerun-if-changed=Cargo.toml"); |
| 27 | + println!("cargo:rerun-if-changed=.git/HEAD"); |
| 28 | + println!("cargo:rerun-if-changed=.git/index"); |
| 29 | +} |
| 30 | + |
| 31 | +fn get_git_commit_hash() -> String { |
| 32 | + Command::new("git") |
| 33 | + .args(["rev-parse", "--short", "HEAD"]) |
| 34 | + .output() |
| 35 | + .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string()) |
| 36 | + .unwrap_or_else(|_| "unknown".to_string()) |
| 37 | +} |
| 38 | + |
| 39 | +fn get_git_branch() -> String { |
| 40 | + Command::new("git") |
| 41 | + .args(["rev-parse", "--abbrev-ref", "HEAD"]) |
| 42 | + .output() |
| 43 | + .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string()) |
| 44 | + .unwrap_or_else(|_| "unknown".to_string()) |
| 45 | +} |
| 46 | + |
| 47 | +fn get_git_dirty() -> String { |
| 48 | + Command::new("git") |
| 49 | + .args(["diff", "--quiet", "--ignore-submodules"]) |
| 50 | + .output() |
| 51 | + .map(|output| { |
| 52 | + if output.status.success() { |
| 53 | + "clean" |
| 54 | + } else { |
| 55 | + "dirty" |
| 56 | + } |
| 57 | + }) |
| 58 | + .unwrap_or_else(|_| "unknown") |
| 59 | + .to_string() |
| 60 | +} |
0 commit comments