other: clean up some build script code (#1218)

Some build script formatting fixes and cleanup of some code. In
particular, I found some of the nightly version handling code to look
pretty gross so I separated out the parts into functions to clean it up
a bit.
This commit is contained in:
Clement Tsang 2023-06-20 05:52:54 +00:00 committed by GitHub
parent 9e97680131
commit 76e81df715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}
}
_ => {}