From af5751d3d87add66cf44843ee8767d5973aca31c Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Sun, 23 Aug 2020 19:34:52 +0200 Subject: [PATCH 1/2] Add release candidate number to version code --- app/build.gradle.kts | 2 +- buildSrc/src/main/kotlin/VersionUtils.kt | 52 +++++++++++++++++------- settings.gradle.kts | 4 +- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9fb2544a..7b463016 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -15,7 +15,7 @@ android { applicationId = "org.jellyfin.mobile" minSdkVersion(21) targetSdkVersion(30) - versionName = "2.0.0-rc2" + versionName = "2.0.0-rc.2" versionCode = getVersionCode(versionName) setProperty("archivesBaseName", "jellyfin-android-v$versionName") testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/buildSrc/src/main/kotlin/VersionUtils.kt b/buildSrc/src/main/kotlin/VersionUtils.kt index 202695c2..3e2e407d 100644 --- a/buildSrc/src/main/kotlin/VersionUtils.kt +++ b/buildSrc/src/main/kotlin/VersionUtils.kt @@ -1,29 +1,51 @@ /** - * Get the versionCode for a given semver + * Get the version code for a given semantic version. + * Does not validate the input and thus will throw an exception when parts are missing. + * + * The pre-release part ("-rc.1", "-beta.1" etc.) defaults to 99 * * Sample output: - * 0.0.0 -> 0 - * 1.1.1 -> 10101 - * 0.7.0 -> 700 - * 99.99.99 -> 999999 - * - * @return the versionCode, or null if value is invalid. + * MA.MI.PA-PR -> MAMIPAPR + * 0.0.0 -> 99 + * 1.1.1 -> 1010199 + * 0.7.0 -> 70099 + * 99.99.99 -> 99999999 + * 2.0.0-rc.3 -> 2000003 + * 2.0.0 -> 2000099 + * 99.99.99-rc.1 -> 99999901 */ fun getVersionCode(versionName: String): Int? { - val parts = versionName - .substringBefore('-') + // Split to core and pre release parts with a default for pre release (null) + val (versionCore, versionPreRelease) = versionName + .split("-", limit = 2) + .let { + when (it.size) { + // No pre release part included + 1 -> arrayOf(it[0], null) + // Pre release part included + else -> arrayOf(it[0], it[1]) + } + } + + // Parse core part + val (major, minor, patch) = versionCore!! .splitToSequence('.') .mapNotNull(String::toIntOrNull) .take(3) .toList() - // Not a valid semver - if (parts.size != 3) return null + // Parse pre release part (ignore type, only get the number) + val buildVersion = versionPreRelease + ?.split('.', limit = 2) + ?.getOrNull(1) + ?.let(String::toIntOrNull) + // Build code var code = 0 - code += parts[0] * 10000 // Major (0-99) - code += parts[1] * 100 // Minor (0-99) - code += parts[2] // Patch (0-99) + code += major * 1000000 // Major (0-99) + code += minor * 10000 // Minor (0-99) + code += patch * 100 // Patch (0-99) + code += buildVersion ?: 99 // Pre release (0-99) return code -} \ No newline at end of file +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 1cdbae80..8129c838 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,7 +1,5 @@ include(":app") -rootProject.name = "Jellyfin" - pluginManagement { val kotlinVersion: String by settings resolutionStrategy { @@ -10,4 +8,4 @@ pluginManagement { useVersion(kotlinVersion) } } -} \ No newline at end of file +} From dc1f5f2c2047c3ca541c800065b18428720ec6c5 Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Sun, 23 Aug 2020 20:29:29 +0200 Subject: [PATCH 2/2] Improve readability and performance of getVersionCode --- buildSrc/src/main/kotlin/VersionUtils.kt | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/buildSrc/src/main/kotlin/VersionUtils.kt b/buildSrc/src/main/kotlin/VersionUtils.kt index 3e2e407d..dfaa8c6c 100644 --- a/buildSrc/src/main/kotlin/VersionUtils.kt +++ b/buildSrc/src/main/kotlin/VersionUtils.kt @@ -16,19 +16,17 @@ */ fun getVersionCode(versionName: String): Int? { // Split to core and pre release parts with a default for pre release (null) - val (versionCore, versionPreRelease) = versionName - .split("-", limit = 2) - .let { - when (it.size) { - // No pre release part included - 1 -> arrayOf(it[0], null) - // Pre release part included - else -> arrayOf(it[0], it[1]) - } + val (versionCore, versionPreRelease) = + when (val index = versionName.indexOf('-')) { + // No pre-release part included + -1 -> versionName to null + // Pre-release part included + else -> versionName.substring(0, index) to + versionName.substring(index + 1, versionName.length) } // Parse core part - val (major, minor, patch) = versionCore!! + val (major, minor, patch) = versionCore .splitToSequence('.') .mapNotNull(String::toIntOrNull) .take(3) @@ -36,8 +34,7 @@ fun getVersionCode(versionName: String): Int? { // Parse pre release part (ignore type, only get the number) val buildVersion = versionPreRelease - ?.split('.', limit = 2) - ?.getOrNull(1) + ?.substringAfter('.') ?.let(String::toIntOrNull) // Build code