@@ -13,6 +13,7 @@ import org.gradle.api.attributes.AttributeContainer
13
13
import org.gradle.api.file.FileCollection
14
14
import org.gradle.api.logging.Logging
15
15
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
16
+ import org.jetbrains.kotlin.gradle.dsl.kotlinExtensionOrNull
16
17
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
17
18
import org.jetbrains.kotlin.gradle.plugin.*
18
19
import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider
@@ -36,15 +37,15 @@ internal sealed class MetadataDependencyResolution(
36
37
}
37
38
38
39
class KeepOriginalDependency (
39
- dependency : ResolvedComponentResult
40
+ dependency : ResolvedComponentResult ,
40
41
) : MetadataDependencyResolution(dependency)
41
42
42
43
sealed class Exclude (
43
- dependency : ResolvedComponentResult
44
+ dependency : ResolvedComponentResult ,
44
45
) : MetadataDependencyResolution(dependency) {
45
46
46
47
class Unrequested (
47
- dependency : ResolvedComponentResult
48
+ dependency : ResolvedComponentResult ,
48
49
) : Exclude(dependency)
49
50
50
51
/* *
@@ -65,7 +66,7 @@ internal sealed class MetadataDependencyResolution(
65
66
val allVisibleSourceSetNames : Set <String >,
66
67
val visibleSourceSetNamesExcludingDependsOn : Set <String >,
67
68
val visibleTransitiveDependencies : Set <ResolvedDependencyResult >,
68
- internal val metadataProvider : MetadataProvider
69
+ internal val metadataProvider : MetadataProvider ,
69
70
) : MetadataDependencyResolution(dependency) {
70
71
71
72
internal sealed class MetadataProvider {
@@ -89,7 +90,7 @@ internal sealed class MetadataDependencyResolution(
89
90
90
91
internal class GranularMetadataTransformation (
91
92
val params : Params ,
92
- val parentSourceSetVisibilityProvider : ParentSourceSetVisibilityProvider
93
+ val parentSourceSetVisibilityProvider : ParentSourceSetVisibilityProvider ,
93
94
) {
94
95
private val logger = Logging .getLogger(" GranularMetadataTransformation[${params.sourceSetName} ]" )
95
96
@@ -114,7 +115,7 @@ internal class GranularMetadataTransformation(
114
115
class ProjectData (
115
116
val path : String ,
116
117
val sourceSetMetadataOutputs : LenientFuture <Map <String , SourceSetMetadataOutputs >>,
117
- val moduleId : LenientFuture <ModuleDependencyIdentifier >
118
+ val moduleId : LenientFuture <ModuleDependencyIdentifier >,
118
119
) {
119
120
override fun toString (): String = " ProjectData[path='$path ']"
120
121
}
@@ -334,13 +335,33 @@ private val Project.allProjectsData: Map<String, GranularMetadataTransformation.
334
335
335
336
private fun Project.collectAllProjectsData (): Map <String , GranularMetadataTransformation .ProjectData > {
336
337
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
+
337
361
GranularMetadataTransformation .ProjectData (
338
362
path = path,
339
363
sourceSetMetadataOutputs = currentProject.future { currentProject.collectSourceSetMetadataOutputs() }.lenient,
340
- moduleId = currentProject.future {
341
- KotlinPluginLifecycle .Stage .AfterFinaliseDsl .await()
342
- ModuleIds .idOfRootModule(currentProject)
343
- }.lenient
364
+ moduleId = moduleId
344
365
)
345
366
}
346
367
}
0 commit comments