Skip to content

Commit 4823fc3

Browse files
sellmairSpace Team
authored and
Space Team
committed
[Gradle] GranularMetadataTransformation: Collect moduleIds in ProjectData for non Kotlin Projects
^KT-59446 Verification Pending (cherry picked from commit 685eee3)
1 parent 0d1932e commit 4823fc3

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
}
@@ -334,13 +335,33 @@ private val Project.allProjectsData: Map<String, GranularMetadataTransformation.
334335

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

0 commit comments

Comments
 (0)