From e26e98d0303ce14a525f4c9ee15d13d3d70fd8ed Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 20 Sep 2020 18:54:51 +0800 Subject: [PATCH] Rename SemVersion.Companion.parse to SemVersion.Companion.invoke --- .../internal/MiraiConsoleBuildConstants.kt | 2 +- .../console/internal/util/SemVersionInternal.kt | 8 ++++---- .../console/plugin/jvm/JvmPluginDescription.kt | 6 +++--- .../net/mamoe/mirai/console/util/SemVersion.kt | 17 ++++++++++++----- .../net/mamoe/mirai/console/TestMiraiConosle.kt | 2 +- .../mamoe/mirai/console/util/TestSemVersion.kt | 6 +++--- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt index f1889aeab..f314be39c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt @@ -17,5 +17,5 @@ internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mira val buildDate: Instant = Instant.ofEpochSecond(1600596035) @JvmStatic - val version: SemVersion = SemVersion.parse("1.0-RC-dev-28") + val version: SemVersion = SemVersion("1.0-RC-dev-28") } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt index bcbd996a6..85a05ac4e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/SemVersionInternal.kt @@ -85,7 +85,7 @@ internal object SemVersionInternal { private fun String.parseRule(): SemVersion.RangeRequirement { val trimmed = trim() if (directVersion.matches(trimmed)) { - val parsed = SemVersion.parse(trimmed) + val parsed = SemVersion.invoke(trimmed) return SemVersion.RangeRequirement { it.compareTo(parsed) == 0 } @@ -101,8 +101,8 @@ internal object SemVersionInternal { } } (versionRange.matchEntire(trimmed) ?: versionMathRange.matchEntire(trimmed))?.let { range -> - var start = SemVersion.parse(range.groupValues[1]) - var end = SemVersion.parse(range.groupValues[4]) + var start = SemVersion.invoke(range.groupValues[1]) + var end = SemVersion.invoke(range.groupValues[4]) if (start > end) { val c = end end = start @@ -115,7 +115,7 @@ internal object SemVersionInternal { } versionRule.matchEntire(trimmed)?.let { result -> val operator = result.groupValues[1] - val version = SemVersion.parse(result.groupValues[7]) + val version = SemVersion.invoke(result.groupValues[7]) return when (operator) { ">=" -> { SemVersion.RangeRequirement { it >= version } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt index 242249f21..1948c7b9a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt @@ -105,7 +105,7 @@ public class JvmPluginDescriptionBuilder( public constructor( @ResolveContext(PLUGIN_NAME) id: String, @ResolveContext(PLUGIN_VERSION) version: String, - ) : this(id, SemVersion.parse(version)) + ) : this(id, SemVersion(version)) private var name: String = id private var author: String = "" @@ -118,7 +118,7 @@ public class JvmPluginDescriptionBuilder( @ILoveKuriyamaMiraiForever public fun version(@ResolveContext(PLUGIN_VERSION) value: String): JvmPluginDescriptionBuilder = - apply { this.version = SemVersion.parse(value) } + apply { this.version = SemVersion(value) } @ILoveKuriyamaMiraiForever public fun version(@ResolveContext(PLUGIN_VERSION) value: SemVersion): JvmPluginDescriptionBuilder = @@ -281,7 +281,7 @@ public data class SimpleJvmPluginDescription author: String = "", info: String = "", dependencies: Set = setOf(), - ) : this(name, SemVersion.parse(version), id, author, info, dependencies) + ) : this(name, SemVersion(version), id, author, info, dependencies) init { require(!name.contains(':')) { "':' is forbidden in plugin name" } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt index 38903ccbd..a1abf883c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersion.kt @@ -21,6 +21,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import kotlinx.serialization.builtins.serializer import net.mamoe.mirai.console.compiler.common.ResolveContext +import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.PLUGIN_VERSION import net.mamoe.mirai.console.internal.data.map import net.mamoe.mirai.console.internal.util.SemVersionInternal import net.mamoe.mirai.console.util.SemVersion.Companion.equals @@ -44,15 +45,20 @@ import net.mamoe.mirai.console.util.SemVersion.RangeRequirement * 对于核心版本号, 此实现稍微比 semver 宽松一些, 允许 x.y 的存在. * * @see RangeRequirement + * @see SemVersion.invoke */ @Serializable(with = SemVersion.SemVersionAsStringSerializer::class) -public data class SemVersion internal constructor( +public data class SemVersion +/** + * @see SemVersion.invoke 字符串解析 + */ +internal constructor( /** 核心版本号, 由主版本号, 次版本号和修订号组成, 其中修订号不一定存在 */ public val mainVersion: IntArray, /** 先行版本号识别符 */ public val identifier: String? = null, /** 版本号元数据, 不参与版本号对比([compareTo]), 但是参与版本号严格对比([equals]) */ - public val metadata: String? = null + public val metadata: String? = null, ) : Comparable { /** * 一条依赖规则 @@ -65,7 +71,7 @@ public data class SemVersion internal constructor( public object SemVersionAsStringSerializer : KSerializer by String.serializer().map( serializer = { it.toString() }, - deserializer = { parse(it) } + deserializer = { SemVersion(it) } ) public companion object { @@ -90,7 +96,8 @@ public data class SemVersion internal constructor( */ @Throws(IllegalArgumentException::class, NumberFormatException::class) @JvmStatic - public fun parse(@ResolveContext(ResolveContext.Kind.PLUGIN_VERSION) version: String): SemVersion = SemVersionInternal.parse(version) + @JvmName("parse") + public operator fun invoke(@ResolveContext(PLUGIN_VERSION) version: String): SemVersion = SemVersionInternal.parse(version) /** * 解析一条依赖需求描述, 在无法解析的时候抛出 [IllegalArgumentException] @@ -121,7 +128,7 @@ public data class SemVersion internal constructor( /** @see [RangeRequirement.test] */ @JvmStatic - public fun RangeRequirement.test(version: String): Boolean = test(parse(version)) + public fun RangeRequirement.test(version: String): Boolean = test(invoke(version)) /** * 当满足 [requirement] 时返回 true, 否则返回 false diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt index 87eda59e8..969f03425 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/TestMiraiConosle.kt @@ -44,7 +44,7 @@ fun initTestEnvironment() { override val vendor: String get() = "Test" override val version: SemVersion - get() = SemVersion.parse("1.0.0") + get() = SemVersion("1.0.0") } override val builtInPluginLoaders: List>> = listOf(lazy { JvmPluginLoader }) diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/util/TestSemVersion.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/util/TestSemVersion.kt index b76d610e1..e2ae8a8c1 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/util/TestSemVersion.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/util/TestSemVersion.kt @@ -20,7 +20,7 @@ import org.junit.jupiter.api.Test internal class TestSemVersion { @Test internal fun testCompare() { - fun String.sem(): SemVersion = SemVersion.parse(this) + fun String.sem(): SemVersion = SemVersion.invoke(this) assert("1.0".sem() < "1.0.1".sem()) assert("1.0.0".sem() == "1.0".sem()) assert("1.1".sem() > "1.0.0".sem()) @@ -85,12 +85,12 @@ internal class TestSemVersion { } private fun String.check() { - val sem = SemVersion.parse(this) + val sem = SemVersion.invoke(this) assert(this == sem.toString()) { "$this != $sem" } } private fun String.checkInvalid() { - kotlin.runCatching { SemVersion.parse(this) } + kotlin.runCatching { SemVersion.invoke(this) } .onSuccess { assert(false) { "$this not a invalid sem-version" } } }