Change file suffix of new plugins to '.mirai2.jar'. Ignore '.mirai.jar' if its '.mirai2.jar' counterparts loaded. Close #1999

This commit is contained in:
Him188 2022-05-01 17:59:05 +01:00
parent cb08f4789c
commit 6b5f9e2eef
15 changed files with 243 additions and 10 deletions

View File

@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#
mirai.jar.before211.nonew.After211NoNew

View File

@ -0,0 +1,31 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package mirai.jar.before211.nonew
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.id
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import kotlin.test.assertTrue
/*
* 2.11 及以后打包的插件, 无新插件替代
*/
internal object After211NoNew : KotlinPlugin(
JvmPluginDescription(
id = "net.mamoe.tester.before211.nonew",
version = "1.0.0",
name = "Test Plugin",
)
) {
override fun onEnable() {
assertTrue("net.mamoe.tester.after211 is not loaded") { PluginManager.plugins.any { it.id == "net.mamoe.tester.after211" } }
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("UnusedImport")
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
id("java")
}
version = "0.0.0"
kotlin {
explicitApiWarning()
}
dependencies {
api(project(":mirai-console.integration-test"))
}
tasks.getByName("jar", Jar::class) {
archiveBaseName.set("mirai-jar-compatibility-211")
archiveExtension.set("mirai2.jar")
}

View File

@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#
mirai.jar.after211.After211

View File

@ -0,0 +1,33 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package mirai.jar.after211
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.id
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import kotlin.test.assertFalse
import kotlin.test.assertTrue
/*
* 2.11 及以后打包的插件, net.mamoe.tester.after211 替代
*/
internal object After211 : KotlinPlugin(
JvmPluginDescription(
id = "net.mamoe.tester.after211",
version = "1.0.0",
name = "Test Plugin",
)
) {
override fun onEnable() {
assertFalse("net.mamoe.tester.before211 is loaded") { PluginManager.plugins.any { it.id == "net.mamoe.tester.before211" } }
assertTrue("net.mamoe.tester.before211.nonew is not loaded") { PluginManager.plugins.any { it.id == "net.mamoe.tester.before211.nonew" } }
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("UnusedImport")
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
id("java")
}
version = "0.0.0"
kotlin {
explicitApiWarning()
}
dependencies {
api(project(":mirai-console.integration-test"))
}
tasks.getByName("jar", Jar::class) {
archiveBaseName.set("mirai-jar-compatibility-211")
archiveExtension.set("mirai.jar")
}

View File

@ -0,0 +1,10 @@
#
# Copyright 2019-2022 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/dev/LICENSE
#
mirai.jar.before211.Before211

View File

@ -0,0 +1,32 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package mirai.jar.before211
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.id
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import kotlin.test.assertFalse
/*
* 2.11 以前打包的插件
*/
internal object Before211 : KotlinPlugin(
JvmPluginDescription(
id = "net.mamoe.tester.before211",
version = "1.0.0",
name = "Test Plugin",
)
) {
override fun onEnable() {
assertFalse("Both before211 and after211 are loaded.") { PluginManager.plugins.any { it.id == "net.mamoe.tester.after211" } }
throw AssertionError("Only net.mamoe.tester.before211 is loaded.")
}
}

View File

@ -2288,7 +2288,7 @@ public abstract class net/mamoe/mirai/console/plugin/loader/AbstractFilePluginLo
public fun <init> (Ljava/lang/String;)V
protected abstract fun extractPlugins (Lkotlin/sequences/Sequence;)Ljava/util/List;
public fun getFileSuffix ()Ljava/lang/String;
public final fun listPlugins ()Ljava/util/List;
public fun listPlugins ()Ljava/util/List;
}
public abstract interface class net/mamoe/mirai/console/plugin/loader/FilePluginLoader : net/mamoe/mirai/console/plugin/loader/PluginLoader {

View File

@ -25,7 +25,6 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoadException
import net.mamoe.mirai.console.plugin.name
import net.mamoe.mirai.utils.*
import java.io.File
import java.io.InputStream
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.CoroutineContext
@ -44,6 +43,28 @@ internal class BuiltInJvmPluginLoaderImpl(
internal val logger: MiraiLogger = MiraiConsole.createLogger(JvmPluginLoader::class.simpleName!!)
}
fun pluginsFilesSequence(
files: Sequence<File> = PluginManager.pluginsFolder.listFiles().orEmpty().asSequence()
): Sequence<File> {
val raw = files
.filter { it.isFile && it.name.endsWith(fileSuffix, ignoreCase = true) }
.toMutableList()
val mirai2List = raw.filter { it.name.endsWith(".mirai2.jar", ignoreCase = true) }
for (mirai2Plugin in mirai2List) {
val name = mirai2Plugin.name.substringBeforeLast('.').substringBeforeLast('.') // without ext.
raw.removeAll {
it !== mirai2Plugin && it.name.substringBeforeLast('.').substringBeforeLast('.') == name
} // remove those with .mirai.jar
}
return raw.asSequence()
}
override fun listPlugins(): List<JvmPlugin> {
return pluginsFilesSequence().extractPlugins()
}
override val configStorage: PluginDataStorage
get() = MiraiConsoleImplementation.getInstance().configStorageForJvmPluginLoader

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -13,6 +13,7 @@ import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.description.PluginDescription
import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader
import net.mamoe.mirai.utils.NotStableForInheritance
import java.io.File
/**
@ -48,5 +49,6 @@ public abstract class AbstractFilePluginLoader<P : Plugin, D : PluginDescription
*/
protected abstract fun Sequence<File>.extractPlugins(): List<P>
public final override fun listPlugins(): List<P> = pluginsFilesSequence().extractPlugins()
@NotStableForInheritance // made non-final in 2.11
public override fun listPlugins(): List<P> = pluginsFilesSequence().extractPlugins()
}

View File

@ -0,0 +1,16 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.console.plugin
import net.mamoe.mirai.console.testFramework.AbstractConsoleInstanceTest
internal class BuiltInJvmPluginLoaderImplTest : AbstractConsoleInstanceTest() {
}

View File

@ -204,7 +204,7 @@ class TestBuildPlugin : AbstractTest() {
}
}
private fun findJar(): File = tempDir.resolve("build/mirai").listFiles()!!.first { it.name.endsWith(".mirai.jar") }
private fun findJar(): File = tempDir.resolve("build/mirai").listFiles()!!.first { it.name.endsWith(".mirai2.jar") }
private fun checkOutput() {
val jar = findJar()

View File

@ -35,6 +35,9 @@ import javax.inject.Inject
@Suppress("RedundantLambdaArrow", "RemoveExplicitTypeArguments")
public open class BuildMiraiPluginV2 : Jar() {
public companion object {
public const val FILE_SUFFIX: String = "mirai2.jar"
}
// @get:Internal
private lateinit var metadataTask: GenMetadataTask
@ -243,7 +246,7 @@ public open class BuildMiraiPluginV2 : Jar() {
internal fun init(target: KotlinTarget) {
dependsOn(metadataTask)
archiveExtension.set("mirai.jar")
archiveExtension.set(FILE_SUFFIX)
duplicatesStrategy = DuplicatesStrategy.WARN
val compilations = target.compilations.filter { it.name == KotlinCompilation.MAIN_COMPILATION_NAME }
@ -256,4 +259,5 @@ public open class BuildMiraiPluginV2 : Jar() {
elm.path.startsWith("META-INF/") && elm.name.endsWith(".sf", ignoreCase = true)
}
}
}

View File

@ -30,9 +30,11 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
public class MiraiConsoleGradlePlugin : Plugin<Project> {
internal companion object {
const val MIRAI_SHADOW_CONF_NAME: String = "shadowLink"
const val MIRAI_AS_NORMAL_DEP_CONF_NAME: String = "asNormalDep"
public companion object {
internal const val MIRAI_SHADOW_CONF_NAME: String = "shadowLink"
internal const val MIRAI_AS_NORMAL_DEP_CONF_NAME: String = "asNormalDep"
public const val FILE_SUFFIX: String = "mirai.jar"
}
private fun KotlinSourceSet.configureSourceSet(project: Project, target: KotlinTarget) {
@ -137,7 +139,7 @@ public class MiraiConsoleGradlePlugin : Plugin<Project> {
).apply shadow@{
group = "mirai"
archiveExtension.set("legacy.mirai.jar")
archiveExtension.set(FILE_SUFFIX)
val compilations = target.compilations.filter { it.name == MAIN_COMPILATION_NAME }