From e303eae15ac44d64e9976843a4c759c0084cafb8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 23 Oct 2020 12:49:00 +0800 Subject: [PATCH] Add String variant of dependsOn.versionRequirement; Add ResolveContext.VERSION_REQUIREMENT --- .../console/compiler/common/ResolveContext.kt | 2 ++ .../plugin/jvm/JvmPluginDescription.kt | 27 ++++++++++--------- .../mamoe/mirai/console/util/SemVersion.kt | 9 ++++++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt index 9dff987e2..a10aa00d8 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt @@ -40,6 +40,8 @@ public annotation class ResolveContext( PLUGIN_NAME, // ILLEGAL_PLUGIN_DESCRIPTION PLUGIN_VERSION, // ILLEGAL_PLUGIN_DESCRIPTION + VERSION_REQUIREMENT, // ILLEGAL_VERSION_REQUIREMENT // TODO + COMMAND_NAME, // ILLEGAL_COMMAND_NAME PERMISSION_NAMESPACE, // ILLEGAL_COMMAND_NAMESPACE 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 71b1f97a9..8a08a4cb7 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 @@ -148,18 +148,6 @@ public class JvmPluginDescriptionBuilder( } } - /** - * @see PluginDependency - */ - @ILoveKuriyamaMiraiForever - public fun dependsOn( - @ResolveContext(PLUGIN_ID) pluginId: String, - isOptional: Boolean = false, - versionRequirement: SemVersion.Requirement, - ): JvmPluginDescriptionBuilder = apply { - this.dependencies.add(PluginDependency(pluginId, versionRequirement, isOptional)) - } - /** * isOptional = false * @@ -169,8 +157,21 @@ public class JvmPluginDescriptionBuilder( public fun dependsOn( @ResolveContext(PLUGIN_ID) pluginId: String, versionRequirement: SemVersion.Requirement, + isOptional: Boolean = false, ): JvmPluginDescriptionBuilder = apply { - this.dependencies.add(PluginDependency(pluginId, versionRequirement, false)) + this.dependencies.add(PluginDependency(pluginId, versionRequirement, isOptional)) + } + + /** + * @see PluginDependency + */ + @ILoveKuriyamaMiraiForever + public fun dependsOn( + @ResolveContext(PLUGIN_ID) pluginId: String, + @ResolveContext(VERSION_REQUIREMENT) versionRequirement: String, + isOptional: Boolean = false, + ): JvmPluginDescriptionBuilder = apply { + this.dependencies.add(PluginDependency(pluginId, SemVersion.parseRangeRequirement(versionRequirement), isOptional)) } /** 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 c96e2950f..89519e813 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 @@ -22,6 +22,7 @@ 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.compiler.common.ResolveContext.Kind.VERSION_REQUIREMENT import net.mamoe.mirai.console.internal.data.map import net.mamoe.mirai.console.internal.util.semver.SemVersionInternal import net.mamoe.mirai.console.util.SemVersion.Companion.equals @@ -139,7 +140,7 @@ internal constructor( */ @Throws(IllegalArgumentException::class) @JvmStatic - public fun parseRangeRequirement(requirement: String): Requirement = + public fun parseRangeRequirement(@ResolveContext(VERSION_REQUIREMENT) requirement: String): Requirement = SemVersionInternal.parseRangeRequirement(requirement) /** @see [Requirement.test] */ @@ -152,6 +153,12 @@ internal constructor( @JvmStatic public fun SemVersion.satisfies(requirement: Requirement): Boolean = requirement.test(this) + /** + * 当满足 [requirement] 时返回 true, 否则返回 false + */ + @JvmStatic + public fun SemVersion.satisfies(@ResolveContext(VERSION_REQUIREMENT) requirement: String): Boolean = parseRangeRequirement(requirement).test(this) + /** for Kotlin only */ @JvmStatic @JvmSynthetic