(minor) Transform KotlinPlatformType attribute to String, rename entries

* To avoid unexpected effects from the Gradle instantiation mechanisms
  for attribute values, use a safer String attribute type for the
  KotlinPlatformType attribute

* Rename its entries to keep enum entries naming consistent
This commit is contained in:
Sergey Igushkin
2018-09-24 22:21:13 +03:00
parent 65e3559c09
commit 3f24f8bd8d
7 changed files with 32 additions and 32 deletions
@@ -10,43 +10,43 @@ import org.gradle.api.attributes.*
import org.gradle.util.GradleVersion
import java.io.Serializable
enum class KotlinPlatformType: Named, Serializable {
common, jvm, js, androidJvm, native;
enum class KotlinPlatformType(val attributeValue: String): Named, Serializable {
COMMON("common"), JVM("jvm"), JS("js"), ANDROID_JVM("androidJvm"), NATIVE("native");
override fun toString(): String = name
override fun getName(): String = name
class CompatibilityRule : AttributeCompatibilityRule<KotlinPlatformType> {
override fun execute(details: CompatibilityCheckDetails<KotlinPlatformType>) = with(details) {
if (producerValue == jvm && consumerValue == androidJvm)
class CompatibilityRule : AttributeCompatibilityRule<String> {
override fun execute(details: CompatibilityCheckDetails<String?>) = with(details) {
if (producerValue == JVM.attributeValue && consumerValue == ANDROID_JVM.attributeValue)
compatible()
// Allow the input metadata configuration consume platform-specific artifacts if no metadata is available, KT-26834
if (consumerValue == common)
if (consumerValue == COMMON.attributeValue)
compatible()
}
}
class DisambiguationRule : AttributeDisambiguationRule<KotlinPlatformType> {
override fun execute(details: MultipleCandidatesDetails<KotlinPlatformType?>) = with(details) {
if (candidateValues == setOf(androidJvm, jvm))
closestMatch(androidJvm)
class DisambiguationRule : AttributeDisambiguationRule<String> {
override fun execute(details: MultipleCandidatesDetails<String?>) = with(details) {
if (candidateValues == setOf(ANDROID_JVM.attributeValue, JVM.attributeValue))
closestMatch(ANDROID_JVM.attributeValue)
if (common in candidateValues)
if (COMMON.attributeValue in candidateValues)
// then the consumer requests common or requests no platform-specific artifacts,
// so common is the best match, KT-26834
closestMatch(common)
closestMatch(COMMON.attributeValue)
}
}
companion object {
val attribute = Attribute.of(
val ATTRIBUTE = Attribute.of(
"org.jetbrains.kotlin.platform.type",
KotlinPlatformType::class.java
String::class.java
)
fun setupAttributesMatchingStrategy(attributesSchema: AttributesSchema) {
attributesSchema.attribute(KotlinPlatformType.attribute).run {
attributesSchema.attribute(KotlinPlatformType.ATTRIBUTE).run {
if (isGradleVersionAtLeast(4, 0)) {
compatibilityRules.add(CompatibilityRule::class.java)
disambiguationRules.add(DisambiguationRule::class.java)
@@ -395,7 +395,7 @@ internal abstract class AbstractKotlinPlugin(
val javaSourceSets = project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets
val kotlinSourceSetDslName = when (kotlinTarget.platformType) {
KotlinPlatformType.js -> KOTLIN_JS_DSL_NAME
KotlinPlatformType.JS -> KOTLIN_JS_DSL_NAME
else -> KOTLIN_DSL_NAME
}
@@ -428,7 +428,7 @@ internal abstract class AbstractKotlinPlugin(
val project = kotlinTarget.project
// Setup the consuming configurations:
project.dependencies.attributesSchema.attribute(KotlinPlatformType.attribute)
project.dependencies.attributesSchema.attribute(KotlinPlatformType.ATTRIBUTE)
kotlinTarget.compilations.all { compilation ->
AbstractKotlinTargetConfigurator.defineConfigurationsForCompilation(compilation, kotlinTarget, project.configurations)
}
@@ -436,7 +436,7 @@ internal abstract class AbstractKotlinPlugin(
// Setup the published configurations:
// Don't set the attributes for common module; otherwise their 'common' platform won't be compatible with the one in
// platform-specific modules
if (kotlinTarget.platformType != KotlinPlatformType.common) {
if (kotlinTarget.platformType != KotlinPlatformType.COMMON) {
project.configurations.getByName(kotlinTarget.apiElementsConfigurationName).run {
attributes.attribute(Usage.USAGE_ATTRIBUTE, KotlinUsages.producerApiUsage(kotlinTarget))
usesPlatformOf(kotlinTarget)
@@ -493,7 +493,7 @@ internal open class KotlinPlugin(
Kotlin2JvmSourceSetProcessor(project, tasksProvider, compilation, kotlinPluginVersion)
override fun apply(project: Project) {
val target = KotlinWithJavaTarget(project, KotlinPlatformType.jvm, targetName).apply {
val target = KotlinWithJavaTarget(project, KotlinPlatformType.JVM, targetName).apply {
disambiguationClassifier = null // don't add anything to the task names
}
(project.kotlinExtension as KotlinSingleJavaTargetExtension).target = target
@@ -520,7 +520,7 @@ internal open class KotlinCommonPlugin(
KotlinCommonSourceSetProcessor(project, compilation, tasksProvider, kotlinPluginVersion)
override fun apply(project: Project) {
val target = KotlinWithJavaTarget(project, KotlinPlatformType.common, targetName)
val target = KotlinWithJavaTarget(project, KotlinPlatformType.COMMON, targetName)
(project.kotlinExtension as KotlinSingleJavaTargetExtension).target = target
super.apply(project)
@@ -546,7 +546,7 @@ internal open class Kotlin2JsPlugin(
)
override fun apply(project: Project) {
val target = KotlinWithJavaTarget(project, KotlinPlatformType.js, targetName)
val target = KotlinWithJavaTarget(project, KotlinPlatformType.JS, targetName)
(project.kotlinExtension as KotlinSingleJavaTargetExtension).target = target
super.apply(project)
@@ -701,7 +701,7 @@ internal fun Project.usageByName(usageName: String): Usage =
}
fun Configuration.usesPlatformOf(target: KotlinTarget): Configuration {
attributes.attribute(KotlinPlatformType.attribute, target.platformType)
attributes.attribute(KotlinPlatformType.ATTRIBUTE, target.platformType.attributeValue)
// TODO: Provide an universal way to copy attributes from the target.
if (target is KotlinNativeTarget) {
attributes.attribute(KotlinNativeTarget.konanTargetAttribute, target.konanTarget.name)
@@ -8,8 +8,8 @@ package org.jetbrains.kotlin.gradle.plugin.mpp
import org.gradle.api.attributes.*
import org.gradle.api.attributes.Usage.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.androidJvm
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.ANDROID_JVM
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.JVM
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.usageByName
import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast
@@ -19,7 +19,7 @@ object KotlinUsages {
const val KOTLIN_RUNTIME = "kotlin-runtime"
val values = setOf(KOTLIN_API, KOTLIN_RUNTIME)
private val jvmPlatformTypes: Set<KotlinPlatformType> = setOf(jvm, androidJvm)
private val jvmPlatformTypes: Set<KotlinPlatformType> = setOf(JVM, ANDROID_JVM)
internal fun consumerApiUsage(target: KotlinTarget) = target.project.usageByName(
when (target.platformType) {
@@ -85,7 +85,7 @@ class KotlinMetadataTargetPreset(
KotlinCommonCompilationFactory(forTarget)
override val platformType: KotlinPlatformType
get() = KotlinPlatformType.common
get() = KotlinPlatformType.COMMON
override fun buildCompilationProcessor(compilation: KotlinCommonCompilation): KotlinSourceSetProcessor<*> =
KotlinCommonSourceSetProcessor(
@@ -136,7 +136,7 @@ class KotlinJvmTargetPreset(
KotlinJvmCompilationFactory(forTarget)
override val platformType: KotlinPlatformType
get() = KotlinPlatformType.jvm
get() = KotlinPlatformType.JVM
override fun buildCompilationProcessor(compilation: KotlinJvmCompilation): KotlinSourceSetProcessor<*> =
Kotlin2JvmSourceSetProcessor(project, KotlinTasksProvider(compilation.target.targetName), compilation, kotlinPluginVersion)
@@ -165,7 +165,7 @@ class KotlinJsTargetPreset(
KotlinJsCompilationFactory(project, forTarget)
override val platformType: KotlinPlatformType
get() = KotlinPlatformType.js
get() = KotlinPlatformType.JS
override fun buildCompilationProcessor(compilation: KotlinJsCompilation): KotlinSourceSetProcessor<*> =
Kotlin2JsSourceSetProcessor(project, KotlinTasksProvider(compilation.target.targetName), compilation, kotlinPluginVersion)
@@ -210,7 +210,7 @@ class KotlinJvmWithJavaTargetPreset(
override fun createTarget(name: String): KotlinWithJavaTarget {
project.plugins.apply(JavaPlugin::class.java)
val target = KotlinWithJavaTarget(project, KotlinPlatformType.jvm, name).apply {
val target = KotlinWithJavaTarget(project, KotlinPlatformType.JVM, name).apply {
disambiguationClassifier = name
}
@@ -122,7 +122,7 @@ open class KotlinAndroidTarget(
internal set
override val platformType: KotlinPlatformType
get() = KotlinPlatformType.androidJvm
get() = KotlinPlatformType.ANDROID_JVM
private val compilationFactory = KotlinJvmAndroidCompilationFactory(project, this)
@@ -179,7 +179,7 @@ open class KotlinOnlyTarget<T : KotlinCompilation>(
class KotlinNativeTarget(
project: Project,
val konanTarget: KonanTarget
) : KotlinOnlyTarget<KotlinNativeCompilation>(project, KotlinPlatformType.native) {
) : KotlinOnlyTarget<KotlinNativeCompilation>(project, KotlinPlatformType.NATIVE) {
init {
attributes.attribute(konanTargetAttribute, konanTarget.name)
@@ -68,7 +68,7 @@ internal class DefaultKotlinSourceSetFactory(
dependencyConfigurationWithMetadata.forEach { (configurationName, metadataName) ->
project.configurations.maybeCreate(metadataName).apply {
attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.common)
attributes.attribute(KotlinPlatformType.ATTRIBUTE, KotlinPlatformType.COMMON.attributeValue)
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.usageByName(KotlinUsages.KOTLIN_API))
isVisible = false
isCanBeConsumed = false