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