[Gradle] Introduce isProjectComponentIdentifierInCurrentBuild

KT-58471 Verification Pending
This commit is contained in:
Anton Lakotka
2023-05-19 13:48:56 +02:00
committed by Space Team
parent 9ead1d84b1
commit 7c47ff0819
6 changed files with 36 additions and 20 deletions
@@ -316,15 +316,6 @@ internal class GranularMetadataTransformation(
}
internal val ResolvedComponentResult.currentBuildProjectIdOrNull
get(): ProjectComponentIdentifier? {
val identifier = id
return when {
identifier is ProjectComponentIdentifier && identifier.build.isCurrentBuild -> identifier
else -> null
}
}
private val Project.allProjectsData: Map<String, GranularMetadataTransformation.ProjectData>
get() = rootProject
.extraProperties
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.isProjectComponentIdentifierInCurrentBuild
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
internal class MetadataDependencyTransformationTaskInputs(
@@ -81,8 +82,6 @@ internal class MetadataDependencyTransformationTaskInputs(
private fun Configuration.withoutProjectDependencies(): FileCollection {
return incoming.artifactView { view ->
view.componentFilter { componentIdentifier ->
componentIdentifier !is ProjectComponentIdentifier || !componentIdentifier.build.isCurrentBuild
}
view.componentFilter { componentIdentifier -> !componentIdentifier.isProjectComponentIdentifierInCurrentBuild }
}.files
}
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.PublishedModuleCoordinatesPro
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.ComputedCapability
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.currentBuildId
import org.jetbrains.kotlin.gradle.utils.getValue
import org.jetbrains.kotlin.gradle.utils.isProjectComponentIdentifierInCurrentBuild
import org.jetbrains.kotlin.project.model.KpmLocalModuleIdentifier
import org.jetbrains.kotlin.project.model.KpmMavenModuleIdentifier
import org.jetbrains.kotlin.project.model.KpmModuleIdentifier
@@ -47,7 +48,7 @@ internal object ModuleIds {
fun fromComponent(thisProject: Project, component: ResolvedComponentResult) =
// If the project component comes from another build, we can't extract anything from it, so just use the module coordinates:
if ((component.id as? ProjectComponentIdentifier)?.build?.isCurrentBuild == false)
if (!component.id.isProjectComponentIdentifierInCurrentBuild)
ModuleDependencyIdentifier(component.moduleVersion?.group ?: "unspecified", component.moduleVersion?.name ?: "unspecified")
else
fromComponentId(thisProject, component.id)
@@ -10,6 +10,7 @@ import org.gradle.api.artifacts.Configuration
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.targets.metadata.dependsOnClosureWithInterCompilationDependencies
import org.jetbrains.kotlin.gradle.utils.isProjectComponentIdentifierInCurrentBuild
import org.jetbrains.kotlin.tooling.core.extrasNullableLazyProperty
/**
@@ -83,7 +84,7 @@ private fun Project.applyTransformationToLegacyDependenciesMetadataConfiguration
configuration.exclude(mapOf("group" to group, "module" to name))
}
requested.filter { it.dependency.currentBuildProjectIdOrNull == null }.forEach {
requested.filter { !it.dependency.id.isProjectComponentIdentifierInCurrentBuild }.forEach {
val (group, name) = ModuleIds.fromComponent(project, it.dependency)
val notation = listOfNotNull(group.orEmpty(), name, it.dependency.moduleVersion?.version).joinToString(":")
configuration.resolutionStrategy.force(notation)
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.gradle.tasks.dependsOn
import org.jetbrains.kotlin.gradle.tasks.locateOrRegisterTask
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.isProjectComponentIdentifierInCurrentBuild
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.project.model.KpmModuleIdentifier
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
@@ -198,12 +199,7 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
private fun Iterable<MetadataDependencyResolution>.resolutionsToTransform(): List<ChooseVisibleSourceSets> {
return filterIsInstance<ChooseVisibleSourceSets>()
.applyIf(skipProjectDependencies) {
filterNot {
val dependencyId = it.dependency.id
// filter out ProjectDependencies but keep the ones which are coming from included builds
// i.e. they are ProjectDependencies but have isCurrentBuild = false
dependencyId is ProjectComponentIdentifier && dependencyId.build.isCurrentBuild
}
filterNot { it.dependency.id.isProjectComponentIdentifierInCurrentBuild }
}
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.utils
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
/**
* For composite builds gradle replaces [ModuleComponentIdentifier] with [ProjectComponentIdentifier]
* for which [build.isCurrentBuild] is false
* Often, these dependencies needs to be handled differently not as usual ProjectDependencies
* Since included build *has to* produce their artifacts on which "current build projects" will depend on.
*/
internal val ComponentIdentifier.isProjectComponentIdentifierInCurrentBuild: Boolean
get() = currentBuildProjectIdOrNull != null
internal val ComponentIdentifier.currentBuildProjectIdOrNull
get(): ProjectComponentIdentifier? = when {
this is ProjectComponentIdentifier && build.isCurrentBuild -> this
else -> null
}
internal val ResolvedComponentResult.currentBuildProjectIdOrNull get() = id.currentBuildProjectIdOrNull