diff options
| author | Linus Färnstrand <faern@faern.net> | 2022-10-04 16:23:25 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2022-10-10 11:55:56 +0200 |
| commit | 7fbca24356da28bf97523090f3e93729e52bbb92 (patch) | |
| tree | 68d23cf29d2ce93a753757590893b7a65ce3cd27 | |
| parent | 48235c30777a865eeef23ed7011bad8e2ece1d0d (diff) | |
| download | mullvadvpn-7fbca24356da28bf97523090f3e93729e52bbb92.tar.xz mullvadvpn-7fbca24356da28bf97523090f3e93729e52bbb92.zip | |
Make Android fetch versionName and versionCode from mullvad-version
| -rw-r--r-- | android/app/build.gradle.kts | 11 | ||||
| -rw-r--r-- | android/buildSrc/src/main/kotlin/Utils.kt | 20 | ||||
| -rwxr-xr-x | build-apk.sh | 20 | ||||
| -rw-r--r-- | dist-assets/android-product-version.txt | 1 | ||||
| -rw-r--r-- | mullvad-version/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-version/build.rs | 82 | ||||
| -rw-r--r-- | mullvad-version/src/main.rs | 32 | ||||
| -rwxr-xr-x | version-metadata.sh | 34 |
8 files changed, 103 insertions, 99 deletions
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 44ea1f7f32..265e0666c0 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -28,8 +28,8 @@ android { applicationId = "net.mullvad.mullvadvpn" minSdkVersion(Versions.Android.minSdkVersion) targetSdkVersion(Versions.Android.targetSdkVersion) - versionCode = 22020002 - versionName = "2022.2-beta2" + versionCode = generateVersionCode() + versionName = generateVersionName() testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } @@ -162,6 +162,13 @@ tasks.register("ensureJniDirectoryExist") { } } +tasks.create("printVersion") { + doLast { + println("versionCode=${project.android.defaultConfig.versionCode}") + println("versionName=${project.android.defaultConfig.versionName}") + } +} + play { serviceAccountCredentials = file("play-api-key.json") } diff --git a/android/buildSrc/src/main/kotlin/Utils.kt b/android/buildSrc/src/main/kotlin/Utils.kt new file mode 100644 index 0000000000..96c44b78c2 --- /dev/null +++ b/android/buildSrc/src/main/kotlin/Utils.kt @@ -0,0 +1,20 @@ +import java.io.ByteArrayOutputStream +import org.gradle.api.Project +import org.gradle.process.ExecSpec + +fun Project.execWithOutput(spec: ExecSpec.() -> Unit) = + ByteArrayOutputStream().use { outputStream -> + exec { + this.spec() + this.standardOutput = outputStream + } + outputStream.toString().trim() + } + +fun Project.generateVersionCode() = execWithOutput { + commandLine("cargo", "run", "-q", "--bin", "mullvad-version", "versionCode") +}.toInt() + +fun Project.generateVersionName() = execWithOutput { + commandLine("cargo", "run", "-q", "--bin", "mullvad-version", "versionName") +} diff --git a/build-apk.sh b/build-apk.sh index 984f892268..9e906d97c0 100755 --- a/build-apk.sh +++ b/build-apk.sh @@ -6,7 +6,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd "$SCRIPT_DIR" -PRODUCT_VERSION="$(sed -n -e 's/^ *versionName = "\([^"]*\)"$/\1/p' android/app/build.gradle.kts)" +PRODUCT_VERSION=$(cargo run -q --bin mullvad-version versionName) BUILD_TYPE="release" GRADLE_BUILD_TYPE="release" GRADLE_TASK="assembleRelease" @@ -50,12 +50,7 @@ if [[ "$GRADLE_BUILD_TYPE" == "release" ]]; then fi fi -product_version_commit_hash=$(git rev-parse android/$PRODUCT_VERSION^{commit} || echo "") -current_head_commit_hash=$(git rev-parse HEAD^{commit}) -if [[ "$BUILD_TYPE" == "debug" || $product_version_commit_hash != $current_head_commit_hash ]]; then - PRODUCT_VERSION="${PRODUCT_VERSION}-dev-${current_head_commit_hash:0:6}" - echo "Modifying product version to $PRODUCT_VERSION" -else +if [[ "$BUILD_TYPE" == "release" && "$PRODUCT_VERSION" != *"-dev-"* ]]; then echo "Removing old Rust build artifacts" cargo clean CARGO_ARGS+=" --locked" @@ -80,17 +75,6 @@ $GRADLE_CMD --console plain clean mkdir -p "app/build/extraJni" popd -function restore_metadata_backups() { - pushd "$SCRIPT_DIR" - ./version-metadata.sh restore-backup --android - mv Cargo.lock.bak Cargo.lock || true - popd -} -trap 'restore_metadata_backups' EXIT - -cp Cargo.lock Cargo.lock.bak -./version-metadata.sh inject $PRODUCT_VERSION --android - ./wireguard/build-wireguard-go.sh --android $EXTRA_WGGO_ARGS for ARCHITECTURE in ${ARCHITECTURES:-aarch64 armv7 x86_64 i686}; do diff --git a/dist-assets/android-product-version.txt b/dist-assets/android-product-version.txt new file mode 100644 index 0000000000..8f7e21b428 --- /dev/null +++ b/dist-assets/android-product-version.txt @@ -0,0 +1 @@ +2022.2-beta2 diff --git a/mullvad-version/Cargo.toml b/mullvad-version/Cargo.toml index fc89f1d252..fdc013a261 100644 --- a/mullvad-version/Cargo.toml +++ b/mullvad-version/Cargo.toml @@ -13,5 +13,5 @@ what version string a build should have. This crate is responsible for computing and other formats. """ -[build-dependencies] +[dependencies] regex = "1.6.0" diff --git a/mullvad-version/build.rs b/mullvad-version/build.rs index 5d36717fc2..a5b8734fb9 100644 --- a/mullvad-version/build.rs +++ b/mullvad-version/build.rs @@ -1,21 +1,20 @@ -use regex::Regex; use std::{env, fs, path::PathBuf, process::Command}; /// How many characters of the git commit that should be added to the version name /// in dev builds. const GIT_HASH_DEV_SUFFIX_LEN: usize = 6; -const ANDROID_VERSION_FILE_PATH: &str = "../android/app/build.gradle.kts"; +const ANDROID_VERSION_FILE_PATH: &str = "../dist-assets/android-product-version.txt"; const DESKTOP_VERSION_FILE_PATH: &str = "../dist-assets/desktop-product-version.txt"; -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] enum Target { Android, Desktop, } impl Target { - pub fn get() -> Self { + pub fn current_target() -> Self { println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_OS"); match env::var("CARGO_CFG_TARGET_OS") .expect("CARGO_CFG_TARGET_OS should be set") @@ -29,13 +28,41 @@ impl Target { } fn main() { - let target = Target::get(); - let mut product_version = parse_current_version_from_file(&target); + let product_version = get_product_version(Target::current_target()); + let android_product_version = get_product_version(Target::Android); + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + fs::write(out_dir.join("product-version.txt"), &product_version).unwrap(); + fs::write( + out_dir.join("android-product-version.txt"), + &android_product_version, + ) + .unwrap(); +} + +/// Returns the Mullvad product version from the corresponding metadata files, +/// depending on target platform. +fn get_product_version(target: Target) -> String { + let version_file_path = match target { + Target::Android => ANDROID_VERSION_FILE_PATH, + Target::Desktop => DESKTOP_VERSION_FILE_PATH, + }; + println!("cargo:rerun-if-changed={version_file_path}"); + let version = fs::read_to_string(version_file_path) + .unwrap_or_else(|_| panic!("Failed to read {version_file_path}")) + .trim() + .to_owned(); + + let dev_suffix = get_dev_suffix(target, &version); + + format!("{version}{dev_suffix}") +} + +fn get_dev_suffix(target: Target, product_version: &str) -> String { // Compute the expected tag name for the release named `product_version` - let release_tag = match &target { + let release_tag = match target { Target::Android => format!("android/{product_version}"), - Target::Desktop => product_version.clone(), + Target::Desktop => product_version.to_owned(), }; // Get the git commit hashes for the latest release and current HEAD @@ -47,45 +74,12 @@ fn main() { // Adjust product version string accordingly. if product_version_commit_hash.as_ref() != Some(¤t_head_commit_hash) { let hash_suffix = ¤t_head_commit_hash[..GIT_HASH_DEV_SUFFIX_LEN]; - product_version = format!("{product_version}-dev-{hash_suffix}"); - } - - // TODO: Remove this and all other warnings - println!("cargo:warning=PRODUCT VERSION {product_version}"); - - let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); - fs::write(out_dir.join("product-version.txt"), &product_version).unwrap(); -} - -/// Returns the Mullvad product version from the corresponding metadata files, -/// depending on target platform. -fn parse_current_version_from_file(target: &Target) -> String { - match target { - Target::Android => { - println!("cargo:rerun-if-changed={ANDROID_VERSION_FILE_PATH}"); - get_single_capture_from_file( - ANDROID_VERSION_FILE_PATH, - Regex::new("versionName = \"([^\"]*)\"").unwrap(), - ) - } - Target::Desktop => { - println!("cargo:rerun-if-changed={DESKTOP_VERSION_FILE_PATH}"); - fs::read_to_string(DESKTOP_VERSION_FILE_PATH) - .unwrap_or_else(|_| panic!("Failed to read {DESKTOP_VERSION_FILE_PATH}")) - } + format!("-dev-{hash_suffix}") + } else { + "".to_owned() } } -/// Returns the content of the first capture group in in the single match -/// of the given `regex` over the content of the file at `path` -fn get_single_capture_from_file(path: &str, regex: Regex) -> String { - let file_content = fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path}")); - let mut capture = regex.captures_iter(&file_content); - let regex_match = capture.next().expect("failed to find version capture")[1].to_owned(); - assert!(capture.next().is_none()); - regex_match -} - /// Returns the commit hash for the commit that `git_ref` is pointing to fn git_rev_parse_commit_hash(git_ref: &str) -> Option<String> { // This is a very blunt way of making sure we run again if a tag is added or removed. diff --git a/mullvad-version/src/main.rs b/mullvad-version/src/main.rs index 2040e64e47..09f37880bb 100644 --- a/mullvad-version/src/main.rs +++ b/mullvad-version/src/main.rs @@ -1,10 +1,16 @@ +use regex::Regex; use std::{env, process::exit}; +const ANDROID_VERSION: &str = + include_str!(concat!(env!("OUT_DIR"), "/android-product-version.txt")); + fn main() { let command = env::args().nth(1); match command.as_deref() { None => println!("{}", mullvad_version::VERSION), Some("semver") => println!("{}", to_semver(mullvad_version::VERSION)), + Some("versionName") => println!("{ANDROID_VERSION}"), + Some("versionCode") => println!("{}", to_android_version_code(ANDROID_VERSION)), Some(command) => { eprintln!("Unknown command: {command}"); exit(1); @@ -24,3 +30,29 @@ fn to_semver(version: &str) -> String { format!("{version}.0{remainder}") } + +/// Takes a version in the normal Mullvad VPN app version format and returns the Android +/// `versionCode` formatted version. +/// +/// The format of the code is: YYVV00XX +/// Last two digits of the year (major) ^^ +/// Incrementing version (minor) ^^ +/// Unused ^^ +/// Beta number, 00 if stable ^^ +/// +/// # Example +/// +/// Version: 2021.34-beta5 +/// versionCode: 21340005 +fn to_android_version_code(version: &str) -> String { + let re = + Regex::new(r"^20([0-9]{2})\.([1-9][0-9]?)(-beta([1-9][0-9]?))?(-dev-[0-9a-f]+)?$").unwrap(); + let captures = re + .captures(version) + .expect("Version does not match expected format"); + let year = captures.get(1).expect("Missing year").as_str(); + let incremental = captures.get(2).expect("Missing incremental").as_str(); + let beta = captures.get(4).map(|m| m.as_str()).unwrap_or_default(); + + format!("{year}{:0>2}00{:0>2}", incremental, beta) +} diff --git a/version-metadata.sh b/version-metadata.sh index bf3874a282..eb5f93a02d 100755 --- a/version-metadata.sh +++ b/version-metadata.sh @@ -13,13 +13,9 @@ COMMAND="$1" shift 1 PRODUCT_VERSION="" -ANDROID="false" DESKTOP="false" for argument in "$@"; do case "$argument" in - "--android") - ANDROID="true" - ;; "--desktop") DESKTOP="true" ;; @@ -60,26 +56,6 @@ function inject_version { #define PRODUCT_VERSION "$PRODUCT_VERSION" EOF fi - - if [[ "$ANDROID" == "true" ]]; then - # Android - local version_year - version_year=$(printf "%02d" "${BASH_REMATCH[1]}") - local version_number - version_number=$(printf "%02d" "${BASH_REMATCH[2]}") - local version_patch="00" # Not used for now. - local version_beta - version_beta=$(printf "%02d" "${BASH_REMATCH[4]:-99}") - local android_version_code=${version_year}${version_number}${version_patch}${version_beta} - - echo "Setting Android versionName to $PRODUCT_VERSION and versionCode to $android_version_code" - - cp android/app/build.gradle.kts android/app/build.gradle.kts.bak - sed -i -Ee "s/versionCode = [0-9]+/versionCode = $android_version_code/g" \ - android/app/build.gradle.kts - sed -i -Ee "s/versionName = \"[^\"]+\"/versionName = \"$PRODUCT_VERSION\"/g" \ - android/app/build.gradle.kts - fi } function restore_backup { @@ -90,11 +66,6 @@ function restore_backup { mv dist-assets/windows/version.h.bak dist-assets/windows/version.h fi - - if [[ "$ANDROID" == "true" ]]; then - # Android - mv android/app/build.gradle.kts.bak android/app/build.gradle.kts - fi set -e } @@ -106,11 +77,6 @@ function delete_backup { rm dist-assets/windows/version.h.bak fi - - if [[ "$ANDROID" == "true" ]]; then - # Android - rm android/app/build.gradle.kts.bak - fi set -e } |
