[Gradle] Implement UnusedSourcesAndDocumentationFilter
^KT-55289 Verification Pending
This commit is contained in:
committed by
Space Team
parent
79fc191c20
commit
d7ba6916b5
+7
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPro
|
|||||||
import org.jetbrains.kotlin.gradle.plugin.ide.IdeMultiplatformImport.SourceSetConstraint
|
import org.jetbrains.kotlin.gradle.plugin.ide.IdeMultiplatformImport.SourceSetConstraint
|
||||||
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyResolvers.*
|
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyResolvers.*
|
||||||
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyTransformers.IdePlatformStdlibCommonDependencyFilter
|
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyTransformers.IdePlatformStdlibCommonDependencyFilter
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyTransformers.UnusedSourcesAndDocumentationFilter
|
||||||
|
|
||||||
internal fun IdeMultiplatformImport(extension: KotlinProjectExtension): IdeMultiplatformImport {
|
internal fun IdeMultiplatformImport(extension: KotlinProjectExtension): IdeMultiplatformImport {
|
||||||
return IdeMultiplatformImportImpl(extension).apply {
|
return IdeMultiplatformImportImpl(extension).apply {
|
||||||
@@ -131,6 +132,12 @@ internal fun IdeMultiplatformImport(extension: KotlinProjectExtension): IdeMulti
|
|||||||
phase = IdeMultiplatformImport.DependencyTransformationPhase.DependencyFilteringPhase,
|
phase = IdeMultiplatformImport.DependencyTransformationPhase.DependencyFilteringPhase,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
registerDependencyTransformer(
|
||||||
|
transformer = UnusedSourcesAndDocumentationFilter,
|
||||||
|
constraint = SourceSetConstraint.unconstrained,
|
||||||
|
phase = IdeMultiplatformImport.DependencyTransformationPhase.DependencyFilteringPhase
|
||||||
|
)
|
||||||
|
|
||||||
registerDependencyEffect(
|
registerDependencyEffect(
|
||||||
effect = IdeDependencyLogger,
|
effect = IdeDependencyLogger,
|
||||||
constraint = SourceSetConstraint.unconstrained
|
constraint = SourceSetConstraint.unconstrained
|
||||||
|
|||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.plugin.ide.dependencyTransformers
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinBinaryCoordinates
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinDependency
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinDependency.Companion.CLASSPATH_BINARY_TYPE
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinDependency.Companion.DOCUMENTATION_BINARY_TYPE
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinDependency.Companion.SOURCES_BINARY_TYPE
|
||||||
|
import org.jetbrains.kotlin.gradle.idea.tcs.IdeaKotlinResolvedBinaryDependency
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.ide.IdeDependencyTransformer
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.ide.dependencyResolvers.IdeArtifactResolutionQuerySourcesAndDocumentationResolver
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters out -sources.jar and -javadoc.jar dependencies that are actually not necessary for the KotlinSourceSet:
|
||||||
|
* This counter-acts the fact that the [IdeArtifactResolutionQuerySourcesAndDocumentationResolver] is potentially "over-resolving" sources
|
||||||
|
* for multiplatform libraries.
|
||||||
|
*/
|
||||||
|
internal object UnusedSourcesAndDocumentationFilter : IdeDependencyTransformer {
|
||||||
|
|
||||||
|
override fun transform(sourceSet: KotlinSourceSet, dependencies: Set<IdeaKotlinDependency>): Set<IdeaKotlinDependency> {
|
||||||
|
val binaryDependencies = dependencies.filterIsInstance<IdeaKotlinResolvedBinaryDependency>()
|
||||||
|
|
||||||
|
val classpathBinaryCoordinates = binaryDependencies.filter { it.binaryType == CLASSPATH_BINARY_TYPE }
|
||||||
|
.mapNotNull { it.coordinates?.relevantString() }
|
||||||
|
.toSet()
|
||||||
|
|
||||||
|
val unusedDependencies = binaryDependencies.filter { it.binaryType in sourcesAndDocumentationBinaryTypes }
|
||||||
|
.filter { it.coordinates?.relevantString() !in classpathBinaryCoordinates }
|
||||||
|
.toSet()
|
||||||
|
|
||||||
|
return dependencies - unusedDependencies
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private val sourcesAndDocumentationBinaryTypes = setOf(SOURCES_BINARY_TYPE, DOCUMENTATION_BINARY_TYPE)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This filter only works based upon group, module and version.
|
||||||
|
* We can consider the sourceSetName to be irrelevant, since we do not have -sources.jar published on a 'per source set' level.
|
||||||
|
*/
|
||||||
|
private fun IdeaKotlinBinaryCoordinates.relevantString(): String = "$group:$module:$version"
|
||||||
|
}
|
||||||
|
|
||||||
+1
-19
@@ -66,10 +66,6 @@ class IdeSourcesAndDocumentationResolutionTest {
|
|||||||
binaryCoordinates("com.arkivanov.mvikotlin:mvikotlin:3.0.2"),
|
binaryCoordinates("com.arkivanov.mvikotlin:mvikotlin:3.0.2"),
|
||||||
binaryCoordinates("com.arkivanov.essenty:lifecycle:0.4.2"),
|
binaryCoordinates("com.arkivanov.essenty:lifecycle:0.4.2"),
|
||||||
binaryCoordinates("com.arkivanov.essenty:instance-keeper:0.4.2"),
|
binaryCoordinates("com.arkivanov.essenty:instance-keeper:0.4.2"),
|
||||||
|
|
||||||
/* sources.jar resolution using ArtifactResolutionQuery will resolve those additionally */
|
|
||||||
binaryCoordinates("org.jetbrains:annotations:13.0"),
|
|
||||||
binaryCoordinates(Regex(escape("org.jetbrains.kotlin:kotlin-stdlib") + ".*"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val resolvedDependencies = resolveDependencySources(commonMain)
|
val resolvedDependencies = resolveDependencySources(commonMain)
|
||||||
@@ -84,11 +80,7 @@ class IdeSourcesAndDocumentationResolutionTest {
|
|||||||
binaryCoordinates("com.arkivanov.mvikotlin:mvikotlin:3.0.2"),
|
binaryCoordinates("com.arkivanov.mvikotlin:mvikotlin:3.0.2"),
|
||||||
binaryCoordinates("com.arkivanov.essenty:lifecycle:0.4.2"),
|
binaryCoordinates("com.arkivanov.essenty:lifecycle:0.4.2"),
|
||||||
binaryCoordinates("com.arkivanov.essenty:instance-keeper:0.4.2"),
|
binaryCoordinates("com.arkivanov.essenty:instance-keeper:0.4.2"),
|
||||||
IdeNativeStdlibDependencyResolver.nativeStdlibCoordinates(project),
|
IdeNativeStdlibDependencyResolver.nativeStdlibCoordinates(project)
|
||||||
|
|
||||||
/* sources.jar resolution using ArtifactResolutionQuery will resolve those additionally */
|
|
||||||
binaryCoordinates("org.jetbrains:annotations:13.0"),
|
|
||||||
binaryCoordinates(Regex(escape("org.jetbrains.kotlin:kotlin-stdlib") + ".*"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val resolvedDependencies = resolveDependencySources(nativeMain)
|
val resolvedDependencies = resolveDependencySources(nativeMain)
|
||||||
@@ -100,16 +92,6 @@ class IdeSourcesAndDocumentationResolutionTest {
|
|||||||
/* Check linuxX64Main and linuxX64Test */
|
/* Check linuxX64Main and linuxX64Test */
|
||||||
run {
|
run {
|
||||||
val expectedDependencies = listOf(
|
val expectedDependencies = listOf(
|
||||||
/* Artifact Query is 'over providing' */
|
|
||||||
listOf(
|
|
||||||
binaryCoordinates("com.arkivanov.mvikotlin:mvikotlin:3.0.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.mvikotlin:utils-internal:3.0.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.mvikotlin:rx:3.0.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.mvikotlin:rx-internal:3.0.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.essenty:utils-internal:0.4.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.essenty:lifecycle:0.4.2"),
|
|
||||||
binaryCoordinates("com.arkivanov.essenty:instance-keeper:0.4.2")
|
|
||||||
),
|
|
||||||
|
|
||||||
/* Required dependencies */
|
/* Required dependencies */
|
||||||
listOf(
|
listOf(
|
||||||
|
|||||||
Reference in New Issue
Block a user