From 90ec1512581357a3f7f9f46ca030df7c219b63c6 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Mon, 27 Aug 2018 21:39:03 +0300 Subject: [PATCH] Bring back two methods used by old MPP K/N plugin * First, it overrides `addCommonSourceSetToPlatformSourceSet` with an old signature that was drop during migration to 'KotlinSourceSet's * Second, it uses an extension SourceSet.kotlin that got dropped during the aforementioned migration Now, for the old MPP K/N plugin, `addCommonSourceSetToPlatformSourceSet` is called twice, one time with the new signature and another with the old one, which it overrides. --- .../plugin/KotlinMultiplatformPlugin.kt | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt index 91a8f603a80..ab4d92edeee 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt @@ -21,6 +21,8 @@ import org.gradle.api.* import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.file.SourceDirectorySet +import org.gradle.api.plugins.JavaPluginConvention +import org.gradle.api.tasks.SourceSet import org.jetbrains.kotlin.gradle.dsl.kotlinExtension import org.jetbrains.kotlin.gradle.plugin.source.KotlinSourceSet import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile @@ -114,6 +116,15 @@ open class KotlinPlatformImplementationPluginBase(platformName: String) : Kotlin namedSourceSetsContainer(platformProject) ) { commonSourceSet: Named, _ -> addCommonSourceSetToPlatformSourceSet(commonSourceSet, platformProject) + + // Workaround for older versions of Kotlin/Native overriding the old signature + commonProject.convention.findPlugin(JavaPluginConvention::class.java) + ?.sourceSets + ?.findByName(commonSourceSet.name) + ?.let { javaSourceSet -> + @Suppress("DEPRECATION") + addCommonSourceSetToPlatformSourceSet(javaSourceSet, platformProject) + } } } } @@ -152,11 +163,13 @@ open class KotlinPlatformImplementationPluginBase(platformName: String) : Kotlin platformProject.whenEvaluated { // At the point when the source set in the platform module is created, the task does not exist val platformTask = platformProject.tasks .filterIsInstance>() - .single { it.sourceSetName == commonSourceSet.name } + .singleOrNull { it.sourceSetName == commonSourceSet.name } // TODO use strict check once this code is not run in K/N val commonSources = getKotlinSourceDirectorySetSafe(commonSourceSet)!! - platformTask.source(commonSources) - platformTask.commonSourceSet += commonSources + if (platformTask != null) { + platformTask.source(commonSources) + platformTask.commonSourceSet += commonSources + } } } @@ -172,6 +185,20 @@ open class KotlinPlatformImplementationPluginBase(platformName: String) : Kotlin val getKotlin = from.javaClass.getMethod("getKotlin") return getKotlin(from) as? SourceDirectorySet } + + @Deprecated("Migrate to the new Kotlin source sets and use the addCommonSourceSetToPlatformSourceSet(Named, Project) overload") + protected open fun addCommonSourceSetToPlatformSourceSet(sourceSet: SourceSet, platformProject: Project) = Unit + + @Deprecated("Retained for older Kotlin/Native MPP plugin binary compatibility") + protected val SourceSet.kotlin: SourceDirectorySet? + get() { + // Access through reflection, because another project's KotlinSourceSet might be loaded + // by a different class loader: + val convention = (getConvention("kotlin") ?: getConvention("kotlin2js")) ?: return null + val kotlinSourceSetIface = convention.javaClass.interfaces.find { it.name == KotlinSourceSet::class.qualifiedName } + val getKotlin = kotlinSourceSetIface?.methods?.find { it.name == "getKotlin" } ?: return null + return getKotlin(convention) as? SourceDirectorySet + } } internal fun Project.whenEvaluated(fn: Project.() -> T) {