Skip to content

Commit 685eee3

Browse files
sellmairSpace Team
authored and
Space Team
committed
[Gradle] GranularMetadataTransformation: Collect moduleIds in ProjectData for non Kotlin Projects
^KT-59446 Verification Pending
1 parent d3d6cdc commit 685eee3

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.gradle.api.attributes.AttributeContainer
1313
import org.gradle.api.file.FileCollection
1414
import org.gradle.api.logging.Logging
1515
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
16+
import org.jetbrains.kotlin.gradle.dsl.kotlinExtensionOrNull
1617
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
1718
import org.jetbrains.kotlin.gradle.plugin.*
1819
import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider
@@ -36,15 +37,15 @@ internal sealed class MetadataDependencyResolution(
3637
}
3738

3839
class KeepOriginalDependency(
39-
dependency: ResolvedComponentResult
40+
dependency: ResolvedComponentResult,
4041
) : MetadataDependencyResolution(dependency)
4142

4243
sealed class Exclude(
43-
dependency: ResolvedComponentResult
44+
dependency: ResolvedComponentResult,
4445
) : MetadataDependencyResolution(dependency) {
4546

4647
class Unrequested(
47-
dependency: ResolvedComponentResult
48+
dependency: ResolvedComponentResult,
4849
) : Exclude(dependency)
4950

5051
/**
@@ -65,7 +66,7 @@ internal sealed class MetadataDependencyResolution(
6566
val allVisibleSourceSetNames: Set<String>,
6667
val visibleSourceSetNamesExcludingDependsOn: Set<String>,
6768
val visibleTransitiveDependencies: Set<ResolvedDependencyResult>,
68-
internal val metadataProvider: MetadataProvider
69+
internal val metadataProvider: MetadataProvider,
6970
) : MetadataDependencyResolution(dependency) {
7071

7172
internal sealed class MetadataProvider {
@@ -89,7 +90,7 @@ internal sealed class MetadataDependencyResolution(
8990

9091
internal class GranularMetadataTransformation(
9192
val params: Params,
92-
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider
93+
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider,
9394
) {
9495
private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]")
9596

@@ -114,7 +115,7 @@ internal class GranularMetadataTransformation(
114115
class ProjectData(
115116
val path: String,
116117
val sourceSetMetadataOutputs: LenientFuture<Map<String, SourceSetMetadataOutputs>>,
117-
val moduleId: LenientFuture<ModuleDependencyIdentifier>
118+
val moduleId: LenientFuture<ModuleDependencyIdentifier>,
118119
) {
119120
override fun toString(): String = "ProjectData[path='$path']"
120121
}
@@ -325,13 +326,33 @@ private val Project.allProjectsData: Map<String, GranularMetadataTransformation.
325326

326327
private fun Project.collectAllProjectsData(): Map<String, GranularMetadataTransformation.ProjectData> {
327328
return rootProject.allprojects.associateBy { it.path }.mapValues { (path, currentProject) ->
329+
330+
/*
331+
We're calling into various different projects (Note: This implementation will change with Project Isolation)
332+
Since not all projects might have the Kotlin Gradle Plugin applied we do call into 'idOfRootModule' in two different ways:
333+
334+
1) If KGP is applied, we use the lifecycle APIs to safely get a value after the DSL was finalised.
335+
2) If KGP was *not* applied, we create a Future which
336+
- creates a lazy once the project is evaluated
337+
- only evaluates the lazy once the moduleId is actually accessed
338+
339+
This double deferral ensures that the value indeed is accessed as late as possible.
340+
Unwrapping the lazy before the buildscript is evaluated will fail with 'future not completed'
341+
*/
342+
val moduleId = if (currentProject.kotlinExtensionOrNull != null) currentProject.future {
343+
KotlinPluginLifecycle.Stage.AfterFinaliseDsl.await()
344+
ModuleIds.idOfRootModule(currentProject)
345+
}.lenient else CompletableFuture<Lazy<ModuleDependencyIdentifier>>().apply {
346+
currentProject.whenEvaluated {
347+
complete(lazy { ModuleIds.idOfRootModule(currentProject) })
348+
}
349+
}.map { it.value }.lenient
350+
351+
328352
GranularMetadataTransformation.ProjectData(
329353
path = path,
330354
sourceSetMetadataOutputs = currentProject.future { currentProject.collectSourceSetMetadataOutputs() }.lenient,
331-
moduleId = currentProject.future {
332-
KotlinPluginLifecycle.Stage.AfterFinaliseDsl.await()
333-
ModuleIds.idOfRootModule(currentProject)
334-
}.lenient
355+
moduleId = moduleId
335356
)
336357
}
337358
}

0 commit comments

Comments
 (0)