diff options
Diffstat (limited to 'build.rs')
-rw-r--r-- | build.rs | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -12,9 +12,7 @@ fn create_dir(dir: &Path) -> io::Result<()> { match &res { Ok(()) => {} Err(err) => { - eprintln!( - "Failed to create a directory at location {dir:?}, encountered error {err:?}. Aborting...", - ); + eprintln!("Failed to create a directory at location {dir:?}, encountered error {err:?}. Aborting...",); } } @@ -58,6 +56,14 @@ fn btm_generate() -> io::Result<()> { Ok(()) } +fn extract_sha(sha: Option<&str>) -> Option<&str> { + sha.and_then(|sha: &str| sha.get(0..8)) +} + +fn output_nightly_version(version: &str, git_hash: &str) { + println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}"); +} + fn nightly_version() { const ENV_KEY: &str = "BTM_BUILD_RELEASE_CALLER"; @@ -65,20 +71,20 @@ fn nightly_version() { Some(var) if !var.is_empty() && var == "nightly" => { let version = env!("CARGO_PKG_VERSION"); - if let Some(git_hash) = option_env!("CIRRUS_CHANGE_IN_REPO") - .and_then(|cirrus_sha: &str| cirrus_sha.get(0..8)) - { - println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}"); - } else if let Some(git_hash) = - option_env!("GITHUB_SHA").and_then(|gha_sha: &str| gha_sha.get(0..8)) - { - println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}"); + if let Some(hash) = extract_sha(option_env!("CIRRUS_CHANGE_IN_REPO")) { + // May be set if we're building with Cirrus CI. + output_nightly_version(version, hash); + } else if let Some(hash) = extract_sha(option_env!("GITHUB_SHA")) { + // May be set if we're building with GHA. + output_nightly_version(version, hash); } else if let Ok(output) = std::process::Command::new("git") .args(["rev-parse", "--short=8", "HEAD"]) .output() { - let git_hash = String::from_utf8(output.stdout).unwrap(); - println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}"); + // If we're not building in either, we do the lazy thing and fall back to + // manually grabbing info using git as a command. + let hash = String::from_utf8(output.stdout).unwrap(); + output_nightly_version(version, &hash); } } _ => {} |