From 8de404a69ac53019de9f3967d0bc0ed1d2acc261 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Wed, 8 Jun 2022 00:19:08 +0800 Subject: [PATCH] Fix shadowing libraries that another used; fix #2070 --- .../src/integTest/kotlin/TestBuildPlugin.kt | 53 +++++++++++++++++++ .../src/main/kotlin/BuildMiraiPluginV2.kt | 16 ++++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/mirai-console/tools/gradle-plugin/src/integTest/kotlin/TestBuildPlugin.kt b/mirai-console/tools/gradle-plugin/src/integTest/kotlin/TestBuildPlugin.kt index b9f6c37d1..0a8518eab 100644 --- a/mirai-console/tools/gradle-plugin/src/integTest/kotlin/TestBuildPlugin.kt +++ b/mirai-console/tools/gradle-plugin/src/integTest/kotlin/TestBuildPlugin.kt @@ -387,6 +387,58 @@ class TestBuildPlugin : AbstractTest() { } } + @Test + @DisplayName("Ktor 2.x available") + fun `ktor 2_x`() { + tempDir.resolve("build.gradle").appendText( + """ + dependencies { + implementation "io.ktor:ktor-client-core:2.0.0" + } + """.trimIndent() + ) + gradleRunner() + .withArguments("buildPlugin", "dependencies", "--stacktrace", "--info") + .build() + + ZipFile(findJar()).use { zipFile -> + + val dpPrivate = zipFile.getInputStream( + zipFile.getEntry("META-INF/mirai-console-plugin/dependencies-private.txt") + ).use { it.readBytes().decodeToString() } + + assertTrue { dpPrivate.contains("io.ktor:ktor-client-core:2.0.0") } + assertTrue { dpPrivate.contains("io.ktor:ktor-client-core-jvm:2.0.0") } + } + } + + @Test + @DisplayName("can shadow special libraries that another used") + fun issue2070() { + tempDir.resolve("build.gradle").appendText( + """ + dependencies { + implementation("cn.hutool:hutool-extra:5.8.2") + shadowLink("cn.hutool:hutool-core") + } + """.trimIndent() + ) + gradleRunner() + .withArguments("buildPlugin", "dependencies", "--stacktrace", "--info") + .build() + ZipFile(findJar()).use { zipFile -> + assertNotNull(zipFile.getEntry("cn/hutool/core/annotation/Alias.class")) + + + val dpPrivate = zipFile.getInputStream( + zipFile.getEntry("META-INF/mirai-console-plugin/dependencies-private.txt") + ).use { it.readBytes().decodeToString() } + + assertFalse { dpPrivate.contains("hutool-core") } + } + + } + private fun findJar(): File = tempDir.resolve("build/mirai").listFiles()!!.first { it.name.endsWith(".mirai2.jar") } private fun checkOutput() { @@ -409,6 +461,7 @@ class TestBuildPlugin : AbstractTest() { assertTrue { dpPrivate.contains("com.zaxxer:SparseBitSet:1.2") } assertTrue { dpPrivate.contains("com.google.code.gson:gson:2.8.9") } assertFalse { dpPrivate.contains("org.slf4j:slf4j-simple") } + assertFalse { dpPrivate.contains("io.ktor") } } } diff --git a/mirai-console/tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginV2.kt b/mirai-console/tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginV2.kt index 18f288b3c..7c08944ab 100644 --- a/mirai-console/tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginV2.kt +++ b/mirai-console/tools/gradle-plugin/src/main/kotlin/BuildMiraiPluginV2.kt @@ -87,8 +87,12 @@ public open class BuildMiraiPluginV2 : Jar() { if (dep is ProjectDependency) { linkedDependencies.add("${dep.group}:${dep.name}") subprojects_linked_fullpath.add(dep.dependencyProject.path) - dep.dependencyProject.configurations.findByName("apiElements")?.allDependencies?.forEach { resolve0(it) } - dep.dependencyProject.configurations.findByName("implementation")?.allDependencies?.forEach { resolve0(it) } + dep.dependencyProject.configurations.findByName("apiElements")?.allDependencies?.forEach { + resolve0(it) + } + dep.dependencyProject.configurations.findByName("implementation")?.allDependencies?.forEach { + resolve0(it) + } } } resolve0(dep1) @@ -143,14 +147,18 @@ public open class BuildMiraiPluginV2 : Jar() { val runtimeClasspath = project.configurations["runtimeClasspath"].resolvedConfiguration fun markAsResolved(resolvedDependency: ResolvedDependency) { val depId = resolvedDependency.depId() - linkedDependencies.add(depId) + if (depId !in shadowedDependencies) { + linkedDependencies.add(depId) + } resolvedDependency.children.forEach { markAsResolved(it) } } fun linkDependencyTo(resolvedDependency: ResolvedDependency, dependencies: MutableCollection) { // bom files - if (resolvedDependency.allModuleArtifacts.any { it.extension == "jar" }) { + if (resolvedDependency.allModuleArtifacts.any { it.extension == "jar" }) kotlin.run link@{ + if (resolvedDependency.depId() in shadowedDependencies) return@link + dependencies.add(resolvedDependency.module.toString()) }