diff --git a/backend/mirai-console/build.gradle.kts b/backend/mirai-console/build.gradle.kts index d520dd69e..6aad5bba4 100644 --- a/backend/mirai-console/build.gradle.kts +++ b/backend/mirai-console/build.gradle.kts @@ -101,8 +101,8 @@ tasks { })""" } .replace( - Regex("""val version: Semver = Semver\(".*", Semver.SemverType.LOOSE\)""") - ) { """val version: Semver = Semver("${project.version}", Semver.SemverType.LOOSE)""" } + Regex("""val version: SemVersion = SemVersion.parse\(".*"\)""") + ) { """val version: SemVersion = SemVersion.parse("${project.version}")""" } ) } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 15f4b9a01..42106685e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -12,7 +12,6 @@ package net.mamoe.mirai.console -import com.vdurmont.semver4j.Semver import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import net.mamoe.mirai.Bot @@ -29,6 +28,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleInternalApi import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext +import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.MiraiLogger import java.io.File @@ -79,7 +79,7 @@ public interface MiraiConsole : CoroutineScope { /** * 此 Console 后端版本号 */ - public val version: Semver + public val version: SemVersion @ConsoleExperimentalApi diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEndDescription.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEndDescription.kt index 21c1f8103..6b7426d4d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEndDescription.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEndDescription.kt @@ -9,7 +9,8 @@ package net.mamoe.mirai.console -import com.vdurmont.semver4j.Semver +import net.mamoe.mirai.console.util.SemVersion + /** * 有关前端实现的信息 @@ -28,7 +29,7 @@ public interface MiraiConsoleFrontEndDescription { /** * 此前端实现的名称 */ - public val version: Semver + public val version: SemVersion /** * 兼容的 [MiraiConsole] 后端版本号 @@ -37,7 +38,7 @@ public interface MiraiConsoleFrontEndDescription { * * 返回 `null` 表示禁止 [MiraiConsole] 后端检查版本兼容性. */ - public val compatibleBackendVersion: Semver? get() = null + public val compatibleBackendVersion: SemVersion? get() = null /** * 返回显示在 [MiraiConsole] 启动时的信息 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 19dd40076..f1889aeab 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 @@ -9,13 +9,13 @@ package net.mamoe.mirai.console.internal -import com.vdurmont.semver4j.Semver +import net.mamoe.mirai.console.util.SemVersion import java.time.Instant internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants) @JvmStatic - val buildDate: Instant = Instant.ofEpochSecond(1600522812) + val buildDate: Instant = Instant.ofEpochSecond(1600596035) @JvmStatic - val version: Semver = Semver("1.0-RC-dev-28", Semver.SemverType.LOOSE) + val version: SemVersion = SemVersion.parse("1.0-RC-dev-28") } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt index 0751b2432..175af2331 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt @@ -11,7 +11,6 @@ package net.mamoe.mirai.console.internal -import com.vdurmont.semver4j.Semver import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job @@ -47,6 +46,7 @@ import net.mamoe.mirai.console.plugin.jvm.AbstractJvmPlugin import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleInput +import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.utils.* import java.nio.file.Path import java.time.Instant @@ -66,7 +66,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI private val instance: MiraiConsoleImplementation by MiraiConsoleImplementation.Companion::instance override val buildDate: Instant by MiraiConsoleBuildConstants::buildDate - override val version: Semver by MiraiConsoleBuildConstants::version + override val version: SemVersion by MiraiConsoleBuildConstants::version override val rootPath: Path by instance::rootPath override val frontEndDescription: MiraiConsoleFrontEndDescription by instance::frontEndDescription diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt index f53ebac6f..2f15ba42a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt @@ -27,6 +27,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoadException import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.plugin.name import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope +import net.mamoe.mirai.console.util.SemVersion.Companion.contains import net.mamoe.mirai.utils.info import java.io.File import java.nio.file.Path diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt index 55a00f667..8db84c890 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt @@ -11,7 +11,6 @@ package net.mamoe.mirai.console.plugin -import com.vdurmont.semver4j.Semver import net.mamoe.mirai.console.command.CommandOwner import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.disable import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable @@ -20,6 +19,7 @@ import net.mamoe.mirai.console.plugin.description.PluginDependency import net.mamoe.mirai.console.plugin.description.PluginDescription import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.loader.PluginLoader +import net.mamoe.mirai.console.util.SemVersion /** * 表示一个 mirai-console 插件. @@ -62,7 +62,7 @@ public inline val Plugin.name: String get() = this.description.name /** * 获取 [PluginDescription.version] */ -public inline val Plugin.version: Semver get() = this.description.version +public inline val Plugin.version: SemVersion get() = this.description.version /** * 获取 [PluginDescription.info] diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDependency.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDependency.kt index 49bfad6ff..d22883093 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDependency.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDependency.kt @@ -11,6 +11,8 @@ package net.mamoe.mirai.console.plugin.description +import net.mamoe.mirai.console.util.SemVersion + /** * 插件的一个依赖的信息. * @@ -29,7 +31,7 @@ public data class PluginDependency @JvmOverloads constructor( * ### 示例 * `Requirement.buildIvy("[1.0, 2.0)")` */ - public val versionRequirement: VersionRequirement? = null, + public val versionRequirement: SemVersion.RangeRequirement? = null, /** * 若为 `false`, 插件在找不到此依赖时也能正常加载. */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt index 85c199628..ca9267495 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt @@ -9,10 +9,10 @@ package net.mamoe.mirai.console.plugin.description -import com.vdurmont.semver4j.Semver import net.mamoe.mirai.console.compiler.common.ResolveContext import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.* import net.mamoe.mirai.console.plugin.Plugin +import net.mamoe.mirai.console.util.SemVersion /** @@ -93,7 +93,7 @@ public interface PluginDescription { * @see Semver 语义化版本. 允许 [宽松][Semver.SemverType.LOOSE] 类型版本. */ @ResolveContext(PLUGIN_VERSION) - public val version: Semver + public val version: SemVersion /** * 插件信息, 允许为空 diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/VersionRequirement.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/VersionRequirement.kt deleted file mode 100644 index e3a3e6680..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/VersionRequirement.kt +++ /dev/null @@ -1,242 +0,0 @@ -package net.mamoe.mirai.console.plugin.description - -import com.vdurmont.semver4j.Requirement -import com.vdurmont.semver4j.Semver - -public sealed class VersionRequirement { - public abstract operator fun contains(version: Semver): Boolean - public fun contains(version: String): Boolean = contains(Semver(version, Semver.SemverType.LOOSE)) - - public class Exact - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - constructor( - version: Semver, - ) : VersionRequirement() { - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - public val version: Semver = version.toStrict() - - @Suppress("DEPRECATION_ERROR") - public constructor(version: String) : this(Semver(version, Semver.SemverType.LOOSE)) - - @Suppress("DEPRECATION_ERROR") - override fun contains(version: Semver): Boolean = this.version.isEquivalentTo(version.toStrict()) - } - - public data class MatchesNpmPattern( - val pattern: String, - ) : VersionRequirement() { - private val requirement = Requirement.buildNPM(pattern) - override fun contains(version: Semver): Boolean = requirement.isSatisfiedBy(version.toStrict()) - } - - public data class MatchesIvyPattern( - val pattern: String, - ) : VersionRequirement() { - private val requirement = Requirement.buildIvy(pattern) - override fun contains(version: Semver): Boolean = requirement.isSatisfiedBy(version.toStrict()) - } - - - public data class MatchesCocoapodsPattern( - val pattern: String, - ) : VersionRequirement() { - private val requirement = Requirement.buildCocoapods(pattern) - override fun contains(version: Semver): Boolean = requirement.isSatisfiedBy(version.toStrict()) - } - - public abstract class Custom : VersionRequirement() - - @Suppress("MemberVisibilityCanBePrivate") - public class InRange( - begin: Semver, - public val beginInclusive: Boolean, - end: Semver, - public val endInclusive: Boolean, - ) : VersionRequirement() { - public val end: Semver = end.toStrict() - public val begin: Semver = begin.toStrict() - - public constructor( - begin: String, - beginInclusive: Boolean, - end: Semver, - endInclusive: Boolean, - ) : this(Semver(begin, Semver.SemverType.LOOSE), beginInclusive, end, endInclusive) - - public constructor( - begin: String, - beginInclusive: Boolean, - end: String, - endInclusive: Boolean, - ) : this(Semver(begin, Semver.SemverType.LOOSE), - beginInclusive, - Semver(end, Semver.SemverType.LOOSE), - endInclusive) - - public constructor( - begin: Semver, - beginInclusive: Boolean, - end: String, - endInclusive: Boolean, - ) : this(begin, beginInclusive, Semver(end, Semver.SemverType.LOOSE), endInclusive) - - override fun contains(version: Semver): Boolean { - val strict = version.toStrict() - return if (beginInclusive) { - strict.isGreaterThanOrEqualTo(begin) - } else { - strict.isGreaterThan(begin) - } && if (endInclusive) { - strict.isLowerThanOrEqualTo(end) - } else { - strict.isLowerThan(end) - } - } - - override fun toString(): String { - return buildString { - append(if (beginInclusive) "[" else "(") - append(begin) - append(",") - append(end) - append(if (endInclusive) "]" else ")") - } - } - } - - - @Suppress("unused", "DeprecatedCallableAddReplaceWith") - public class Builder { - @Suppress("DEPRECATION_ERROR") - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public fun exact(version: Semver): VersionRequirement = Exact(version) - - @ILoveKafuuChinoForever - public fun exact(version: String): VersionRequirement = Exact(version) - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public fun custom(checker: (version: Semver) -> Boolean): VersionRequirement { - return object : Custom() { - override fun contains(version: Semver): Boolean = checker(version) - } - } - - /** - * @see Semver.SemverType.NPM - */ - @ILoveKafuuChinoForever - public fun npmPattern(versionPattern: String): VersionRequirement { - return MatchesNpmPattern(versionPattern) - } - - /** - * @see Semver.SemverType.IVY - */ - @ILoveKafuuChinoForever - public fun ivyPattern(versionPattern: String): VersionRequirement { - return MatchesIvyPattern(versionPattern) - } - - /** - * @see Semver.SemverType.COCOAPODS - */ - @ILoveKafuuChinoForever - public fun cocoapodsPattern(versionPattern: String): VersionRequirement { - return MatchesCocoapodsPattern(versionPattern) - } - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public fun range( - begin: Semver, - beginInclusive: Boolean, - end: Semver, - endInclusive: Boolean, - ): VersionRequirement = InRange(begin, beginInclusive, end, endInclusive) - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public fun range( - begin: String, - beginInclusive: Boolean, - end: Semver, - endInclusive: Boolean, - ): VersionRequirement = InRange(begin, beginInclusive, end, endInclusive) - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public fun range( - begin: Semver, - beginInclusive: Boolean, - end: String, - endInclusive: Boolean, - ): VersionRequirement = InRange(begin, beginInclusive, end, endInclusive) - - @ILoveKafuuChinoForever - public fun range( - begin: String, - beginInclusive: Boolean, - end: String, - endInclusive: Boolean, - ): VersionRequirement = InRange(begin, beginInclusive, end, endInclusive) - - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public operator fun Semver.rangeTo(endInclusive: Semver): VersionRequirement { - return InRange(this, true, endInclusive, true) - } - - @ILoveKafuuChinoForever - public operator fun Semver.rangeTo(endInclusive: String): VersionRequirement { - return InRange(this, true, Semver(endInclusive, Semver.SemverType.LOOSE), true) - } - - @ILoveKafuuChinoForever - public operator fun String.rangeTo(endInclusive: String): VersionRequirement { - return InRange(Semver(this, Semver.SemverType.LOOSE), - true, - Semver(endInclusive, Semver.SemverType.LOOSE), - true) - } - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public operator fun String.rangeTo(endInclusive: Semver): VersionRequirement { - return InRange(Semver(this, Semver.SemverType.LOOSE), true, endInclusive, true) - } - - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public infix fun Semver.until(endExclusive: Semver): VersionRequirement { - return InRange(this, true, endExclusive, false) - } - - @ILoveKafuuChinoForever - public infix fun Semver.until(endExclusive: String): VersionRequirement { - return InRange(this, true, Semver(endExclusive, Semver.SemverType.LOOSE), false) - } - - @ILoveKafuuChinoForever - public infix fun String.until(endExclusive: String): VersionRequirement { - return InRange(Semver(this, Semver.SemverType.LOOSE), - true, - Semver(endExclusive, Semver.SemverType.LOOSE), - false) - } - - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) - @ILoveKafuuChinoForever - public infix fun String.until(endExclusive: Semver): VersionRequirement { - return InRange(Semver(this, Semver.SemverType.LOOSE), true, endExclusive, false) - } - - @Suppress("SpellCheckingInspection") - @Retention(AnnotationRetention.BINARY) - @DslMarker - internal annotation class ILoveKafuuChinoForever - } -} \ No newline at end of file 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 0b61101bc..242249f21 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 @@ -11,12 +11,12 @@ package net.mamoe.mirai.console.plugin.jvm -import com.vdurmont.semver4j.Semver import net.mamoe.mirai.console.compiler.common.ResolveContext import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.* import net.mamoe.mirai.console.plugin.description.PluginDependency import net.mamoe.mirai.console.plugin.description.PluginDescription -import net.mamoe.mirai.console.plugin.description.VersionRequirement +import net.mamoe.mirai.console.util.SemVersion +import net.mamoe.mirai.console.util.SemVersionRangeRequirementBuilder /** * JVM 插件的描述. 通常作为 `plugin.yml` @@ -66,7 +66,7 @@ public interface JvmPluginDescription : PluginDescription { /** * @see [PluginDescription.version] */ - @ResolveContext(PLUGIN_VERSION) version: Semver, + @ResolveContext(PLUGIN_VERSION) version: SemVersion, /** * @see [PluginDescription.name] */ @@ -97,17 +97,15 @@ public interface JvmPluginDescription : PluginDescription { * * @see [JvmPluginDescription.invoke] */ -public class JvmPluginDescriptionBuilder -@Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) -constructor( +public class JvmPluginDescriptionBuilder( private var id: String, - private var version: Semver, + private var version: SemVersion, ) { @Suppress("DEPRECATION_ERROR") public constructor( @ResolveContext(PLUGIN_NAME) id: String, @ResolveContext(PLUGIN_VERSION) version: String, - ) : this(id, Semver(version, Semver.SemverType.LOOSE)) + ) : this(id, SemVersion.parse(version)) private var name: String = id private var author: String = "" @@ -118,14 +116,12 @@ constructor( public fun name(@ResolveContext(PLUGIN_NAME) value: String): JvmPluginDescriptionBuilder = apply { this.name = value.trim() } - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) @ILoveKuriyamaMiraiForever public fun version(@ResolveContext(PLUGIN_VERSION) value: String): JvmPluginDescriptionBuilder = - apply { this.version = Semver(value, Semver.SemverType.LOOSE) } + apply { this.version = SemVersion.parse(value) } - @Deprecated("Semver 将会在 1.0-RC 被替换为 Console 自己实现的版本。请临时使用 String。", level = DeprecationLevel.ERROR) @ILoveKuriyamaMiraiForever - public fun version(@ResolveContext(PLUGIN_VERSION) value: Semver): JvmPluginDescriptionBuilder = + public fun version(@ResolveContext(PLUGIN_VERSION) value: SemVersion): JvmPluginDescriptionBuilder = apply { this.version = value } @ILoveKuriyamaMiraiForever @@ -161,7 +157,7 @@ constructor( public fun dependsOn( @ResolveContext(PLUGIN_ID) pluginId: String, isOptional: Boolean = false, - versionRequirement: VersionRequirement, + versionRequirement: SemVersion.RangeRequirement, ): JvmPluginDescriptionBuilder = apply { this.dependencies.add(PluginDependency(pluginId, versionRequirement, isOptional)) } @@ -174,7 +170,7 @@ constructor( @ILoveKuriyamaMiraiForever public fun dependsOn( @ResolveContext(PLUGIN_ID) pluginId: String, - versionRequirement: VersionRequirement, + versionRequirement: SemVersion.RangeRequirement, ): JvmPluginDescriptionBuilder = apply { this.dependencies.add(PluginDependency(pluginId, versionRequirement, false)) } @@ -203,17 +199,17 @@ constructor( * ``` * * @see PluginDependency - * @see VersionRequirement.Builder + * @see SemVersionRangeRequirementBuilder */ @ILoveKuriyamaMiraiForever public fun dependsOn( @ResolveContext(PLUGIN_ID) pluginId: String, isOptional: Boolean = false, - versionRequirement: VersionRequirement.Builder.() -> VersionRequirement, + versionRequirement: SemVersionRangeRequirementBuilder.() -> SemVersion.RangeRequirement, ): JvmPluginDescriptionBuilder = apply { this.dependencies.add(PluginDependency(pluginId, - VersionRequirement.Builder().run(versionRequirement), + SemVersionRangeRequirementBuilder.run(versionRequirement), isOptional)) } @@ -259,7 +255,7 @@ public data class SimpleJvmPluginDescription ) @JvmOverloads public constructor( public override val name: String, - public override val version: Semver, + public override val version: SemVersion, public override val id: String = name, public override val author: String = "", public override val info: String = "", @@ -285,7 +281,7 @@ public data class SimpleJvmPluginDescription author: String = "", info: String = "", dependencies: Set = setOf(), - ) : this(name, Semver(version, Semver.SemverType.LOOSE), id, author, info, dependencies) + ) : this(name, SemVersion.parse(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 56e7ac81e..38903ccbd 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 @@ -20,6 +20,7 @@ import kotlinx.serialization.KSerializer 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.internal.data.map import net.mamoe.mirai.console.internal.util.SemVersionInternal import net.mamoe.mirai.console.util.SemVersion.Companion.equals @@ -89,7 +90,7 @@ public data class SemVersion internal constructor( */ @Throws(IllegalArgumentException::class, NumberFormatException::class) @JvmStatic - public fun parse(version: String): SemVersion = SemVersionInternal.parse(version) + public fun parse(@ResolveContext(ResolveContext.Kind.PLUGIN_VERSION) version: String): SemVersion = SemVersionInternal.parse(version) /** * 解析一条依赖需求描述, 在无法解析的时候抛出 [IllegalArgumentException] diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersionRangeRequirementBuilder.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersionRangeRequirementBuilder.kt new file mode 100644 index 000000000..ec4d663a4 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/SemVersionRangeRequirementBuilder.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + * + */ + +package net.mamoe.mirai.console.util + +/** + * 构造 [SemVersion.RangeRequirement] 的 DSL + */ +public object SemVersionRangeRequirementBuilder { + /** @see [SemVersion.parseRangeRequirement] */ + @ILoveHim188moeForever + public fun parse(rule: String): SemVersion.RangeRequirement = SemVersion.parseRangeRequirement(rule) + + @ILoveHim188moeForever + public infix fun SemVersion.RangeRequirement.or(other: SemVersion.RangeRequirement): SemVersion.RangeRequirement { + return object : SemVersion.RangeRequirement { + override fun test(version: SemVersion): Boolean { + return this@or.test(version) || other.test(version) + } + + override fun toString(): String { + return "(${this@or}) or ($other)" + } + } + } + + @ILoveHim188moeForever + public infix fun String.or(other: String): SemVersion.RangeRequirement = parse(this) or parse(other) + + @ILoveHim188moeForever + public infix fun SemVersion.RangeRequirement.or(other: String): SemVersion.RangeRequirement = or(parse(other)) + + @ILoveHim188moeForever + public infix fun String.or(other: SemVersion.RangeRequirement): SemVersion.RangeRequirement = parse(this) or other + + @ILoveHim188moeForever + public infix fun SemVersion.RangeRequirement.and(other: SemVersion.RangeRequirement): SemVersion.RangeRequirement { + return object : SemVersion.RangeRequirement { + override fun test(version: SemVersion): Boolean { + return this@and.test(version) && other.test(version) + } + + override fun toString(): String { + return "(${this@and}) or ($other)" + } + } + } + + @ILoveHim188moeForever + public infix fun String.and(other: String): SemVersion.RangeRequirement = parse(this) and parse(other) + + @ILoveHim188moeForever + public infix fun SemVersion.RangeRequirement.and(other: String): SemVersion.RangeRequirement = and(parse(other)) + + @ILoveHim188moeForever + public infix fun String.and(other: SemVersion.RangeRequirement): SemVersion.RangeRequirement = parse(this) and other + + @Suppress("NOTHING_TO_INLINE") + @ILoveHim188moeForever + public inline fun custom(rule: SemVersion.RangeRequirement): SemVersion.RangeRequirement = rule + + /** + * 标注一个 [SemVersionRangeRequirementBuilder] DSL + */ + @Suppress("SpellCheckingInspection") + @Retention(AnnotationRetention.BINARY) + @DslMarker + internal annotation class ILoveHim188moeForever + + /** [SemVersionRangeRequirementBuilder] 的使用示例 */ + @Suppress("unused") + private class ExampleOfBuilder { + val e1 = SemVersionRangeRequirementBuilder.run { + "1.0.0" or "1.1.5" + } + val e2 = SemVersionRangeRequirementBuilder.run { + parse("> 1.0.0") and parse("< 1.2.3") + } + val e3 = SemVersionRangeRequirementBuilder.run { + ("> 1.0.0" and "< 1.2.3") or "2.0.0" + } + } +} \ No newline at end of file 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 8afa90d9a..87eda59e8 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 @@ -9,7 +9,6 @@ package net.mamoe.mirai.console -import com.vdurmont.semver4j.Semver import kotlinx.coroutines.* import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.command.CommandManager @@ -20,6 +19,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleInput import net.mamoe.mirai.console.util.ConsoleInternalApi +import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.LoginSolver @@ -43,8 +43,8 @@ fun initTestEnvironment() { get() = "Test" override val vendor: String get() = "Test" - override val version: Semver - get() = Semver("1.0.0") + override val version: SemVersion + get() = SemVersion.parse("1.0.0") } override val builtInPluginLoaders: List>> = listOf(lazy { JvmPluginLoader }) diff --git a/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/MiraiConsoleImplementationTerminal.kt b/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/MiraiConsoleImplementationTerminal.kt index b1af02164..c6d790a76 100644 --- a/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/MiraiConsoleImplementationTerminal.kt +++ b/frontend/mirai-console-terminal/src/main/kotlin/net/mamoe/mirai/console/terminal/MiraiConsoleImplementationTerminal.kt @@ -23,7 +23,6 @@ package net.mamoe.mirai.console.terminal -import com.vdurmont.semver4j.Semver import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineName @@ -39,10 +38,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.terminal.ConsoleInputImpl.requestInput import net.mamoe.mirai.console.terminal.noconsole.AllEmptyLineReader import net.mamoe.mirai.console.terminal.noconsole.NoConsole -import net.mamoe.mirai.console.util.ConsoleExperimentalApi -import net.mamoe.mirai.console.util.ConsoleInput -import net.mamoe.mirai.console.util.ConsoleInternalApi -import net.mamoe.mirai.console.util.NamedSupervisorJob +import net.mamoe.mirai.console.util.* import net.mamoe.mirai.utils.* import org.fusesource.jansi.Ansi import org.jline.reader.LineReader @@ -155,7 +151,7 @@ val terminal: Terminal = run { private object ConsoleFrontEndDescImpl : MiraiConsoleFrontEndDescription { override val name: String get() = "Terminal" override val vendor: String get() = "Mamoe Technologies" - override val version: Semver = net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version + override val version: SemVersion = net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version } private val ANSI_RESET = Ansi().reset().toString()