Native: improve CompilerVersion to correspond to the versioning schema
* Add pub and dev-google-pr meta versions * Allow using release versions with and without build number * Add tests for version parsing
This commit is contained in:
@@ -6,6 +6,7 @@ plugins {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(kotlinStdlib())
|
implementation(kotlinStdlib())
|
||||||
testImplementation(commonDep("junit:junit"))
|
testImplementation(commonDep("junit:junit"))
|
||||||
|
testImplementation(kotlin("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
|
|||||||
@@ -12,14 +12,17 @@ interface CompilerVersion : Serializable {
|
|||||||
val major: Int
|
val major: Int
|
||||||
val minor: Int
|
val minor: Int
|
||||||
val maintenance: Int
|
val maintenance: Int
|
||||||
|
|
||||||
|
@Deprecated("Milestone is deprecated in favour to MetaVersion's M1 and M2")
|
||||||
val milestone: Int
|
val milestone: Int
|
||||||
|
|
||||||
val build: Int
|
val build: Int
|
||||||
|
|
||||||
fun toString(showMeta: Boolean, showBuild: Boolean): String
|
fun toString(showMeta: Boolean, showBuild: Boolean): String
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// major.minor.patch-meta-build where patch, meta and build are optional.
|
// major.minor.patch-meta-build where patch, meta and build are optional.
|
||||||
private val versionPattern = "(\\d+)\\.(\\d+)(?:\\.(\\d+))?(?:-(\\p{Alpha}\\p{Alnum}*))?(?:-(\\d+))?".toRegex()
|
val versionPattern = "(\\d+)\\.(\\d+)(?:\\.(\\d+))?(?:-(\\p{Alpha}\\p{Alnum}|[\\p{Alpha}-]*))?(?:-(\\d+))?".toRegex()
|
||||||
|
|
||||||
fun fromString(version: String): CompilerVersion {
|
fun fromString(version: String): CompilerVersion {
|
||||||
val (major, minor, maintenance, metaString, build) =
|
val (major, minor, maintenance, metaString, build) =
|
||||||
@@ -68,7 +71,7 @@ data class CompilerVersionImpl(
|
|||||||
private val isRelease: Boolean
|
private val isRelease: Boolean
|
||||||
get() = meta == MetaVersion.RELEASE
|
get() = meta == MetaVersion.RELEASE
|
||||||
|
|
||||||
private val versionString by lazy { toString(!isRelease, !isRelease) }
|
private val versionString by lazy { toString(!isRelease, true) }
|
||||||
|
|
||||||
override fun toString() = versionString
|
override fun toString() = versionString
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,18 +5,15 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.konan
|
package org.jetbrains.kotlin.konan
|
||||||
|
|
||||||
/**
|
|
||||||
* https://en.wikipedia.org/wiki/Software_versioning
|
|
||||||
* scheme major.minor[.build[.revision]].
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class MetaVersion(val metaString: String) {
|
enum class MetaVersion(val metaString: String) {
|
||||||
DEV("dev"),
|
DEV("dev"),
|
||||||
|
DEV_GOOGLE("dev-google-pr"),
|
||||||
EAP("eap"),
|
EAP("eap"),
|
||||||
BETA("beta"),
|
BETA("beta"),
|
||||||
M1("M1"),
|
M1("M1"),
|
||||||
M2("M2"),
|
M2("M2"),
|
||||||
RC("RC"),
|
RC("RC"),
|
||||||
|
PUB("PUB"),
|
||||||
RELEASE("release");
|
RELEASE("release");
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.native
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.konan.CompilerVersion
|
||||||
|
import org.jetbrains.kotlin.konan.CompilerVersionImpl
|
||||||
|
import org.jetbrains.kotlin.konan.MetaVersion
|
||||||
|
import org.jetbrains.kotlin.konan.parseCompilerVersion
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
class NativeCompilerVersionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun versionParseTest() {
|
||||||
|
"1.5.30-dev-1".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.DEV, meta)
|
||||||
|
assertEquals(1, build)
|
||||||
|
}
|
||||||
|
"1.5.30-rc-12".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.RC, meta)
|
||||||
|
assertEquals(12, build)
|
||||||
|
}
|
||||||
|
// Final release
|
||||||
|
"1.5.30".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.RELEASE, meta)
|
||||||
|
assertEquals(-1, build)
|
||||||
|
}
|
||||||
|
// Final release build.number
|
||||||
|
"1.5.30-release-34".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.RELEASE, meta)
|
||||||
|
assertEquals(34, build)
|
||||||
|
}
|
||||||
|
// Release builds
|
||||||
|
"1.5.30-1234".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.RELEASE, meta)
|
||||||
|
assertEquals(1234, build)
|
||||||
|
}
|
||||||
|
"1.6.0-dev-1".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(6, minor)
|
||||||
|
assertEquals(0, maintenance)
|
||||||
|
assertEquals(MetaVersion.DEV, meta)
|
||||||
|
assertEquals(1, build)
|
||||||
|
}
|
||||||
|
"1.6.10-M1-3".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(6, minor)
|
||||||
|
assertEquals(10, maintenance)
|
||||||
|
assertEquals(MetaVersion.M1, meta)
|
||||||
|
assertEquals(3, build)
|
||||||
|
}
|
||||||
|
"1.6.20-M2".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(6, minor)
|
||||||
|
assertEquals(20, maintenance)
|
||||||
|
assertEquals(MetaVersion.M2, meta)
|
||||||
|
assertEquals(-1, build)
|
||||||
|
}
|
||||||
|
"1.6.10-RC".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(6, minor)
|
||||||
|
assertEquals(10, maintenance)
|
||||||
|
assertEquals(MetaVersion.RC, meta)
|
||||||
|
assertEquals(-1, build)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun incorrectVersionString() {
|
||||||
|
assertFailsWith<IllegalArgumentException> { CompilerVersion.fromString("1.5.30-M1-dev-123") }
|
||||||
|
assertFailsWith<IllegalArgumentException> { CompilerVersion.fromString("1.5.30-M1-release-123") }
|
||||||
|
assertFailsWith<IllegalArgumentException> { CompilerVersion.fromString("1.5.30.40-release-123") }
|
||||||
|
assertFailsWith<IllegalStateException> { CompilerVersion.fromString("1.6.0-M3-123") }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun versionToString() {
|
||||||
|
assertEquals("1.5.30-dev-140", CompilerVersionImpl(MetaVersion.DEV, 1, 5, 30, -1, 140).toString())
|
||||||
|
assertEquals("1.5.30-RC-140", CompilerVersionImpl(MetaVersion.RC, 1, 5, 30, -1, 140).toString())
|
||||||
|
assertEquals("1.5.30-RC", CompilerVersionImpl(MetaVersion.RC, 1, 5, 30, -1, -1).toString())
|
||||||
|
assertEquals("1.6.10-14", CompilerVersionImpl(MetaVersion.RELEASE, 1, 6, 10, -1, 14).toString())
|
||||||
|
assertEquals("1.6.10-14", CompilerVersionImpl(MetaVersion.RELEASE, 1, 6, 10, -1, 14).toString())
|
||||||
|
assertEquals("1.6.0", CompilerVersionImpl(MetaVersion.RELEASE, 1, 6, 0, -1, -1).toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun publicVersion() {
|
||||||
|
"1.5.30-dev-google-pr-123".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.DEV_GOOGLE, meta)
|
||||||
|
assertEquals(123, build)
|
||||||
|
}
|
||||||
|
assertEquals("1.5.30-dev-google-pr-140", CompilerVersionImpl(MetaVersion.DEV_GOOGLE, 1, 5, 30, -1, 140).toString())
|
||||||
|
|
||||||
|
"1.5.30-pub-123".parseCompilerVersion().apply {
|
||||||
|
assertEquals(1, major)
|
||||||
|
assertEquals(5, minor)
|
||||||
|
assertEquals(30, maintenance)
|
||||||
|
assertEquals(MetaVersion.PUB, meta)
|
||||||
|
assertEquals(123, build)
|
||||||
|
}
|
||||||
|
assertEquals("1.5.30-PUB-140", CompilerVersionImpl(MetaVersion.PUB, 1, 5, 30, -1, 140).toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-21
@@ -12,13 +12,16 @@ import org.gradle.api.Project
|
|||||||
import org.jetbrains.kotlin.konan.*
|
import org.jetbrains.kotlin.konan.*
|
||||||
|
|
||||||
|
|
||||||
fun Project.konanVersionGeneratedSrc() = rootProject.findProject(":kotlin-native")?.file("../buildSrc/build/version-generated/src/generated") ?: file("build/version-generated/src/generated")
|
fun Project.konanVersionGeneratedSrc() =
|
||||||
fun Project.kotlinNativeVersionSrc():File {
|
rootProject.findProject(":kotlin-native")?.file("../buildSrc/build/version-generated/src/generated")
|
||||||
|
?: file("build/version-generated/src/generated")
|
||||||
|
|
||||||
|
fun Project.kotlinNativeVersionSrc(): File {
|
||||||
val kotlinNativeProject = rootProject.findProject(":kotlin-native")
|
val kotlinNativeProject = rootProject.findProject(":kotlin-native")
|
||||||
return if(kotlinNativeProject != null){
|
return if (kotlinNativeProject != null) {
|
||||||
if (kotlinNativeVersionInResources)
|
if (kotlinNativeVersionInResources)
|
||||||
kotlinNativeProject.file("${findProperty("kotlin_root")!!}/buildSrc/src/kotlin-native-binary-version/kotlin")
|
kotlinNativeProject.file("${findProperty("kotlin_root")!!}/buildSrc/src/kotlin-native-binary-version/kotlin")
|
||||||
else
|
else
|
||||||
kotlinNativeProject.file("../buildSrc/build/version-generated/src/generated")
|
kotlinNativeProject.file("../buildSrc/build/version-generated/src/generated")
|
||||||
} else {
|
} else {
|
||||||
if (kotlinNativeVersionInResources)
|
if (kotlinNativeVersionInResources)
|
||||||
@@ -27,8 +30,10 @@ fun Project.kotlinNativeVersionSrc():File {
|
|||||||
file("build/version-generated/src/generated")
|
file("build/version-generated/src/generated")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.konanRootDir() = rootProject.findProject(":kotlin-native")?.projectDir ?: file("../kotlin-native")
|
fun Project.konanRootDir() = rootProject.findProject(":kotlin-native")?.projectDir ?: file("../kotlin-native")
|
||||||
fun Project.kotlinNativeProperties() = Properties().apply{
|
|
||||||
|
fun Project.kotlinNativeProperties() = Properties().apply {
|
||||||
val kotlinNativeProperyFile = File(this@kotlinNativeProperties.konanRootDir(), "gradle.properties")
|
val kotlinNativeProperyFile = File(this@kotlinNativeProperties.konanRootDir(), "gradle.properties")
|
||||||
if (!kotlinNativeProperyFile.exists())
|
if (!kotlinNativeProperyFile.exists())
|
||||||
return@apply
|
return@apply
|
||||||
@@ -37,10 +42,14 @@ fun Project.kotlinNativeProperties() = Properties().apply{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val Project.kotlinNativeVersionInResources:Boolean
|
val Project.kotlinNativeVersionInResources: Boolean
|
||||||
get() = kotlinNativeProperties()["kotlinNativeVersionInResources"]?.toString()?.toBoolean() ?: false
|
get() = kotlinNativeProperties()["kotlinNativeVersionInResources"]?.toString()?.toBoolean() ?: false
|
||||||
|
|
||||||
fun Project.kotlinNativeVersionResourceFile() = File("${project.findProperty("kotlin_root")!!}/buildSrc/build/version-generated/META-INF/kotlin-native.compiler.version")
|
fun Project.kotlinNativeVersionResourceFile() = File(
|
||||||
|
"${project.findProperty("kotlin_root")!!}" +
|
||||||
|
"/buildSrc/build/version-generated/META-INF/kotlin-native.compiler.version"
|
||||||
|
)
|
||||||
|
|
||||||
fun Project.kotlinNativeVersionValue(): CompilerVersion? {
|
fun Project.kotlinNativeVersionValue(): CompilerVersion? {
|
||||||
return if (this.kotlinNativeVersionInResources)
|
return if (this.kotlinNativeVersionInResources)
|
||||||
kotlinNativeVersionResourceFile().let { file ->
|
kotlinNativeVersionResourceFile().let { file ->
|
||||||
@@ -50,7 +59,7 @@ fun Project.kotlinNativeVersionValue(): CompilerVersion? {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open class VersionGenerator: DefaultTask() {
|
open class VersionGenerator : DefaultTask() {
|
||||||
private val kotlinNativeProperties = project.kotlinNativeProperties()
|
private val kotlinNativeProperties = project.kotlinNativeProperties()
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
@@ -63,7 +72,7 @@ open class VersionGenerator: DefaultTask() {
|
|||||||
open var versionFile: File? = project.file("${versionSourceDirectory.path}/org/jetbrains/kotlin/konan/CompilerVersionGenerated.kt")
|
open var versionFile: File? = project.file("${versionSourceDirectory.path}/org/jetbrains/kotlin/konan/CompilerVersionGenerated.kt")
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
open val konanVersion = project.findProperty("konanVersion") as? String ?: kotlinNativeProperties["konanVersion"].toString()
|
open val konanVersion = project.findProperty("konanVersion") as? String ?: kotlinNativeProperties["konanVersion"].toString()
|
||||||
|
|
||||||
|
|
||||||
// TeamCity passes all configuration parameters into a build script as project properties.
|
// TeamCity passes all configuration parameters into a build script as project properties.
|
||||||
@@ -88,19 +97,16 @@ open class VersionGenerator: DefaultTask() {
|
|||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
open fun generateVersion() {
|
open fun generateVersion() {
|
||||||
val matcher = versionPattern.matcher(konanVersion)
|
val matchResult = CompilerVersion.versionPattern.matchEntire(konanVersion)
|
||||||
require(matcher.matches()) { "Cannot parse Kotlin/Native version: $konanVersion" }
|
requireNotNull(matchResult) { "Cannot parse Kotlin/Native version: $konanVersion" }
|
||||||
val major = matcher.group(1).toInt()
|
val major = matchResult.groups.get(1)?.value?.toInt() ?: throw IllegalArgumentException("Unable to parse major in $konanVersion")
|
||||||
val minor = matcher.group(2).toInt()
|
val minor = matchResult.groups.get(2)?.value?.toInt() ?: throw IllegalArgumentException("Unable to parse minor in $konanVersion")
|
||||||
val maintenanceStr = matcher.group(3)
|
val maintenance = matchResult.groups.get(3)?.value?.toInt() ?: 0
|
||||||
val maintenance = maintenanceStr?.toInt() ?: 0
|
val milestone = -1 // isn't used any more
|
||||||
val milestone = -1
|
|
||||||
project.logger.info("BUILD_NUMBER: $buildNumber")
|
project.logger.info("BUILD_NUMBER: $buildNumber")
|
||||||
var build = -1
|
val buildNumberSplit = buildNumber?.split("-".toRegex())?.toTypedArray()
|
||||||
if (buildNumber != null) {
|
val build = buildNumberSplit?.get(buildNumberSplit.size - 1)?.toIntOrNull() ?: -1
|
||||||
val buildNumberSplit = buildNumber!!.split("-".toRegex()).toTypedArray()
|
|
||||||
build = buildNumberSplit[buildNumberSplit.size - 1].toInt() // //7-dev-buildcount
|
|
||||||
}
|
|
||||||
|
|
||||||
val versionObject = CompilerVersionImpl(meta, major, minor, maintenance, milestone, build)
|
val versionObject = CompilerVersionImpl(meta, major, minor, maintenance, milestone, build)
|
||||||
versionObject.serialize()
|
versionObject.serialize()
|
||||||
|
|||||||
Reference in New Issue
Block a user