From 997c3f80a9237e9ba385f6938a5c7eb7e4506029 Mon Sep 17 00:00:00 2001 From: JFronny Date: Mon, 30 Sep 2024 18:23:41 +0200 Subject: [PATCH] feat: add support for type supplements in bumpVersion --- .../io/gitlab/jfronny/scripts/BumpVersionTask.kt | 3 ++- .../io/gitlab/jfronny/scripts/SemanticVersion.kt | 10 +++++----- convention/src/main/kotlin/jf.autoversion.gradle.kts | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/BumpVersionTask.kt b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/BumpVersionTask.kt index 73466da..f0322a5 100644 --- a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/BumpVersionTask.kt +++ b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/BumpVersionTask.kt @@ -18,6 +18,7 @@ abstract class BumpVersionTask : DefaultTask() { @get:Input abstract val nextVersionType: Property @get:Input abstract val nextRelease: Property @get:Input abstract val lastRelease: Property + @get:Input abstract val typeSupplement: Property @TaskAction fun action() { @@ -26,7 +27,7 @@ abstract class BumpVersionTask : DefaultTask() { val vt = nextVersionType .map { VersionType.byName(it) ?: throw IllegalStateException("Unrecognized version type: $this") } .getOrThrow { IllegalStateException("bumpVersion requires you to set -PversionType=release|beta|alpha") } - val name = nextRelease.get().withType(vt).toString() + val name = nextRelease.get().withType(vt, typeSupplement.orNull).toString() Git.open(gitDir.asFile.get()).use { git -> git.tag().setName(name).call() logger.warn("Created release $name (last was ${lastRelease.get()}). Make sure to push it!") diff --git a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt index 94c047f..5ea9f49 100644 --- a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt +++ b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt @@ -6,7 +6,7 @@ package io.gitlab.jfronny.scripts * For example, the pre-release type identifier is restricted to alpha, beta, rc followed by an optional number. * Parsing also supports legacy version strings like v1.2.3, b1.2.3, a1.2.3, rc1.2.3. */ -data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val type: VersionType, val typeSupplement: Int?, val build: String?): Comparable { +data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val type: VersionType, val typeSupplement: String?, val build: String?): Comparable { constructor(major: Int, minor: Int, patch: Int, versionType: VersionType) : this(major, minor, patch, versionType, null, null) constructor(major: Int, minor: Int, patch: Int) : this(major, minor, patch, VersionType.RELEASE) @@ -18,7 +18,7 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t override fun toString(): String { return "$major.$minor.$patch" + when (type) { - VersionType.RELEASE -> "" + VersionType.RELEASE -> typeSupplement ?: "" else -> "-${type.semanticName}" + if(typeSupplement != null) ".$typeSupplement" else "" } + if (build == null) "" else "+$build" } @@ -33,7 +33,7 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t CommitType.BREAKING -> SemanticVersion(major + 1, 0, 0, versionType, null, build) } - fun withType(versionType: VersionType) = SemanticVersion(major, minor, patch, versionType, null, build) + fun withType(versionType: VersionType, typeSupplement: String? = null) = SemanticVersion(major, minor, patch, versionType, typeSupplement, build) companion object { private val identifier = Regex("[a-zA-Z1-9][a-zA-Z0-9]*") @@ -41,7 +41,7 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t private val number = Regex("[1-9][0-9]*|0") private val versionCore = Regex("($number)\\.($number)(?:\\.($number))?") private val legacyVersion = Regex("([vba]|rc)$versionCore(\\+$buildPattern)?") - private val restrictedSemver = Regex("$versionCore(?:-(alpha|beta|rc|pre)(\\.?\\d+)?)?(\\+$buildPattern)?") + private val restrictedSemver = Regex("$versionCore(?:-(alpha|beta|rc|pre)(\\.?[a-zA-Z0-9-]+)?)?(\\+$buildPattern)?") fun parse(source: String): SemanticVersion { val legacyMatch = legacyVersion.matchEntire(source) @@ -60,7 +60,7 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t return SemanticVersion( m[1].toInt(), m[2].toInt(), m[3].ifEmpty { "0" }.toInt(), VersionType.byName(m[4].ifEmpty { "release" })!!, - m[5].ifEmpty { null }?.trimStart('.')?.toInt(), + m[5].ifEmpty { null }?.trimStart('.'), m[6].ifEmpty { null }?.substring(1) ) } diff --git a/convention/src/main/kotlin/jf.autoversion.gradle.kts b/convention/src/main/kotlin/jf.autoversion.gradle.kts index 92344e9..dad5648 100644 --- a/convention/src/main/kotlin/jf.autoversion.gradle.kts +++ b/convention/src/main/kotlin/jf.autoversion.gradle.kts @@ -69,4 +69,5 @@ tasks.register("bumpVersion", BumpVersionTask::class) { nextVersionType = project.provider { nprop("nextVersionType") } nextRelease = project.provider { project.nextRelease } lastRelease = project.provider { project.lastRelease } + typeSupplement = project.provider { nprop("typeSupplement") } }