From b834a9474cd130a559ea7dd6e7556082cb19a4f1 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Thu, 17 Oct 2024 13:59:59 +0200 Subject: [PATCH 1/2] wip: add hardcoded version string --- crates/bitwarden-wasm-internal/src/client.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index 0a5ce27d5..f85c2c5db 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -49,6 +49,10 @@ impl BitwardenClient { msg } + pub fn version(&self) -> String { + "PM-12989-create-process-for-qa-to-build-client-with-particular-sdk-version".to_owned() + } + pub fn throw(&self, msg: String) -> Result<(), crate::error::GenericError> { Err(crate::error::GenericError(msg)) } From d9f4fad175862369bdd3447fb1174d32b63a9515 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Mon, 21 Oct 2024 11:26:21 +0200 Subject: [PATCH 2/2] feat: implement dynamic build-version variable --- crates/bitwarden-wasm-internal/build.rs | 52 ++++++++++++++++++++ crates/bitwarden-wasm-internal/src/client.rs | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 crates/bitwarden-wasm-internal/build.rs diff --git a/crates/bitwarden-wasm-internal/build.rs b/crates/bitwarden-wasm-internal/build.rs new file mode 100644 index 000000000..eca07053b --- /dev/null +++ b/crates/bitwarden-wasm-internal/build.rs @@ -0,0 +1,52 @@ +use std::{env, process::Command}; + +fn main() { + // Use the SDK_VERSION environment variable if it is set (e.g. by CI) or get it from Git + let sdk_version = env::var("SDK_VERSION") + .or_else(|_| version_from_git_info()) + .unwrap_or("unknown".to_string()); + + println!("cargo:rustc-env=SDK_VERSION={sdk_version}"); + println!("cargo:rustc-env=CARGO_PKG_VERSION={sdk_version}"); +} + +fn run(args: &[&str]) -> Result { + let out = Command::new(args[0]).args(&args[1..]).output()?; + if !out.status.success() { + use std::io::{Error, ErrorKind}; + return Err(Error::new(ErrorKind::Other, "Command not successful")); + } + Ok(String::from_utf8(out.stdout).unwrap().trim().to_string()) +} + +/// This method reads info from Git, namely tags, branch, and revision +/// To access these values, use: +/// - `env!("GIT_EXACT_TAG")` +/// - `env!("GIT_BRANCH")` +/// - `env!("GIT_REV")` +fn version_from_git_info() -> Result { + // The exact tag for the current commit, can be empty when + // the current commit doesn't have an associated tag + let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok(); + if let Some(ref exact) = exact_tag { + println!("cargo:rustc-env=GIT_EXACT_TAG={exact}"); + } + + // The current branch name + let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?; + println!("cargo:rustc-env=GIT_BRANCH={branch}"); + + // The current git commit hash + let rev = run(&["git", "rev-parse", "HEAD"])?; + let rev_short = rev.get(..8).unwrap_or_default(); + println!("cargo:rustc-env=GIT_REV={rev_short}"); + + // Combined version + if let Some(exact) = exact_tag { + Ok(exact) + } else if &branch != "main" && &branch != "master" && &branch != "HEAD" { + Ok(format!("{rev_short} ({branch})")) + } else { + Ok(format!("{rev_short}")) + } +} diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index f85c2c5db..a824b427f 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -50,7 +50,7 @@ impl BitwardenClient { } pub fn version(&self) -> String { - "PM-12989-create-process-for-qa-to-build-client-with-particular-sdk-version".to_owned() + env!("SDK_VERSION").to_owned() } pub fn throw(&self, msg: String) -> Result<(), crate::error::GenericError> {