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:
Pavel Punegov
2021-07-01 23:28:38 +03:00
committed by Space
parent 1bd0607b53
commit c2fe46020e
5 changed files with 161 additions and 28 deletions
@@ -12,13 +12,16 @@ import org.gradle.api.Project
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.kotlinNativeVersionSrc():File {
fun Project.konanVersionGeneratedSrc() =
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")
return if(kotlinNativeProject != null){
return if (kotlinNativeProject != null) {
if (kotlinNativeVersionInResources)
kotlinNativeProject.file("${findProperty("kotlin_root")!!}/buildSrc/src/kotlin-native-binary-version/kotlin")
else
else
kotlinNativeProject.file("../buildSrc/build/version-generated/src/generated")
} else {
if (kotlinNativeVersionInResources)
@@ -27,8 +30,10 @@ fun Project.kotlinNativeVersionSrc():File {
file("build/version-generated/src/generated")
}
}
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")
if (!kotlinNativeProperyFile.exists())
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
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? {
return if (this.kotlinNativeVersionInResources)
kotlinNativeVersionResourceFile().let { file ->
@@ -50,7 +59,7 @@ fun Project.kotlinNativeVersionValue(): CompilerVersion? {
}
open class VersionGenerator: DefaultTask() {
open class VersionGenerator : DefaultTask() {
private val kotlinNativeProperties = project.kotlinNativeProperties()
@Input
@@ -63,7 +72,7 @@ open class VersionGenerator: DefaultTask() {
open var versionFile: File? = project.file("${versionSourceDirectory.path}/org/jetbrains/kotlin/konan/CompilerVersionGenerated.kt")
@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.
@@ -88,19 +97,16 @@ open class VersionGenerator: DefaultTask() {
@TaskAction
open fun generateVersion() {
val matcher = versionPattern.matcher(konanVersion)
require(matcher.matches()) { "Cannot parse Kotlin/Native version: $konanVersion" }
val major = matcher.group(1).toInt()
val minor = matcher.group(2).toInt()
val maintenanceStr = matcher.group(3)
val maintenance = maintenanceStr?.toInt() ?: 0
val milestone = -1
val matchResult = CompilerVersion.versionPattern.matchEntire(konanVersion)
requireNotNull(matchResult) { "Cannot parse Kotlin/Native version: $konanVersion" }
val major = matchResult.groups.get(1)?.value?.toInt() ?: throw IllegalArgumentException("Unable to parse major in $konanVersion")
val minor = matchResult.groups.get(2)?.value?.toInt() ?: throw IllegalArgumentException("Unable to parse minor in $konanVersion")
val maintenance = matchResult.groups.get(3)?.value?.toInt() ?: 0
val milestone = -1 // isn't used any more
project.logger.info("BUILD_NUMBER: $buildNumber")
var build = -1
if (buildNumber != null) {
val buildNumberSplit = buildNumber!!.split("-".toRegex()).toTypedArray()
build = buildNumberSplit[buildNumberSplit.size - 1].toInt() // //7-dev-buildcount
}
val buildNumberSplit = buildNumber?.split("-".toRegex())?.toTypedArray()
val build = buildNumberSplit?.get(buildNumberSplit.size - 1)?.toIntOrNull() ?: -1
val versionObject = CompilerVersionImpl(meta, major, minor, maintenance, milestone, build)
versionObject.serialize()