|
| 1 | +use std::env; |
1 | 2 | use std::process::Command;
|
2 | 3 |
|
3 | 4 | const GRAPHITE_RELEASE_SERIES: &str = "Alpha 4";
|
4 | 5 |
|
5 | 6 | fn main() {
|
6 |
| - // Execute a Git command for its stdout. Early exit if it fails for any of the possible reasons. |
7 |
| - let try_git_command = |args: &[&str]| -> Option<String> { |
8 |
| - let git_output = Command::new("git").args(args).output().ok()?; |
9 |
| - let maybe_empty = String::from_utf8(git_output.stdout).ok()?; |
10 |
| - let command_result = (!maybe_empty.is_empty()).then_some(maybe_empty)?; |
11 |
| - Some(command_result) |
12 |
| - }; |
13 |
| - // Execute a Git command for its output. Return "unknown" if it fails for any of the possible reasons. |
14 |
| - let git_command = |args| -> String { try_git_command(args).unwrap_or_else(|| String::from("unknown")) }; |
15 |
| - |
16 |
| - // Rather than printing to any terminal, these commands set environment variables in the Cargo toolchain. |
17 |
| - // They are accessed with the `env!("...")` macro in the codebase. |
18 |
| - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={}", git_command(&["log", "-1", "--format=%cd"])); |
19 |
| - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={}", git_command(&["rev-parse", "HEAD"])); |
20 |
| - let branch = std::env::var("GITHUB_HEAD_REF").unwrap_or_default(); |
21 |
| - let branch = if branch.is_empty() { git_command(&["name-rev", "--name-only", "HEAD"]) } else { branch }; |
22 |
| - println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={branch}"); |
| 7 | + // Instruct Cargo to rerun this build script if any of these environment variables change. |
| 8 | + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_DATE"); |
| 9 | + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_HASH"); |
| 10 | + println!("cargo:rerun-if-env-changed=GRAPHITE_GIT_COMMIT_BRANCH"); |
| 11 | + println!("cargo:rerun-if-env-changed=GITHUB_HEAD_REF"); |
| 12 | + |
| 13 | + // Instruct Cargo to rerun this build script if the Git HEAD or refs change. |
| 14 | + println!("cargo:rerun-if-changed=.git/HEAD"); |
| 15 | + println!("cargo:rerun-if-changed=.git/refs/heads"); |
| 16 | + |
| 17 | + // Try to get the commit information from the environment (e.g. set by CI), otherwise fall back to Git commands. |
| 18 | + let commit_date = env_or_else("GRAPHITE_GIT_COMMIT_DATE", || git_or_unknown(&["log", "-1", "--format=%cI"])); |
| 19 | + let commit_hash = env_or_else("GRAPHITE_GIT_COMMIT_HASH", || git_or_unknown(&["rev-parse", "HEAD"])); |
| 20 | + let commit_branch = env_or_else("GRAPHITE_GIT_COMMIT_BRANCH", || { |
| 21 | + let gh = env::var("GITHUB_HEAD_REF").unwrap_or_default(); |
| 22 | + if !gh.trim().is_empty() { |
| 23 | + gh.trim().to_string() |
| 24 | + } else { |
| 25 | + git_or_unknown(&["rev-parse", "--abbrev-ref", "HEAD"]) |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + // Instruct Cargo to set environment variables for compile time. |
| 30 | + // They are accessed with the `env!("GRAPHITE_*")` macro in the codebase. |
| 31 | + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_DATE={commit_date}"); |
| 32 | + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_HASH={commit_hash}"); |
| 33 | + println!("cargo:rustc-env=GRAPHITE_GIT_COMMIT_BRANCH={commit_branch}"); |
23 | 34 | println!("cargo:rustc-env=GRAPHITE_RELEASE_SERIES={GRAPHITE_RELEASE_SERIES}");
|
24 | 35 | }
|
| 36 | + |
| 37 | +/// Get an environment variable, or if it is not set or empty, use the provided fallback function. Returns a string with trimmed whitespace. |
| 38 | +fn env_or_else(key: &str, fallback: impl FnOnce() -> String) -> String { |
| 39 | + match env::var(key) { |
| 40 | + Ok(v) if !v.trim().is_empty() => v.trim().to_string(), |
| 41 | + _ => fallback().trim().to_string(), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Execute a Git command to obtain its output. Return "unknown" if it fails for any of the possible reasons. |
| 46 | +fn git_or_unknown(args: &[&str]) -> String { |
| 47 | + git(args).unwrap_or_else(|| "unknown".to_string()) |
| 48 | +} |
| 49 | + |
| 50 | +/// Run a git command and capture trimmed stdout. |
| 51 | +/// Returns None if git is missing, exits with error, or stdout is empty/non-UTF8. |
| 52 | +fn git(args: &[&str]) -> Option<String> { |
| 53 | + let output = Command::new("git").args(args).output().ok()?; |
| 54 | + if !output.status.success() { |
| 55 | + return None; |
| 56 | + } |
| 57 | + let s = String::from_utf8(output.stdout).ok()?; |
| 58 | + let t = s.trim(); |
| 59 | + if t.is_empty() { None } else { Some(t.to_string()) } |
| 60 | +} |
0 commit comments