Avoid git version on release builds (#253)

This commit is contained in:
Ajeet D'Souza 2021-08-18 21:55:25 +05:30 committed by GitHub
parent 75310ba069
commit bc57339473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,27 @@ use std::env;
use std::io; use std::io;
use std::process::Command; use std::process::Command;
fn main() {
let pkg_version = env::var("CARGO_PKG_VERSION").unwrap();
let version = match env::var_os("PROFILE") {
Some(profile) if profile == "release" => format!("v{}", pkg_version),
_ => git_version().unwrap_or_else(|| format!("v{}-unknown", pkg_version)),
};
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version);
// Since we are generating completions in the package directory, we need to
// set this so that Cargo doesn't rebuild every time.
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=templates");
println!("cargo:rerun-if-changed=tests");
generate_completions().unwrap();
}
fn git_version() -> Option<String> { fn git_version() -> Option<String> {
let dir = env::var("CARGO_MANIFEST_DIR").ok()?;
let mut git = Command::new("git"); let mut git = Command::new("git");
git.args(&["describe", "--tags", "--broken"]); git.args(&["-C", &dir, "describe", "--tags", "--broken"]);
let output = git.output().ok()?; let output = git.output().ok()?;
if !output.status.success() || output.stdout.is_empty() || !output.stderr.is_empty() { if !output.status.success() || output.stdout.is_empty() || !output.stderr.is_empty() {
@ -13,11 +31,6 @@ fn git_version() -> Option<String> {
String::from_utf8(output.stdout).ok() String::from_utf8(output.stdout).ok()
} }
fn crate_version() -> String {
// unwrap is safe here, since Cargo will always supply this variable.
format!("v{}", env::var("CARGO_PKG_VERSION").unwrap())
}
fn generate_completions() -> io::Result<()> { fn generate_completions() -> io::Result<()> {
#[path = "src/app/_app.rs"] #[path = "src/app/_app.rs"]
mod app; mod app;
@ -39,20 +52,3 @@ fn generate_completions() -> io::Result<()> {
Ok(()) Ok(())
} }
fn main() {
// Packaged releases of zoxide almost always use the source tarball
// provided by GitHub, which does not include the `.git` folder. Since this
// feature is only useful for development, we can silently fall back to
// using the crate version.
let version = git_version().unwrap_or_else(crate_version);
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version);
// Since we are generating completions in the package directory, we need to
// set this so that Cargo doesn't rebuild every time.
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=templates");
println!("cargo:rerun-if-changed=tests");
generate_completions().unwrap();
}