Rename SemVersion.Companion.parse to SemVersion.Companion.invoke

This commit is contained in:
Him188 2020-09-20 18:54:51 +08:00
parent eb7cd3811d
commit e26e98d030
6 changed files with 24 additions and 17 deletions

View File

@ -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")
}

View File

@ -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 }

View File

@ -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<PluginDependency> = 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" }

View File

@ -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<SemVersion> {
/**
* 一条依赖规则
@ -65,7 +71,7 @@ public data class SemVersion internal constructor(
public object SemVersionAsStringSerializer : KSerializer<SemVersion> 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

View File

@ -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<Lazy<PluginLoader<*, *>>> = listOf(lazy { JvmPluginLoader })

View File

@ -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" } }
}