Fix shadowing libraries that another used; fix #2070

This commit is contained in:
Karlatemp 2022-06-08 00:19:08 +08:00
parent 75fea25e38
commit 8de404a69a
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
2 changed files with 65 additions and 4 deletions

View File

@ -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") }
}
}

View File

@ -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<String>) {
// 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())
}