From 032c3330ab71112f393c4032dbff98076d9e58a1 Mon Sep 17 00:00:00 2001 From: Anton Lakotka Date: Tue, 29 Nov 2022 14:57:51 +0100 Subject: [PATCH] [Gradle] Support sources publication as variants in ExternalKotlinTarget ^KT-36943 --- .../gradle/android/AndroidTargetPrototype.kt | 15 +++++++++++++++ .../external/ExternalKotlinTargetComponent.kt | 5 +++++ .../external/ExternalKotlinTargetDescriptor.kt | 12 ++++++++++++ .../mpp/external/ExternalKotlinTargetImpl.kt | 5 +++++ .../mpp/external/createExternalKotlinTarget.kt | 18 ++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/libraries/tools/kotlin-gradle-plugin-tcs-android/src/main/kotlin/org/jetbrains/kotlin/gradle/android/AndroidTargetPrototype.kt b/libraries/tools/kotlin-gradle-plugin-tcs-android/src/main/kotlin/org/jetbrains/kotlin/gradle/android/AndroidTargetPrototype.kt index 89eda36852f..c2a0c29584d 100644 --- a/libraries/tools/kotlin-gradle-plugin-tcs-android/src/main/kotlin/org/jetbrains/kotlin/gradle/android/AndroidTargetPrototype.kt +++ b/libraries/tools/kotlin-gradle-plugin-tcs-android/src/main/kotlin/org/jetbrains/kotlin/gradle/android/AndroidTargetPrototype.kt @@ -68,6 +68,15 @@ fun KotlinMultiplatformExtension.androidTargetPrototype(): PrototypeAndroidTarge configuration.attributes.attribute(AgpVersionAttr.ATTRIBUTE, project.objects.named("7.4.0-beta02")) /* For demo */ } + /* + Configure runtimeElements configuration attributes (project to project dependency) + In this example we hardcoded AGP version 7.4.0-beta02 as demo + */ + sourcesElements.configure { _, configuration -> + configuration.attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, project.objects.named(TargetJvmEnvironment.ANDROID)) + configuration.attributes.attribute(AgpVersionAttr.ATTRIBUTE, project.objects.named("7.4.0-beta02")) /* For demo */ + } + /* Configure published configurations (maven publication): We override KotlinPlatformType to be androidJvm for now (to be discussed w/ Google later) @@ -84,6 +93,12 @@ fun KotlinMultiplatformExtension.androidTargetPrototype(): PrototypeAndroidTarge configuration.attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, project.objects.named(TargetJvmEnvironment.ANDROID)) } + sourcesElementsPublished.configure { _, configuration -> + /* TODO w/ Google: Find a way to deprecate this attribute */ + configuration.attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm) + configuration.attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, project.objects.named(TargetJvmEnvironment.ANDROID)) + } + configureIdeImport { registerDependencyResolver( AndroidBootClasspathIdeDependencyResolver(project), diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetComponent.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetComponent.kt index c5e0a111d64..7f4784a64fb 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetComponent.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetComponent.kt @@ -47,6 +47,11 @@ internal class ExternalKotlinTargetComponent( override val defaultArtifactId: String get() = dashSeparatedName(target.project.name, target.name.toLowerCase()) + @Deprecated( + message = "Sources artifacts are now published as separate variant " + + "use target.sourcesElementsConfigurationName to obtain necessary information", + replaceWith = ReplaceWith("target.sourcesElementsConfigurationName") + ) override val sourcesArtifacts: Set get() = emptySet() diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetDescriptor.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetDescriptor.kt index 4b8f36d3694..775aa8b01c2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetDescriptor.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetDescriptor.kt @@ -24,8 +24,10 @@ interface ExternalKotlinTargetDescriptor { val apiElements: ExternalKotlinTargetConfigurationDescriptor val runtimeElements: ExternalKotlinTargetConfigurationDescriptor + val sourcesElements: ExternalKotlinTargetConfigurationDescriptor val apiElementsPublished: ExternalKotlinTargetConfigurationDescriptor val runtimeElementsPublished: ExternalKotlinTargetConfigurationDescriptor + val sourcesElementsPublished: ExternalKotlinTargetConfigurationDescriptor val configure: ((T) -> Unit)? val configureIdeImport: (IdeMultiplatformImport.() -> Unit)? @@ -50,12 +52,18 @@ class ExternalKotlinTargetDescriptorBuilder i val runtimeElements: ExternalKotlinTargetConfigurationDescriptorBuilder = ExternalKotlinTargetConfigurationDescriptorBuilder() + val sourcesElements: ExternalKotlinTargetConfigurationDescriptorBuilder = + ExternalKotlinTargetConfigurationDescriptorBuilder() + val apiElementsPublished: ExternalKotlinTargetConfigurationDescriptorBuilder = ExternalKotlinTargetConfigurationDescriptorBuilder() val runtimeElementsPublished: ExternalKotlinTargetConfigurationDescriptorBuilder = ExternalKotlinTargetConfigurationDescriptorBuilder() + val sourcesElementsPublished: ExternalKotlinTargetConfigurationDescriptorBuilder = + ExternalKotlinTargetConfigurationDescriptorBuilder() + var configure: ((T) -> Unit)? = null fun configure(action: (T) -> Unit) { @@ -78,8 +86,10 @@ class ExternalKotlinTargetDescriptorBuilder i targetFactory = targetFactory, apiElements = apiElements.build(), runtimeElements = runtimeElements.build(), + sourcesElements = sourcesElements.build(), apiElementsPublished = apiElementsPublished.build(), runtimeElementsPublished = runtimeElementsPublished.build(), + sourcesElementsPublished = sourcesElementsPublished.build(), configure = configure, configureIdeImport = configureIdeImport ) @@ -91,8 +101,10 @@ private data class ExternalKotlinTargetDescriptorImpl, override val apiElements: ExternalKotlinTargetConfigurationDescriptor, override val runtimeElements: ExternalKotlinTargetConfigurationDescriptor, + override val sourcesElements: ExternalKotlinTargetConfigurationDescriptor, override val apiElementsPublished: ExternalKotlinTargetConfigurationDescriptor, override val runtimeElementsPublished: ExternalKotlinTargetConfigurationDescriptor, + override val sourcesElementsPublished: ExternalKotlinTargetConfigurationDescriptor, override val configure: ((T) -> Unit)?, override val configureIdeImport: (IdeMultiplatformImport.() -> Unit)?, ) : ExternalKotlinTargetDescriptor diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetImpl.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetImpl.kt index c574cbd777d..b6117d17826 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetImpl.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/ExternalKotlinTargetImpl.kt @@ -28,8 +28,10 @@ internal class ExternalKotlinTargetImpl internal constructor( val defaultConfiguration: Configuration, val apiElementsConfiguration: Configuration, val runtimeElementsConfiguration: Configuration, + val sourcesElementsConfiguration: Configuration, val apiElementsPublishedConfiguration: Configuration, val runtimeElementsPublishedConfiguration: Configuration, + val sourcesElementsPublishedConfiguration: Configuration, val kotlinTargetComponent: ExternalKotlinTargetComponent, private val artifactsTaskLocator: ArtifactsTaskLocator, ) : InternalKotlinTarget { @@ -65,6 +67,9 @@ internal class ExternalKotlinTargetImpl internal constructor( override val runtimeElementsConfigurationName: String get() = runtimeElementsConfiguration.name + override val sourcesElementsConfigurationName: String + get() = sourcesElementsConfiguration.name + @InternalKotlinGradlePluginApi override val kotlinComponents: Set = setOf(kotlinTargetComponent) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/createExternalKotlinTarget.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/createExternalKotlinTarget.kt index c7347b812d5..059126b8b37 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/createExternalKotlinTarget.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/external/createExternalKotlinTarget.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.gradle.ExternalKotlinTargetApi import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.ide.kotlinIdeMultiplatformImport import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsages +import org.jetbrains.kotlin.gradle.plugin.mpp.configureSourcesPublicationAttributes import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.markConsumable import org.jetbrains.kotlin.gradle.plugin.usesPlatformOf import org.jetbrains.kotlin.gradle.tasks.locateOrRegisterTask @@ -28,6 +29,7 @@ fun KotlinMultiplatformExtension.createExter val defaultConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "default")) val apiElementsConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "apiElements")) val runtimeElementsConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "runtimeElements")) + val sourcesElementsConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "sourcesElements")) val apiElementsPublishedConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "apiElements-published")) @@ -35,6 +37,9 @@ fun KotlinMultiplatformExtension.createExter val runtimeElementsPublishedConfiguration = project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "runtimeElements-published")) + val sourcesElementsPublishedConfiguration = + project.configurations.maybeCreate(lowerCamelCaseName(descriptor.targetName, "sourcesElements-published")) + val kotlinTargetComponent = ExternalKotlinTargetComponent( ExternalKotlinTargetComponent.TargetProvider.byTargetName(this, descriptor.targetName) ) @@ -51,8 +56,10 @@ fun KotlinMultiplatformExtension.createExter defaultConfiguration = defaultConfiguration, apiElementsConfiguration = apiElementsConfiguration, runtimeElementsConfiguration = runtimeElementsConfiguration, + sourcesElementsConfiguration = sourcesElementsConfiguration, apiElementsPublishedConfiguration = apiElementsPublishedConfiguration, runtimeElementsPublishedConfiguration = runtimeElementsPublishedConfiguration, + sourcesElementsPublishedConfiguration = sourcesElementsPublishedConfiguration, kotlinTargetComponent = kotlinTargetComponent, artifactsTaskLocator = artifactsTaskLocator ) @@ -61,14 +68,19 @@ fun KotlinMultiplatformExtension.createExter target.setupApiElements(apiElementsPublishedConfiguration) target.setupRuntimeElements(runtimeElementsConfiguration) target.setupRuntimeElements(runtimeElementsPublishedConfiguration) + target.setupSourcesElements(sourcesElementsConfiguration) + target.setupSourcesElements(sourcesElementsPublishedConfiguration) apiElementsConfiguration.markConsumable() runtimeElementsConfiguration.markConsumable() + sourcesElementsConfiguration.markConsumable() /* Those configurations can not be resolved but also not consumed (not suitable for project to proejct dependencies */ apiElementsPublishedConfiguration.isCanBeConsumed = false apiElementsPublishedConfiguration.isCanBeResolved = false runtimeElementsPublishedConfiguration.isCanBeResolved = false runtimeElementsPublishedConfiguration.isCanBeConsumed = false + sourcesElementsPublishedConfiguration.isCanBeResolved = false + sourcesElementsPublishedConfiguration.isCanBeConsumed = false val decorated = descriptor.targetFactory.create(DecoratedExternalKotlinTarget.Delegate(target)) target.onCreated() @@ -76,8 +88,10 @@ fun KotlinMultiplatformExtension.createExter descriptor.configure?.invoke(decorated) descriptor.apiElements.configure?.invoke(decorated, apiElementsConfiguration) descriptor.runtimeElements.configure?.invoke(decorated, runtimeElementsConfiguration) + descriptor.sourcesElements.configure?.invoke(decorated, sourcesElementsConfiguration) descriptor.apiElementsPublished.configure?.invoke(decorated, apiElementsPublishedConfiguration) descriptor.runtimeElementsPublished.configure?.invoke(decorated, runtimeElementsPublishedConfiguration) + descriptor.sourcesElementsPublished.configure?.invoke(decorated, sourcesElementsPublishedConfiguration) descriptor.configureIdeImport?.invoke(project.kotlinIdeMultiplatformImport) targets.add(decorated) @@ -103,3 +117,7 @@ private fun ExternalKotlinTargetImpl.setupRuntimeElements(configuration: Configu configuration.attributes.attribute(Usage.USAGE_ATTRIBUTE, KotlinUsages.producerRuntimeUsage(this)) configuration.attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.LIBRARY)) } + +private fun ExternalKotlinTargetImpl.setupSourcesElements(configuration: Configuration) { + configuration.configureSourcesPublicationAttributes(this) +} \ No newline at end of file