fix(autoversion): support additional version formats
This commit is contained in:
parent
b15f9dbd35
commit
0e40cc68fe
@ -1,31 +1,24 @@
|
|||||||
package io.gitlab.jfronny.scripts
|
package io.gitlab.jfronny.scripts
|
||||||
|
|
||||||
data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val type: VersionType, val build: String?): Comparable<SemanticVersion> {
|
/**
|
||||||
|
* A semantic version representation.
|
||||||
|
* Not strictly following the semver spec, but a subset of it.
|
||||||
|
* 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<SemanticVersion> {
|
||||||
init {
|
init {
|
||||||
require(build == null || buildPattern.matches(build)) { "Illegal build string" }
|
require(build == null || buildPattern.matches(build)) { "Illegal build string" }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun compareTo(other: SemanticVersion): Int =
|
override fun compareTo(other: SemanticVersion): Int {
|
||||||
when {
|
return compareValuesBy(this, other, SemanticVersion::major, SemanticVersion::minor, SemanticVersion::patch, SemanticVersion::type, SemanticVersion::typeSupplement, SemanticVersion::build)
|
||||||
major > other.major -> 1
|
}
|
||||||
major < other.major -> -1
|
|
||||||
minor > other.minor -> 1
|
|
||||||
minor < other.minor -> -1
|
|
||||||
patch > other.patch -> 1
|
|
||||||
patch < other.patch -> -1
|
|
||||||
else -> when {
|
|
||||||
type != other.type -> type.compareTo(other.type)
|
|
||||||
build == null && other.build != null -> -1
|
|
||||||
build != null && other.build == null -> 1
|
|
||||||
build != null && other.build != null -> build.compareTo(other.build)
|
|
||||||
else -> 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "$major.$minor.$patch" + when (type) {
|
return "$major.$minor.$patch" + when (type) {
|
||||||
VersionType.RELEASE -> ""
|
VersionType.RELEASE -> ""
|
||||||
else -> "-${type.semanticName}"
|
else -> "-${type.semanticName}" + if(typeSupplement != null) ".$typeSupplement" else ""
|
||||||
} + if (build == null) "" else "+$build"
|
} + if (build == null) "" else "+$build"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,28 +27,29 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun incrementBy(commitType: CommitType, versionType: VersionType = VersionType.RELEASE, build: String? = null): SemanticVersion = when(commitType) {
|
fun incrementBy(commitType: CommitType, versionType: VersionType = VersionType.RELEASE, build: String? = null): SemanticVersion = when(commitType) {
|
||||||
CommitType.FIX -> SemanticVersion(major, minor, patch + 1, versionType, build)
|
CommitType.FIX -> SemanticVersion(major, minor, patch + 1, versionType, null, build)
|
||||||
CommitType.FEAT -> SemanticVersion(major, minor + 1, 0, versionType, build)
|
CommitType.FEAT -> SemanticVersion(major, minor + 1, 0, versionType, null, build)
|
||||||
CommitType.BREAKING -> SemanticVersion(major + 1, 0, 0, versionType, build)
|
CommitType.BREAKING -> SemanticVersion(major + 1, 0, 0, versionType, null, build)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun withType(versionType: VersionType) = SemanticVersion(major, minor, patch, versionType, build)
|
fun withType(versionType: VersionType) = SemanticVersion(major, minor, patch, versionType, null, build)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val identifier = Regex("[a-zA-Z1-9][a-zA-Z0-9]*")
|
private val identifier = Regex("[a-zA-Z1-9][a-zA-Z0-9]*")
|
||||||
private val buildPattern = Regex("$identifier(?:\\.$identifier)+")
|
private val buildPattern = Regex("$identifier(?:\\.$identifier)+")
|
||||||
private val number = Regex("[1-9][0-9]*|0")
|
private val number = Regex("[1-9][0-9]*|0")
|
||||||
private val versionCore = Regex("($number)\\.($number)\\.($number)")
|
private val versionCore = Regex("($number)\\.($number)(?:\\.($number))?")
|
||||||
private val legacyVersion = Regex("([vba]|rc)$versionCore(\\+$buildPattern)?")
|
private val legacyVersion = Regex("([vba]|rc)$versionCore(\\+$buildPattern)?")
|
||||||
private val restrictedSemver = Regex("$versionCore(?:-(alpha|beta|rc))?(\\+$buildPattern)?")
|
private val restrictedSemver = Regex("$versionCore(?:-(alpha|beta|rc)(\\.?\\d+)?)?(\\+$buildPattern)?")
|
||||||
|
|
||||||
fun parse(source: String): SemanticVersion {
|
fun parse(source: String): SemanticVersion {
|
||||||
val legacyMatch = legacyVersion.matchEntire(source)
|
val legacyMatch = legacyVersion.matchEntire(source)
|
||||||
if (legacyMatch != null) {
|
if (legacyMatch != null) {
|
||||||
val m = legacyMatch.groupValues
|
val m = legacyMatch.groupValues
|
||||||
return SemanticVersion(
|
return SemanticVersion(
|
||||||
m[2].toInt(), m[3].toInt(), m[4].toInt(),
|
m[2].toInt(), m[3].toInt(), m[4].ifEmpty { "0" }.toInt(),
|
||||||
VersionType.byShorthand(m[1])!!,
|
VersionType.byShorthand(m[1])!!,
|
||||||
|
null,
|
||||||
m[5].ifEmpty { null }
|
m[5].ifEmpty { null }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -63,9 +57,10 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t
|
|||||||
if (semverMatch != null) {
|
if (semverMatch != null) {
|
||||||
val m = semverMatch.groupValues
|
val m = semverMatch.groupValues
|
||||||
return SemanticVersion(
|
return SemanticVersion(
|
||||||
m[1].toInt(), m[2].toInt(), m[3].toInt(),
|
m[1].toInt(), m[2].toInt(), m[3].ifEmpty { "0" }.toInt(),
|
||||||
VersionType.byName(m[4].ifEmpty { "release" })!!,
|
VersionType.byName(m[4].ifEmpty { "release" })!!,
|
||||||
m[5].ifEmpty { null }
|
m[5].ifEmpty { null }?.trimStart('.')?.toInt(),
|
||||||
|
m[6].ifEmpty { null }?.substring(1)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
throw IllegalArgumentException("Source does not match supported version patterns: $source")
|
throw IllegalArgumentException("Source does not match supported version patterns: $source")
|
||||||
|
Loading…
Reference in New Issue
Block a user