Deduplicate logic of computing orphan source sets in MPP Gradle Import

Set of compiled source sets is already available in
MultiplatformModelImportingContext via
'sourceSetsToParticipatedCompilations.keys', no need to recompute it

^KT-37127
This commit is contained in:
Dmitry Savvinov
2020-12-16 14:26:34 +03:00
parent 1d0a696a62
commit 78b2eb994b
2 changed files with 18 additions and 10 deletions
@@ -84,15 +84,7 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
): Map<String, KotlinSourceSetImpl> {
if (importingContext.getProperty(IMPORT_ORPHAN_SOURCE_SETS)) return importingContext.sourceSetsByNames
// TODO: similar to importingContext.sourceSetsByCompilations.keys, will be deduplicated in further commits
val compiledSourceSets: Collection<String> =
importingContext.targets
.flatMap { it.compilations }
.flatMap { it.sourceSets }
.flatMap { it.dependsOnSourceSets.union(listOf(it.name)) }
.distinct()
val (orphanSourceSets, nonOrphanSourceSets) = importingContext.sourceSets.partition { !compiledSourceSets.contains(it.name) }
val (orphanSourceSets, nonOrphanSourceSets) = importingContext.sourceSets.partition { importingContext.isOrphanSourceSet(it) }
orphanSourceSets.forEach {
logger.warn("[sync warning] Source set \"${it.name}\" is not compiled with any compilation. This source set is not imported in the IDE.")
@@ -17,12 +17,25 @@ internal interface MultiplatformModelImportingContext {
val targets: Collection<KotlinTarget>
val compilations: Collection<KotlinCompilation>
/**
* All source sets in a project, including those that are created but not included into any compilations
* (so-called "orphan" source sets). Use [isOrphanSourceSet] to get only compiled source sets
*/
val sourceSets: Collection<KotlinSourceSetImpl>
val sourceSetsByNames: Map<String, KotlinSourceSetImpl>
fun sourceSetByName(name: String): KotlinSourceSet?
fun compilationsBySourceSet(sourceSet: KotlinSourceSet): Collection<KotlinCompilation>?
/**
* "Orphan" is a source set which is not actually compiled by the compiler, i.e. the one
* which doesn't belong to any [KotlinCompilation].
*
* Orphan source sets might appear if one creates a source-set manually and doesn't link
* it anywhere (essentially this is a misconfiguration)
*/
fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = compilationsBySourceSet(sourceSet) == null
}
internal fun MultiplatformModelImportingContext.getProperty(property: GradleImportProperties): Boolean = project.getProperty(property)
@@ -99,6 +112,9 @@ internal class MultiplatformModelImportingContextImpl(override val project: Proj
this.targets = targets
}
// overload for small optimization
override fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = sourceSet in sourceSetToParticipatedCompilations.keys
override fun compilationsBySourceSet(sourceSet: KotlinSourceSet): Collection<KotlinCompilation>? =
sourceSetToParticipatedCompilations[sourceSet]