Move $BWRS_VERSION fallback into build.rs

This commit is contained in:
Jake Howard 2022-01-05 21:18:24 +00:00
parent 743ef74b30
commit 40ae81dd3c
No known key found for this signature in database
GPG Key ID: 57AFB45680EDD477
2 changed files with 19 additions and 30 deletions

View File

@ -15,11 +15,14 @@ fn main() {
"You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite" "You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
); );
if let Ok(version) = env::var("VW_VERSION") { // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
// If neither exist, read from git.
let maybe_vaultwarden_version =
env::var("VW_VERSION").or(env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
if let Ok(version) = maybe_vaultwarden_version {
println!("cargo:rustc-env=VW_VERSION={}", version); println!("cargo:rustc-env=VW_VERSION={}", version);
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version); println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
} else {
read_git_info().ok();
} }
} }
@ -33,7 +36,13 @@ fn run(args: &[&str]) -> Result<String, std::io::Error> {
} }
/// This method reads info from Git, namely tags, branch, and revision /// This method reads info from Git, namely tags, branch, and revision
fn read_git_info() -> Result<(), std::io::Error> { /// To access these values, use:
/// - env!("GIT_EXACT_TAG")
/// - env!("GIT_LAST_TAG")
/// - env!("GIT_BRANCH")
/// - env!("GIT_REV")
/// - env!("VW_VERSION")
fn version_from_git_info() -> Result<String, std::io::Error> {
// The exact tag for the current commit, can be empty when // The exact tag for the current commit, can be empty when
// the current commit doesn't have an associated tag // the current commit doesn't have an associated tag
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok(); let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
@ -56,23 +65,11 @@ fn read_git_info() -> Result<(), std::io::Error> {
println!("cargo:rustc-env=GIT_REV={}", rev_short); println!("cargo:rustc-env=GIT_REV={}", rev_short);
// Combined version // Combined version
let version = if let Some(exact) = exact_tag { if let Some(exact) = exact_tag {
exact Ok(exact)
} else if &branch != "main" && &branch != "master" { } else if &branch != "main" && &branch != "master" {
format!("{}-{} ({})", last_tag, rev_short, branch) Ok(format!("{}-{} ({})", last_tag, rev_short, branch))
} else { } else {
format!("{}-{}", last_tag, rev_short) Ok(format!("{}-{}", last_tag, rev_short))
}; }
println!("cargo:rustc-env=VW_VERSION={}", version);
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
// To access these values, use:
// env!("GIT_EXACT_TAG")
// env!("GIT_LAST_TAG")
// env!("GIT_BRANCH")
// env!("GIT_REV")
// env!("VW_VERSION")
Ok(())
} }

View File

@ -76,15 +76,7 @@ const HELP: &str = "\
-v, --version Prints the app version -v, --version Prints the app version
"; ";
// HACK: Option::or cannot be used in a constant context pub const VERSION: Option<&str> = option_env!("VW_VERSION");
const fn get_version() -> Option<&'static str> {
let bwrs_version = option_env!("BWRS_VERSION");
match bwrs_version {
Some(_) => bwrs_version,
None => option_env!("VW_VERSION"),
}
}
pub const VERSION: Option<&str> = get_version();
fn parse_args() { fn parse_args() {
let mut pargs = pico_args::Arguments::from_env(); let mut pargs = pico_args::Arguments::from_env();