Move Plugin.description to extension; Extract PluginDescription

This commit is contained in:
Him188 2020-08-23 17:03:07 +08:00
parent 4739aa9152
commit c5389201e1
9 changed files with 132 additions and 103 deletions

View File

@ -17,6 +17,9 @@ import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.internal.data.cast
import net.mamoe.mirai.console.internal.data.mkdir
import net.mamoe.mirai.console.plugin.*
import net.mamoe.mirai.console.plugin.dsecription.PluginDependency
import net.mamoe.mirai.console.plugin.dsecription.PluginDescription
import net.mamoe.mirai.console.plugin.dsecription.PluginKind
import net.mamoe.mirai.utils.info
import java.nio.file.Path
import java.util.concurrent.locks.ReentrantLock

View File

@ -12,6 +12,7 @@
package net.mamoe.mirai.console.plugin
import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.plugin.dsecription.PluginDescription
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import java.io.File
@ -39,13 +40,13 @@ public interface Plugin : CommandOwner {
* 所属插件加载器实例, 此加载器必须能加载这个 [Plugin].
*/
public val loader: PluginLoader<*, *>
/**
* 获取插件描述
*/
public val description: PluginDescription
}
/**
* 获取插件描述
*/
public val Plugin.description: PluginDescription get() = safeLoader.getDescription(this)
/**
* 禁用这个插件
*

View File

@ -11,6 +11,8 @@
package net.mamoe.mirai.console.plugin
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.register
import net.mamoe.mirai.console.plugin.dsecription.PluginDescription
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
import java.io.File
@ -25,10 +27,10 @@ import java.io.File
* - [JarPluginLoader] Jar 插件加载器
*
* ### 扩展加载器
* 插件被允许扩展一个加载器 可通过 [PluginManager.registerPluginLoader]
* 插件被允许扩展一个加载器 可通过 [PluginManager.register]
*
* @see JarPluginLoader Jar 插件加载器
* @see PluginManager.registerPluginLoader 注册一个扩展的插件加载器
* @see PluginManager.register 注册一个扩展的插件加载器
*/
public interface PluginLoader<P : Plugin, D : PluginDescription> {
/**
@ -75,6 +77,7 @@ public interface PluginLoader<P : Plugin, D : PluginDescription> {
public fun disable(plugin: P)
}
@Suppress("UNCHECKED_CAST")
@JvmSynthetic
public inline fun <D : PluginDescription, P : Plugin> PluginLoader<in P, out D>.getDescription(plugin: P): D =
plugin.description

View File

@ -13,6 +13,7 @@ package net.mamoe.mirai.console.plugin
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
import net.mamoe.mirai.console.plugin.dsecription.PluginDescription
import java.io.File
import java.nio.file.Path

View File

@ -7,100 +7,19 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.plugin
package net.mamoe.mirai.console.plugin.dsecription
import com.vdurmont.semver4j.Semver
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToString
import net.mamoe.mirai.console.internal.data.map
import net.mamoe.yamlkt.Yaml
import net.mamoe.yamlkt.YamlDynamicSerializer
import java.io.File
/**
* 插件描述.
*
* @see Plugin
*/
public interface PluginDescription {
/**
* 插件类型. 将会决定加载顺序
*
* @see PluginKind
*/
public val kind: PluginKind
/**
* 插件名称.
*/
public val name: String
/**
* 插件作者, 允许为空
*/
public val author: String
/**
* 插件版本.
*
* 语法参考: ([语义化版本 2.0.0](https://semver.org/lang/zh-CN/))
*
* @see Semver 语义化版本. 允许 [宽松][Semver.SemverType.LOOSE] 类型版本.
*/
public val version: Semver
/**
* 插件信息, 允许为空
*/
public val info: String
/**
* 此插件依赖的其他插件, 将会在这些插件加载之后加载此插件
*
* @see PluginDependency
*/
public val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency>
}
/**
* 插件类型
*/
@Serializable(with = PluginKind.AsStringSerializer::class)
public enum class PluginKind {
/** 表示此插件提供一个 [PluginLoader], 应在加载其他 [NORMAL] 类型插件前加载 */
LOADER,
/** 表示此插件为一个通常的插件, 按照正常的依赖关系加载. */
NORMAL;
public object AsStringSerializer : KSerializer<PluginKind> by String.serializer().map(
serializer = { it.name },
deserializer = { str ->
values().firstOrNull {
it.name.equals(str, ignoreCase = true)
} ?: NORMAL
}
)
}
/**
* 插件的一个依赖的信息.
*
* YAML 格式下, 典型的插件依赖示例:
* ```yaml
* dependencies:
* - name: "依赖的插件名" # 依赖的插件名
* version: "" # 依赖的版本号, 支持 Apache Ivy 格式. null 或不指定时不限制版本
* isOptional: true # `true` 表示插件在找不到此依赖时也能正常加载
* - "SamplePlugin" # 名称为 SamplePlugin 的插件, 不限制版本, isOptional=false
* - "TestPlugin:1.0.0+" # 名称为 ExamplePlugin 的插件, 版本至少为 1.0.0, isOptional=false
* - "ExamplePlugin:1.5.0+?" # 名称为 ExamplePlugin 的插件, 版本至少为 1.5.0, 末尾 `?` 表示 isOptional=true
* - "Another test plugin:[1.0.0, 2.0.0)" # 名称为 Another test plugin 的插件, 版本要求大于等于 1.0.0, 小于 2.0.0, isOptional=false
* ```
*
* @see PluginDescription.dependencies
*/
@Serializable(with = PluginDependency.SmartSerializer::class)
@ -121,7 +40,7 @@ public data class PluginDependency(
public val isOptional: Boolean = false
) {
public override fun toString(): String {
return "$name v$version"
return "$name v$version${if (isOptional) "?" else ""}"
}
@ -157,11 +76,4 @@ public data class PluginDependency(
}
}
)
}
/**
* 基于文件的插件 的描述
*/
public interface FilePluginDescription : PluginDescription {
public val file: File
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.plugin.dsecription
import com.vdurmont.semver4j.Semver
import kotlinx.serialization.Serializable
import net.mamoe.mirai.console.plugin.Plugin
/**
* 插件描述.
*
* @see Plugin
*/
public interface PluginDescription {
/**
* 插件类型. 将会决定加载顺序
*
* @see PluginKind
*/
public val kind: PluginKind
/**
* 插件名称. 不允许存在 ":"
*/
public val name: String
/**
* 插件作者, 允许为空
*/
public val author: String
/**
* 插件版本.
*
* 语法参考: ([语义化版本 2.0.0](https://semver.org/lang/zh-CN/))
*
* @see Semver 语义化版本. 允许 [宽松][Semver.SemverType.LOOSE] 类型版本.
*/
public val version: Semver
/**
* 插件信息, 允许为空
*/
public val info: String
/**
* 此插件依赖的其他插件, 将会在这些插件加载之后加载此插件
*
* @see PluginDependency
*/
public val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency>
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.plugin.dsecription
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.internal.data.map
import net.mamoe.mirai.console.plugin.PluginLoader
/**
* 插件类型
*/
@Serializable(with = PluginKind.AsStringSerializer::class)
public enum class PluginKind {
/** 表示此插件提供一个 [PluginLoader], 应在加载其他 [NORMAL] 类型插件前加载 */
LOADER,
/** 表示此插件为一个通常的插件, 按照正常的依赖关系加载. */
NORMAL;
public object AsStringSerializer : KSerializer<PluginKind> by String.serializer().map(
serializer = { it.name },
deserializer = { str ->
values().firstOrNull {
it.name.equals(str, ignoreCase = true)
} ?: NORMAL
}
)
}

View File

@ -43,7 +43,7 @@ public interface JvmPlugin : Plugin, CoroutineScope,
public val logger: MiraiLogger
/** 插件描述 */
public override val description: JvmPluginDescription
public val description: JvmPluginDescription
/** 所属插件加载器实例 */
@JvmDefault

View File

@ -14,9 +14,9 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.mamoe.mirai.console.internal.data.SemverAsStringSerializerLoose
import net.mamoe.mirai.console.plugin.PluginDependency
import net.mamoe.mirai.console.plugin.PluginDescription
import net.mamoe.mirai.console.plugin.PluginKind
import net.mamoe.mirai.console.plugin.dsecription.PluginDependency
import net.mamoe.mirai.console.plugin.dsecription.PluginDescription
import net.mamoe.mirai.console.plugin.dsecription.PluginKind
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import java.io.File
@ -32,7 +32,11 @@ public data class JvmMemoryPluginDescription(
public override val version: Semver,
public override val info: String,
public override val dependencies: List<PluginDependency>
) : JvmPluginDescription
) : JvmPluginDescription {
init {
require(!name.contains(':')) { "':' is forbidden in plugin name" }
}
}
/**
* JVM 插件的描述. 通常作为 `plugin.yml`
@ -72,6 +76,9 @@ public data class JvmMemoryPluginDescription(
*/
public interface JvmPluginDescription : PluginDescription
/**
* @see JvmPluginDescriptionImpl
*/
@MiraiExperimentalAPI
@Serializable
public class JvmPluginDescriptionImpl internal constructor(
@ -86,6 +93,10 @@ public class JvmPluginDescriptionImpl internal constructor(
public override val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency> = listOf()
) : JvmPluginDescription {
init {
require(!name.contains(':')) { "':' is forbidden in plugin name" }
}
/**
* 在手动实现时使用这个构造器.
*/