[Gradle] KpmLocalModuleIdentifier: Migrate to 'BuildIdentifier.getBuildPath'

^KT-58157 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-07-28 13:49:28 +02:00
committed by Space Team
parent d7d6d7ebca
commit 8aee8ad3c4
5 changed files with 29 additions and 13 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.representsProject
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
import org.jetbrains.kotlin.gradle.plugin.sources.getVisibleSourceSetsFromAssociateCompilations
import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.utils.buildPathCompat
import org.jetbrains.kotlin.gradle.utils.getOrPutRootProjectProperty
import org.jetbrains.kotlin.project.model.*
@@ -149,7 +150,7 @@ class GradleProjectModuleBuilder(private val addInferredSourceSetVisibilityAsExp
val sourceSetsToInclude = compilationsToInclude.flatMapTo(mutableSetOf()) { it.allKotlinSourceSets }
val moduleIdentifier = KpmLocalModuleIdentifier(
project.currentBuildId().name,
project.currentBuildId().buildPathCompat,
project.path,
classifier.takeIf { it != KotlinCompilation.MAIN_COMPILATION_NAME }
)
@@ -242,7 +243,7 @@ internal fun Dependency.toKpmModuleDependency(
when (this) {
is ProjectDependency ->
KpmLocalModuleIdentifier(
project.currentBuildId().name,
project.currentBuildId().buildPathCompat,
dependencyProject.path,
moduleClassifiersFromCapabilities(requestedCapabilities).single() // FIXME multiple capabilities
)
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.PublishedModuleCoordinatesProvider
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.buildPathCompat
import org.jetbrains.kotlin.gradle.utils.currentBuild
import org.jetbrains.kotlin.gradle.utils.getValue
import org.jetbrains.kotlin.project.model.KpmLocalModuleIdentifier
@@ -73,7 +74,7 @@ internal object ModuleIds {
fun lossyFromModuleIdentifier(thisProject: Project, moduleIdentifier: KpmModuleIdentifier): ModuleDependencyIdentifier {
when (moduleIdentifier) {
is KpmLocalModuleIdentifier -> {
check(moduleIdentifier.buildId == thisProject.currentBuildId().name)
check(moduleIdentifier.buildId == thisProject.currentBuildId().buildPathCompat)
val dependencyProject = thisProject.project(moduleIdentifier.projectId)
val topLevelExtension = dependencyProject.topLevelExtension
val getRootPublication: () -> MavenPublication? = when {
@@ -12,16 +12,20 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.artifacts.result.ResolvedVariantResult
import org.gradle.api.capabilities.Capability
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.GradleProjectModuleBuilder
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinProjectStructureMetadata
import org.jetbrains.kotlin.gradle.plugin.mpp.ProjectStructureMetadataModuleBuilder
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
import org.jetbrains.kotlin.gradle.utils.buildPathCompat
import org.jetbrains.kotlin.gradle.utils.getOrPutRootProjectProperty
import org.jetbrains.kotlin.project.model.*
class GradleKpmModuleDependencyResolver(
private val gradleComponentResultResolver: GradleKpmComponentResultCachingResolver,
private val projectStructureMetadataModuleBuilder: ProjectStructureMetadataModuleBuilder,
private val projectModuleBuilder: GradleProjectModuleBuilder
private val projectModuleBuilder: GradleProjectModuleBuilder,
) : KpmModuleDependencyResolver {
override fun resolveDependency(requestingModule: KpmModule, moduleDependency: KpmModuleDependency): KpmModule? {
@@ -99,7 +103,7 @@ internal class GradleKpmExternalPlainModule(private val moduleData: KpmBasicModu
internal class GradleKpmExternalImportedModule(
private val moduleData: KpmBasicModule,
val projectStructureMetadata: KotlinProjectStructureMetadata,
val hostSpecificFragments: Set<KpmFragment>
val hostSpecificFragments: Set<KpmFragment>,
) : KpmModule by moduleData {
val hasLegacyMetadataModule = !projectStructureMetadata.isPublishedAsRoot
@@ -134,7 +138,7 @@ internal fun ResolvedVariantResult.toSingleKpmModuleIdentifier(): KpmModuleIdent
private fun ResolvedComponentResult.toKpmModuleIdentifier(moduleClassifier: String?): KpmModuleIdentifier {
return when (val id = id) {
is ProjectComponentIdentifier -> KpmLocalModuleIdentifier(id.build.name, id.projectPath, moduleClassifier)
is ProjectComponentIdentifier -> KpmLocalModuleIdentifier(id.build.buildPathCompat, id.projectPath, moduleClassifier)
is ModuleComponentIdentifier -> id.toSingleKpmModuleIdentifier()
else -> KpmMavenModuleIdentifier(moduleVersion?.group.orEmpty(), moduleVersion?.name.orEmpty(), moduleClassifier)
}
@@ -142,7 +146,7 @@ private fun ResolvedComponentResult.toKpmModuleIdentifier(moduleClassifier: Stri
private fun ResolvedVariantResult.toKpmModuleIdentifier(moduleClassifier: String?): KpmModuleIdentifier {
return when (val id = owner) {
is ProjectComponentIdentifier -> KpmLocalModuleIdentifier(id.build.name, id.projectPath, moduleClassifier)
is ProjectComponentIdentifier -> KpmLocalModuleIdentifier(id.build.buildPathCompat, id.projectPath, moduleClassifier)
is ModuleComponentIdentifier -> id.toSingleKpmModuleIdentifier()
else -> error("Unexpected component identifier '$id' of type ${id.javaClass}")
}
@@ -156,7 +160,11 @@ internal fun moduleClassifiersFromCapabilities(capabilities: Iterable<Capability
internal fun ComponentSelector.toKpmModuleIdentifiers(): Iterable<KpmModuleIdentifier> {
val moduleClassifiers = moduleClassifiersFromCapabilities(requestedCapabilities)
return when (this) {
is ProjectComponentSelector -> moduleClassifiers.map { KpmLocalModuleIdentifier(buildName, projectPath, it) }
is ProjectComponentSelector -> {
val buildPath = if (GradleVersion.current() >= GradleVersion.version("8.2")) buildPath
else @Suppress("DEPRECATION") ":$buildName"
moduleClassifiers.map { KpmLocalModuleIdentifier(buildPath, projectPath, it) }
}
is ModuleComponentSelector -> moduleClassifiers.map { KpmMavenModuleIdentifier(moduleIdentifier.group, moduleIdentifier.name, it) }
else -> error("unexpected component selector")
}
@@ -175,7 +183,7 @@ internal fun ComponentIdentifier.matchesModuleIdentifier(id: KpmModuleIdentifier
when (id) {
is KpmLocalModuleIdentifier -> {
val projectId = this as? ProjectComponentIdentifier
projectId?.build?.name == id.buildId && projectId.projectPath == id.projectId
projectId?.build?.buildPathCompat == id.buildId && projectId.projectPath == id.projectId
}
is KpmMavenModuleIdentifier -> {
val componentId = this as? ModuleComponentIdentifier
@@ -189,7 +197,7 @@ private fun getProjectStructureMetadata(
project: Project,
module: ResolvedComponentResult,
configuration: Configuration,
moduleIdentifier: KpmModuleIdentifier? = null
moduleIdentifier: KpmModuleIdentifier? = null,
): KotlinProjectStructureMetadata? {
TODO("Implement project structure metadata extractor for KPM")
}
@@ -11,6 +11,7 @@ import org.gradle.api.ExtensiblePolymorphicDomainObjectContainer
import org.gradle.api.NamedDomainObjectSet
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.currentBuildId
import org.jetbrains.kotlin.gradle.utils.buildPathCompat
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.project.model.*
import org.jetbrains.kotlin.project.model.utils.variantsContainingFragment
@@ -28,7 +29,7 @@ abstract class GradleKpmModuleInternal(
)
override val moduleIdentifier: KpmModuleIdentifier =
KpmLocalModuleIdentifier(project.currentBuildId().name, project.path, moduleClassifier)
KpmLocalModuleIdentifier(project.currentBuildId().buildPathCompat, project.path, moduleClassifier)
override val fragments: ExtensiblePolymorphicDomainObjectContainer<GradleKpmFragment> =
project.objects.polymorphicDomainObjectContainer(GradleKpmFragment::class.java)
@@ -12,11 +12,16 @@ import org.gradle.internal.build.BuildState
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.plugin.internal.BasePluginConfiguration
import org.jetbrains.kotlin.gradle.plugin.variantImplementationFactory
import org.jetbrains.kotlin.gradle.utils.buildPathCompat
import org.jetbrains.kotlin.project.model.KpmModule
import org.jetbrains.kotlin.project.model.KpmLocalModuleIdentifier
fun KpmModule.representsProject(project: Project): Boolean =
moduleIdentifier.let { it is KpmLocalModuleIdentifier && it.buildId == project.currentBuildId().name && it.projectId == project.path }
moduleIdentifier.let {
it is KpmLocalModuleIdentifier &&
it.buildId == project.currentBuildId().buildPathCompat &&
it.projectId == project.path
}
@InternalKotlinGradlePluginApi
fun Project.currentBuildId(): BuildIdentifier =