Skip to content

Commit

Permalink
[Gradle] GranularMetadataTransformation: Collect moduleIds in Project…
Browse files Browse the repository at this point in the history
…Data for non Kotlin Projects

^KT-59446 Verification Pending

(cherry picked from commit 685eee3)
  • Loading branch information
sellmair authored and qodana-bot committed Jun 21, 2023
1 parent 0d1932e commit 4823fc3
Showing 1 changed file with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.Logging
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtensionOrNull
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider
Expand All @@ -36,15 +37,15 @@ internal sealed class MetadataDependencyResolution(
}

class KeepOriginalDependency(
dependency: ResolvedComponentResult
dependency: ResolvedComponentResult,
) : MetadataDependencyResolution(dependency)

sealed class Exclude(
dependency: ResolvedComponentResult
dependency: ResolvedComponentResult,
) : MetadataDependencyResolution(dependency) {

class Unrequested(
dependency: ResolvedComponentResult
dependency: ResolvedComponentResult,
) : Exclude(dependency)

/**
Expand All @@ -65,7 +66,7 @@ internal sealed class MetadataDependencyResolution(
val allVisibleSourceSetNames: Set<String>,
val visibleSourceSetNamesExcludingDependsOn: Set<String>,
val visibleTransitiveDependencies: Set<ResolvedDependencyResult>,
internal val metadataProvider: MetadataProvider
internal val metadataProvider: MetadataProvider,
) : MetadataDependencyResolution(dependency) {

internal sealed class MetadataProvider {
Expand All @@ -89,7 +90,7 @@ internal sealed class MetadataDependencyResolution(

internal class GranularMetadataTransformation(
val params: Params,
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider,
) {
private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]")

Expand All @@ -114,7 +115,7 @@ internal class GranularMetadataTransformation(
class ProjectData(
val path: String,
val sourceSetMetadataOutputs: LenientFuture<Map<String, SourceSetMetadataOutputs>>,
val moduleId: LenientFuture<ModuleDependencyIdentifier>
val moduleId: LenientFuture<ModuleDependencyIdentifier>,
) {
override fun toString(): String = "ProjectData[path='$path']"
}
Expand Down Expand Up @@ -334,13 +335,33 @@ private val Project.allProjectsData: Map<String, GranularMetadataTransformation.

private fun Project.collectAllProjectsData(): Map<String, GranularMetadataTransformation.ProjectData> {
return rootProject.allprojects.associateBy { it.path }.mapValues { (path, currentProject) ->

/*
We're calling into various different projects (Note: This implementation will change with Project Isolation)
Since not all projects might have the Kotlin Gradle Plugin applied we do call into 'idOfRootModule' in two different ways:
1) If KGP is applied, we use the lifecycle APIs to safely get a value after the DSL was finalised.
2) If KGP was *not* applied, we create a Future which
- creates a lazy once the project is evaluated
- only evaluates the lazy once the moduleId is actually accessed
This double deferral ensures that the value indeed is accessed as late as possible.
Unwrapping the lazy before the buildscript is evaluated will fail with 'future not completed'
*/
val moduleId = if (currentProject.kotlinExtensionOrNull != null) currentProject.future {
KotlinPluginLifecycle.Stage.AfterFinaliseDsl.await()
ModuleIds.idOfRootModule(currentProject)
}.lenient else CompletableFuture<Lazy<ModuleDependencyIdentifier>>().apply {
currentProject.whenEvaluated {
complete(lazy { ModuleIds.idOfRootModule(currentProject) })
}
}.map { it.value }.lenient


GranularMetadataTransformation.ProjectData(
path = path,
sourceSetMetadataOutputs = currentProject.future { currentProject.collectSourceSetMetadataOutputs() }.lenient,
moduleId = currentProject.future {
KotlinPluginLifecycle.Stage.AfterFinaliseDsl.await()
ModuleIds.idOfRootModule(currentProject)
}.lenient
moduleId = moduleId
)
}
}
Expand Down

0 comments on commit 4823fc3

Please sign in to comment.