Improved semver parsing.
This commit is contained in:
parent
e1891c47af
commit
24b12f131e
2 changed files with 59 additions and 75 deletions
|
@ -50,11 +50,6 @@ object Utils {
|
||||||
return canRead() && isFile
|
return canRead() && isFile
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Int.length() = when (this) {
|
|
||||||
0 -> 1
|
|
||||||
else -> Math.log10(Math.abs(toDouble())).toInt() + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Properties.put(key: String, value: String, isValidCondition: Boolean) {
|
private fun Properties.put(key: String, value: String, isValidCondition: Boolean) {
|
||||||
if (isValidCondition) put(key, value)
|
if (isValidCondition) put(key, value)
|
||||||
}
|
}
|
||||||
|
@ -135,73 +130,41 @@ object Utils {
|
||||||
fun parseSemVer(input: String?, version: Version): Boolean {
|
fun parseSemVer(input: String?, version: Version): Boolean {
|
||||||
if (input.isNullOrBlank()) return false
|
if (input.isNullOrBlank()) return false
|
||||||
|
|
||||||
var semver = StringBuilder(input)
|
|
||||||
var start = semver.indexOf(version.separator)
|
|
||||||
var minor = -1
|
|
||||||
var major = -1
|
|
||||||
var patch = -1
|
|
||||||
var preRelease = ""
|
|
||||||
var buildMeta = ""
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// major
|
val parts = input.split(
|
||||||
if (start != -1) {
|
Regex("[\\Q${version.separator}${version.preReleasePrefix}${version.buildMetaPrefix}\\E]"),
|
||||||
major = Math.abs(semver.substring(0, start).toInt())
|
5
|
||||||
semver.delete(0, start + major.length())
|
)
|
||||||
start = semver.indexOf(version.separator)
|
|
||||||
// minor
|
if (parts.size >= 3) {
|
||||||
if (start != -1) {
|
version.major = parts[0].toInt()
|
||||||
minor = Math.abs(semver.substring(0, start).toInt())
|
version.minor = parts[1].toInt()
|
||||||
semver = semver.delete(0, start + minor.length())
|
version.patch = parts[2].toInt()
|
||||||
start = semver.indexOf(version.preReleasePrefix)
|
version.preRelease = ""
|
||||||
// patch
|
version.buildMeta = ""
|
||||||
if (start != -1) {
|
|
||||||
patch = Math.abs(semver.substring(0, start).toInt())
|
if (parts.size > 3) {
|
||||||
semver.delete(0, start + minor.length())
|
when {
|
||||||
start = semver.lastIndexOf(version.buildMetaPrefix)
|
parts.size == 5 -> {
|
||||||
// pre-release
|
version.preRelease = parts[3]
|
||||||
if (start != -1) {
|
version.buildMeta = parts[4]
|
||||||
preRelease = semver.substring(0, start)
|
}
|
||||||
semver.delete(0, preRelease.length)
|
parts.size == 4 -> {
|
||||||
start = semver.indexOf(version.buildMetaPrefix)
|
if (input.contains(version.buildMetaPrefix)) {
|
||||||
// build meta
|
version.buildMeta = parts[3]
|
||||||
if (start != -1) {
|
} else {
|
||||||
buildMeta = semver.substring(version.preReleasePrefix.length)
|
version.preRelease = parts[3]
|
||||||
semver.clear()
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no build meta
|
throw NumberFormatException("Not enough parts.")
|
||||||
preRelease = semver.toString()
|
|
||||||
semver.clear()
|
|
||||||
}
|
|
||||||
} else if (semver.isNotEmpty()) {
|
|
||||||
// no pre-release
|
|
||||||
start = semver.lastIndexOf(version.buildMetaPrefix)
|
|
||||||
// patch & build meta
|
|
||||||
if (start != -1) {
|
|
||||||
patch = semver.substring(0, start).toInt()
|
|
||||||
semver.delete(0, start + minor.length())
|
|
||||||
buildMeta = semver.toString()
|
|
||||||
} else {
|
|
||||||
// patch
|
|
||||||
patch = semver.toString().toInt()
|
|
||||||
}
|
|
||||||
semver.clear()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (e: NumberFormatException) {
|
} catch (e: NumberFormatException) {
|
||||||
throw GradleException("Unable to parse version: \"$input\" (${e.message})", e)
|
throw GradleException("Unable to parse version: \"$input\" (${e.message})", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (semver.isNotEmpty()) throw GradleException("Unable to parse version: \"$input\".")
|
|
||||||
|
|
||||||
version.major = major
|
|
||||||
version.minor = minor
|
|
||||||
version.patch = patch
|
|
||||||
version.preRelease = preRelease
|
|
||||||
version.buildMeta = buildMeta
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,15 +187,21 @@ object Utils {
|
||||||
put(config.buildMetaKey, version.buildMeta)
|
put(config.buildMetaKey, version.buildMeta)
|
||||||
put(config.semverKey, version.semver)
|
put(config.semverKey, version.semver)
|
||||||
|
|
||||||
put(config.buildMetaPrefixKey, version.buildMetaPrefix,
|
put(
|
||||||
|
config.buildMetaPrefixKey, version.buildMetaPrefix,
|
||||||
version.buildMetaPrefix != Version.DEFAULT_BUILDMETA_PREFIX ||
|
version.buildMetaPrefix != Version.DEFAULT_BUILDMETA_PREFIX ||
|
||||||
containsKey(config.buildMetaPrefixKey))
|
containsKey(config.buildMetaPrefixKey)
|
||||||
put(config.preReleasePrefixKey, version.preReleasePrefix,
|
)
|
||||||
|
put(
|
||||||
|
config.preReleasePrefixKey, version.preReleasePrefix,
|
||||||
version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
|
version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
|
||||||
containsKey(config.preReleasePrefixKey))
|
containsKey(config.preReleasePrefixKey)
|
||||||
put(config.separatorKey, version.separator,
|
)
|
||||||
|
put(
|
||||||
|
config.separatorKey, version.separator,
|
||||||
version.separator != Version.DEFAULT_SEPARATOR ||
|
version.separator != Version.DEFAULT_SEPARATOR ||
|
||||||
containsKey(config.separatorKey))
|
containsKey(config.separatorKey)
|
||||||
|
)
|
||||||
|
|
||||||
if (canWrite()) {
|
if (canWrite()) {
|
||||||
FileOutputStream(this).writer().use {
|
FileOutputStream(this).writer().use {
|
||||||
|
|
|
@ -46,7 +46,7 @@ import kotlin.test.assertTrue
|
||||||
object UtilsSpec : Spek({
|
object UtilsSpec : Spek({
|
||||||
Feature("Utils") {
|
Feature("Utils") {
|
||||||
val version = Version()
|
val version = Version()
|
||||||
val config = SemverConfig()
|
val config = SemverConfig(version)
|
||||||
val propsFile = File("test.properties")
|
val propsFile = File("test.properties")
|
||||||
val projectDir = File("./")
|
val projectDir = File("./")
|
||||||
lateinit var props: Properties
|
lateinit var props: Properties
|
||||||
|
@ -88,12 +88,19 @@ object UtilsSpec : Spek({
|
||||||
Pair(config.minorKey, "1"),
|
Pair(config.minorKey, "1"),
|
||||||
Pair(config.patchKey, "1"),
|
Pair(config.patchKey, "1"),
|
||||||
Pair(config.preReleaseKey, "beta"),
|
Pair(config.preReleaseKey, "beta"),
|
||||||
Pair(config.buildMetaKey, "007"))
|
Pair(config.buildMetaKey, "007")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Then("none should already exists") {
|
Then("none should already exists") {
|
||||||
assertTrue(Utils.isNotSystemProperty(setOf(config.majorKey, config.minorKey, config.patchKey, config.preReleaseKey,
|
assertTrue(
|
||||||
config.buildMetaKey)))
|
Utils.isNotSystemProperty(
|
||||||
|
setOf(
|
||||||
|
config.majorKey, config.minorKey, config.patchKey, config.preReleaseKey,
|
||||||
|
config.buildMetaKey
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Then("version should match system properties") {
|
Then("version should match system properties") {
|
||||||
|
@ -147,7 +154,15 @@ object UtilsSpec : Spek({
|
||||||
|
|
||||||
Scenario("Testing Version Parsing") {
|
Scenario("Testing Version Parsing") {
|
||||||
When("validating version parsing") {
|
When("validating version parsing") {
|
||||||
listOf("1.0.0", "2.1.0-beta", "3.2.1-beta+007", "4.3.2+007").forEach {
|
listOf(
|
||||||
|
"1.0.0",
|
||||||
|
"2.1.0-beta",
|
||||||
|
"3.2.1-beta+007",
|
||||||
|
"4.3.2+007",
|
||||||
|
"11.11.1",
|
||||||
|
"111.11.11-beta",
|
||||||
|
"1111.111.11-beta+001.12"
|
||||||
|
).forEach {
|
||||||
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
|
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
|
||||||
assertEquals(it, version.semver, it)
|
assertEquals(it, version.semver, it)
|
||||||
}
|
}
|
||||||
|
@ -166,8 +181,8 @@ object UtilsSpec : Spek({
|
||||||
}
|
}
|
||||||
|
|
||||||
Then("verifying pre-release and meta") {
|
Then("verifying pre-release and meta") {
|
||||||
assertEquals(version.preRelease, "beta.1")
|
assertEquals(version.preRelease, "beta")
|
||||||
assertEquals(version.buildMeta, "007")
|
assertEquals(version.buildMeta, "1.007")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue