[Gradle][Minor] Improve readability of SourceSetVisibilityProvider
This commit is contained in:
committed by
Space Team
parent
b1b744b863
commit
85473068c9
+35
-19
@@ -8,7 +8,8 @@ package org.jetbrains.kotlin.gradle.plugin.mpp
|
|||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.artifacts.result.ResolvedDependencyResult
|
import org.gradle.api.artifacts.result.ResolvedDependencyResult
|
||||||
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
|
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
|
||||||
import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration
|
import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration
|
||||||
import org.jetbrains.kotlin.gradle.utils.dependencyArtifactsOrNull
|
import org.jetbrains.kotlin.gradle.utils.dependencyArtifactsOrNull
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -76,26 +77,41 @@ internal class SourceSetVisibilityProvider(
|
|||||||
dependencyProjectStructureMetadata: KotlinProjectStructureMetadata,
|
dependencyProjectStructureMetadata: KotlinProjectStructureMetadata,
|
||||||
resolvedToOtherProject: Boolean
|
resolvedToOtherProject: Boolean
|
||||||
): SourceSetVisibilityResult {
|
): SourceSetVisibilityResult {
|
||||||
val component = resolvedRootMppDependency.selected
|
val resolvedRootMppDependencyId = resolvedRootMppDependency.selected.id
|
||||||
val componentId = component.id
|
|
||||||
|
|
||||||
val firstConfigurationByVariant = mutableMapOf<String, PlatformCompilationData>()
|
val platformCompilationsByResolvedVariantName = mutableMapOf<String, PlatformCompilationData>()
|
||||||
|
|
||||||
val visiblePlatformVariantNames: Set<String?> =
|
val visiblePlatformVariantNames: Set<String?> = platformCompilations
|
||||||
platformCompilations
|
.filter { visibleFromSourceSet in it.allSourceSets }
|
||||||
.filter { visibleFromSourceSet in it.allSourceSets }
|
.map { platformCompilationData ->
|
||||||
.mapTo(mutableSetOf()) { resolvedConfiguration ->
|
val resolvedPlatformDependency = platformCompilationData
|
||||||
val resolvedVariant = resolvedConfiguration
|
.resolvedDependenciesConfiguration
|
||||||
.resolvedDependenciesConfiguration
|
.allResolvedDependencies
|
||||||
.allResolvedDependencies
|
.find { it.selected.id == resolvedRootMppDependencyId }
|
||||||
.find { it.selected.id == componentId }
|
/*
|
||||||
?.let { kotlinVariantNameFromPublishedVariantName(it.resolvedVariant.displayName) }
|
Returning null if we can't find the given dependency in a certain platform compilations dependencies.
|
||||||
?: return@mapTo null
|
This is not expected, since this means the dependency does not support the given targets which will
|
||||||
|
lead to a dependency resolution error.
|
||||||
|
|
||||||
firstConfigurationByVariant.putIfAbsent(resolvedVariant, resolvedConfiguration)
|
Esoteric cases can still get into this branch: e.g. broken publications (or broken .m2 and mavenLocal()).
|
||||||
resolvedVariant
|
In this case we just return null, effectively ignoring this situation for this algorithm.
|
||||||
|
|
||||||
|
Ignoring this will still lead to a more graceful behaviour in the IDE.
|
||||||
|
A broken publication will potentially lead to 'too many' source sets being visible, which is
|
||||||
|
more desirable than having none.
|
||||||
|
*/ ?: return@map null
|
||||||
|
|
||||||
|
val resolvedVariant = kotlinVariantNameFromPublishedVariantName(
|
||||||
|
resolvedPlatformDependency.resolvedVariant.displayName
|
||||||
|
)
|
||||||
|
|
||||||
|
if (resolvedVariant !in platformCompilationsByResolvedVariantName) {
|
||||||
|
platformCompilationsByResolvedVariantName[resolvedVariant] = platformCompilationData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolvedVariant
|
||||||
|
}.toSet()
|
||||||
|
|
||||||
if (visiblePlatformVariantNames.isEmpty()) {
|
if (visiblePlatformVariantNames.isEmpty()) {
|
||||||
return SourceSetVisibilityResult(emptySet(), emptyMap())
|
return SourceSetVisibilityResult(emptySet(), emptyMap())
|
||||||
}
|
}
|
||||||
@@ -124,20 +140,20 @@ internal class SourceSetVisibilityProvider(
|
|||||||
val someVariantByHostSpecificSourceSet =
|
val someVariantByHostSpecificSourceSet =
|
||||||
hostSpecificSourceSets.associate { sourceSetName ->
|
hostSpecificSourceSets.associate { sourceSetName ->
|
||||||
sourceSetName to dependencyProjectStructureMetadata.sourceSetNamesByVariantName
|
sourceSetName to dependencyProjectStructureMetadata.sourceSetNamesByVariantName
|
||||||
.filterKeys { it in firstConfigurationByVariant }
|
.filterKeys { it in platformCompilationsByResolvedVariantName }
|
||||||
.filterValues { sourceSetName in it }
|
.filterValues { sourceSetName in it }
|
||||||
.keys.first()
|
.keys.first()
|
||||||
}
|
}
|
||||||
|
|
||||||
someVariantByHostSpecificSourceSet.entries.mapNotNull { (sourceSetName, variantName) ->
|
someVariantByHostSpecificSourceSet.entries.mapNotNull { (sourceSetName, variantName) ->
|
||||||
val resolvedHostSpecificMetadataConfiguration = firstConfigurationByVariant
|
val resolvedHostSpecificMetadataConfiguration = platformCompilationsByResolvedVariantName
|
||||||
.getValue(variantName)
|
.getValue(variantName)
|
||||||
.hostSpecificMetadataConfiguration
|
.hostSpecificMetadataConfiguration
|
||||||
?: return@mapNotNull null
|
?: return@mapNotNull null
|
||||||
|
|
||||||
val dependency = resolvedHostSpecificMetadataConfiguration
|
val dependency = resolvedHostSpecificMetadataConfiguration
|
||||||
.allResolvedDependencies
|
.allResolvedDependencies
|
||||||
.find { it.selected.id == componentId }
|
.find { it.selected.id == resolvedRootMppDependencyId }
|
||||||
?: return@mapNotNull null
|
?: return@mapNotNull null
|
||||||
|
|
||||||
val metadataArtifact = resolvedHostSpecificMetadataConfiguration
|
val metadataArtifact = resolvedHostSpecificMetadataConfiguration
|
||||||
|
|||||||
Reference in New Issue
Block a user