Skip to content

Commit

Permalink
Support Android app's new version code format
Browse files Browse the repository at this point in the history
  • Loading branch information
kl committed Nov 20, 2024
1 parent 81a1b1f commit 7f166c6
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 38 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mullvad-version/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ workspace = true


[dependencies]
regex = "1.6.0"
regex = "1.11.1"
161 changes: 147 additions & 14 deletions mullvad-version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,66 @@ pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-versio
pub struct Version {
pub year: String,
pub incremental: String,
pub beta: Option<String>,
pub version_type: VersionType,
}

impl Version {}

#[derive(Debug, Clone, PartialEq)]
pub enum VersionType {
Alpha(String),
Beta(String),
Dev(String),
Release,
}

impl Version {
pub fn parse(version: &str) -> Version {
Version::from_str(version).unwrap()
}

pub fn is_release(&self) -> bool {
matches!(&self.version_type, VersionType::Release)
}

pub fn alpha(&self) -> Option<&str> {
match &self.version_type {
VersionType::Alpha(v) => Some(v),
_ => None,
}
}

pub fn beta(&self) -> Option<&str> {
match &self.version_type {
VersionType::Beta(v) => Some(v),
_ => None,
}
}

pub fn dev(&self) -> Option<&str> {
match &self.version_type {
VersionType::Dev(v) => Some(v),
_ => None,
}
}
}

impl Display for Version {
/// Format Version as a string: year.incremental{-beta}
/// Format Version as a string: year.incremental-{alpha|beta|dev}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Version {
year,
incremental,
beta,
version_type,
} = &self;
match beta {
Some(beta) => write!(f, "{year}.{incremental}-{beta}"),
None => write!(f, "{year}.{incremental}"),

write!(f, "{year}.{incremental}")?;

match version_type {
VersionType::Alpha(version) => write!(f, "-alpha{version}"),
VersionType::Beta(version) => write!(f, "-beta{version}"),
VersionType::Dev(commit_hash) => write!(f, "-dev-{commit_hash}"),
VersionType::Release => Ok(()),
}
}
}
Expand All @@ -39,25 +79,118 @@ impl FromStr for Version {
type Err = String;

fn from_str(version: &str) -> Result<Self, Self::Err> {
const VERSION_REGEX: &str =
r"^20([0-9]{2})\.([1-9][0-9]?)(-beta([1-9][0-9]?))?(-dev-[0-9a-f]+)?$";
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(VERSION_REGEX).unwrap());
static VERSION_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?x)
20(?<year>[0-9]{2})\. # the last two digits of the year
(?<incremental>[1-9][0-9]?) # the incrementing version number
(?: # (optional) alpha or beta or dev
-alpha(?<alpha>[1-9][0-9]?)|
-beta(?<beta>[1-9][0-9]?)|
-dev-(?<dev>[0-9a-f]+)|
-dev
)?$
",
)
.unwrap()
});

let captures = RE
let captures = VERSION_REGEX
.captures(version)
.ok_or_else(|| format!("Version does not match expected format: {version}"))?;
let year = captures.get(1).expect("Missing year").as_str().to_owned();

let year = captures
.name("year")
.expect("Missing year")
.as_str()
.to_owned();

let incremental = captures
.get(2)
.name("incremental")
.ok_or("Missing incremental")?
.as_str()
.to_owned();
let beta = captures.get(4).map(|m| m.as_str().to_owned());

let alpha = captures.name("alpha").map(|m| m.as_str().to_owned());
let beta = captures.name("beta").map(|m| m.as_str().to_owned());
let dev = captures.name("dev").map(|m| m.as_str().to_owned());

let sub_type = match (alpha, beta, dev) {
(Some(v), _, _) => VersionType::Alpha(v),
(_, Some(v), _) => VersionType::Beta(v),
(_, _, Some(v)) => VersionType::Dev(v),
(None, None, None) => VersionType::Release,
};

Ok(Version {
year,
incremental,
beta,
version_type: sub_type,
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse() {
let version = "2021.34";
let parsed = Version::parse(version);
assert_eq!(parsed.year, "21");
assert_eq!(parsed.incremental, "34");
assert_eq!(parsed.alpha(), None);
assert_eq!(parsed.beta(), None);
assert_eq!(parsed.dev(), None);
assert_eq!(parsed.is_release(), true);
}

#[test]
fn test_parse_with_alpha() {
let version = "2023.1-alpha77";
let parsed = Version::parse(version);
assert_eq!(parsed.year, "23");
assert_eq!(parsed.incremental, "1");
assert_eq!(parsed.alpha(), Some("77"));
assert_eq!(parsed.beta(), None);
assert_eq!(parsed.dev(), None);
assert_eq!(parsed.is_release(), false);
}

#[test]
fn test_parse_with_beta() {
let version = "2021.34-beta5";
let parsed = Version::parse(version);
assert_eq!(parsed.year, "21");
assert_eq!(parsed.incremental, "34");
assert_eq!(parsed.alpha(), None);
assert_eq!(parsed.beta(), Some("5"));
assert_eq!(parsed.dev(), None);
assert_eq!(parsed.is_release(), false);
}

#[test]
fn test_parse_with_dev() {
let version = "2021.34-dev-0b60e4d87";
let parsed = Version::parse(version);
assert_eq!(parsed.year, "21");
assert_eq!(parsed.incremental, "34");
assert_eq!(parsed.alpha(), None);
assert_eq!(parsed.beta(), None);
assert_eq!(parsed.dev(), Some("0b60e4d87"));
assert_eq!(parsed.is_release(), false);
}

#[test]
#[should_panic]
fn test_panics_on_invalid_version() {
Version::parse("2021");
}

#[test]
#[should_panic]
fn test_panics_on_alpha_and_beta_in_same_version() {
Version::parse("2021.1-beta5-alpha2");
}
}
65 changes: 49 additions & 16 deletions mullvad-version/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mullvad_version::Version;
use mullvad_version::{Version, VersionType};
use std::{env, process::exit};

const ANDROID_VERSION: &str =
Expand Down Expand Up @@ -35,30 +35,37 @@ fn to_semver(version: &str) -> String {
/// 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, 99 if stable ^^
/// The format of the code is: YYVVXZZZ
/// Last two digits of the year (major)---------^^
/// Incrementing version (minor)------------------^^
/// Build type (0=alpha, 1=beta, 9=stable/dev)------^
/// Build number (000 if stable/dev)-----------------^^^
///
/// # Examples
///
/// Version: 2021.1-alpha1
/// versionCode: 21010001
///
/// Version: 2021.34-beta5
/// versionCode: 21340005
/// versionCode: 21341005
///
/// Version: 2021.34
/// versionCode: 21340099
/// versionCode: 21349000
///
/// Version: 2021.34-dev
/// versionCode: 21349000
fn to_android_version_code(version: &str) -> String {
const ANDROID_STABLE_VERSION_CODE_SUFFIX: &str = "99";

let version = Version::parse(version);

let (build_type, build_number) = match &version.version_type {
VersionType::Alpha(v) => ("0", v.as_str()),
VersionType::Beta(v) => ("1", v.as_str()),
VersionType::Dev(_) | VersionType::Release => ("9", "000"),
};

format!(
"{}{:0>2}00{:0>2}",
version.year,
version.incremental,
version
.beta
.unwrap_or(ANDROID_STABLE_VERSION_CODE_SUFFIX.to_string())
"{}{:0>2}{}{:0>3}",
version.year, version.incremental, build_type, build_number,
)
}

Expand All @@ -74,3 +81,29 @@ fn to_windows_h_format(version: &str) -> String {
#define PRODUCT_VERSION \"{version}\""
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_version_code() {
assert_eq!("21349000", to_android_version_code("2021.34"));
}

#[test]
fn test_version_code_alpha() {
assert_eq!("21010001", to_android_version_code("2021.1-alpha1"));
}

#[test]
fn test_version_code_beta() {
assert_eq!("21341005", to_android_version_code("2021.34-beta5"));
}

#[test]
fn test_version_code_dev() {
assert_eq!("21349000", to_android_version_code("2021.34-dev"));
assert_eq!("21349000", to_android_version_code("2021.34-dev-be846a5f0"));
}
}
5 changes: 4 additions & 1 deletion prepare-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ if [[ "$DESKTOP" == "true" && $(grep "^## \\[$PRODUCT_VERSION\\] - " CHANGELOG.m
exit 1
fi

if [[ "$ANDROID" == "true" && $(grep "^## \\[android/$PRODUCT_VERSION\\] - " android/CHANGELOG.md) == "" ]]; then
if [[ "$ANDROID" == "true" &&
$PRODUCT_VERSION != *"alpha"* &&
$(grep "^## \\[android/$PRODUCT_VERSION\\] - " android/CHANGELOG.md) == "" ]]; then

echo "It looks like you did not add $PRODUCT_VERSION to the changelog?"
echo "Please make sure the changelog is up to date and correct before you proceed."
exit 1
Expand Down

0 comments on commit 7f166c6

Please sign in to comment.