From 76cccb93b92f9e9ad311d2d5322091f333ec017c Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Thu, 18 Mar 2021 13:44:27 +0300 Subject: [PATCH] Don't apply Compose compiler plugin to JS and Native compilations Resolves #352 The fix resolves the issue only for Kotlin compiler builds the following commit: https://github.com/JetBrains/kotlin/commit/d023f09bd45e6b21fa341f8481521c9a21c8b924 See also https://youtrack.jetbrains.com/issue/KT-45020 --- .../ComposeCompilerKotlinSupportPlugin.kt | 23 +++++++++++++ .../org/jetbrains/compose/ComposePlugin.kt | 9 ++--- .../org/jetbrains/compose/GradlePluginTest.kt | 23 +++++++++++++ .../compose/test/GradlePluginTestBase.kt | 10 ++++-- .../compose/test/TestKotlinVersion.kt | 8 +++++ .../org/jetbrains/compose/test/TestProject.kt | 20 +++++++---- .../jetbrains/compose/test/TestProjects.kt | 1 + .../jetbrains/compose/test/TestProperties.kt | 3 -- .../test-projects/misc/jsMpp/build.gradle | 33 +++++++++++++++++++ .../test-projects/misc/jsMpp/settings.gradle | 12 +++++++ .../jsMpp/src/commonMain/kotlin/platform.kt | 1 + .../misc/jsMpp/src/jsMain/kotlin/platform.kt | 1 + .../misc/jsMpp/src/jvmMain/kotlin/platform.kt | 1 + 13 files changed, 127 insertions(+), 18 deletions(-) create mode 100644 gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt create mode 100644 gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/GradlePluginTest.kt create mode 100644 gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestKotlinVersion.kt create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/jsMpp/build.gradle create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/jsMpp/settings.gradle create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/commonMain/kotlin/platform.kt create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jsMain/kotlin/platform.kt create mode 100644 gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jvmMain/kotlin/platform.kt diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt new file mode 100644 index 0000000000..c926992320 --- /dev/null +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt @@ -0,0 +1,23 @@ +package org.jetbrains.compose + +import org.gradle.api.provider.Provider +import org.jetbrains.kotlin.gradle.plugin.* + +class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin { + override fun getCompilerPluginId(): String = + "org.jetbrains.compose" + + override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean { + val targetPlatform = kotlinCompilation.target.platformType + return targetPlatform != KotlinPlatformType.js + && targetPlatform != KotlinPlatformType.native + } + + override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider> = + kotlinCompilation.target.project.provider { emptyList() } + + override fun getPluginArtifact(): SubpluginArtifact = + SubpluginArtifact( + groupId = "org.jetbrains.compose.compiler", artifactId = "compiler", version = composeVersion + ) +} \ No newline at end of file diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt index 3c1d07b065..eed0c7a63f 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt @@ -13,7 +13,7 @@ import org.jetbrains.compose.desktop.application.internal.currentTarget import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -private val composeVersion get() = ComposeBuildConfig.composeVersion +internal val composeVersion get() = ComposeBuildConfig.composeVersion class ComposePlugin : Plugin { override fun apply(project: Project) { @@ -30,17 +30,14 @@ class ComposePlugin : Plugin { } } + project.pluginManager.apply(ComposeCompilerKotlinSupportPlugin::class.java) + project.afterEvaluate { if (desktopExtension._isApplicationInitialized) { // If application object was not accessed in a script, // we want to avoid creating tasks like package, run, etc. to avoid conflicts with other plugins configureApplicationImpl(project, desktopExtension.application) } - - project.dependencies.add( - "kotlinCompilerPluginClasspath", - "org.jetbrains.compose.compiler:compiler:$composeVersion" - ) } fun ComponentModuleMetadataHandler.replaceAndroidx(original: String, replacement: String) { diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/GradlePluginTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/GradlePluginTest.kt new file mode 100644 index 0000000000..ff4c46fcfa --- /dev/null +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/GradlePluginTest.kt @@ -0,0 +1,23 @@ +package org.jetbrains.compose + +import org.gradle.testkit.runner.TaskOutcome +import org.jetbrains.compose.test.GradlePluginTestBase +import org.jetbrains.compose.test.TestKotlinVersion +import org.jetbrains.compose.test.TestProjects +import org.jetbrains.compose.test.checks +import org.junit.jupiter.api.Test + +class GradlePluginTest : GradlePluginTestBase() { + @Test + fun jsMppIsNotBroken() = + with( + testProject( + TestProjects.jsMpp, + testEnvironment = defaultTestEnvironment.copy(kotlinVersion = TestKotlinVersion.V1_5_20_dev_3226) + ) + ) { + gradle(":compileKotlinJs").build().checks { check -> + check.taskOutcome(":compileKotlinJs", TaskOutcome.SUCCESS) + } + } +} \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/GradlePluginTestBase.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/GradlePluginTestBase.kt index 3cff59851c..7c465d0de0 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/GradlePluginTestBase.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/GradlePluginTestBase.kt @@ -7,6 +7,12 @@ abstract class GradlePluginTestBase { @TempDir lateinit var testWorkDir: File - fun testProject(name: String): TestProject = - TestProject(name, workingDir = testWorkDir) + val defaultTestEnvironment: TestEnvironment + get() = TestEnvironment(workingDir = testWorkDir) + + fun testProject( + name: String, + testEnvironment: TestEnvironment = defaultTestEnvironment + ): TestProject = + TestProject(name, testEnvironment = testEnvironment) } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestKotlinVersion.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestKotlinVersion.kt new file mode 100644 index 0000000000..aa4b3b3fae --- /dev/null +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestKotlinVersion.kt @@ -0,0 +1,8 @@ +package org.jetbrains.compose.test + +@Suppress("EnumEntryName") +enum class TestKotlinVersion(val versionString: String) { + // __KOTLIN_COMPOSE_VERSION__ + Default("1.4.31"), + V1_5_20_dev_3226("1.5.20-dev-3226") +} \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProject.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProject.kt index 59f420ef39..bb861f2e48 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProject.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProject.kt @@ -4,9 +4,15 @@ import org.gradle.testkit.runner.GradleRunner import org.jetbrains.compose.desktop.application.internal.ComposeProperties import java.io.File -data class TestProject( +data class TestEnvironment( + val workingDir: File, + val kotlinVersion: TestKotlinVersion = TestKotlinVersion.Default, + val composeVersion: String = TestProperties.composeVersion +) + +class TestProject( private val name: String, - private val workingDir: File + private val testEnvironment: TestEnvironment ) { private val additionalArgs = listOf( "--stacktrace", @@ -20,14 +26,14 @@ data class TestProject( for (orig in originalTestRoot.walk()) { if (!orig.isFile) continue - val target = workingDir.resolve(orig.relativeTo(originalTestRoot)) + val target = testEnvironment.workingDir.resolve(orig.relativeTo(originalTestRoot)) target.parentFile.mkdirs() if (orig.name.endsWith(".gradle") || orig.name.endsWith(".gradle.kts")) { val origContent = orig.readText() val newContent = origContent - .replace("COMPOSE_VERSION_PLACEHOLDER", TestProperties.composeVersion) - .replace("KOTLIN_VERSION_PLACEHOLDER", TestProperties.kotlinVersion) + .replace("COMPOSE_VERSION_PLACEHOLDER", testEnvironment.composeVersion) + .replace("KOTLIN_VERSION_PLACEHOLDER", testEnvironment.kotlinVersion.versionString) target.writeText(newContent) } else { orig.copyTo(target) @@ -38,7 +44,7 @@ data class TestProject( fun gradle(vararg args: String): GradleRunner = GradleRunner.create().apply { withGradleVersion(TestProperties.gradleVersionForTests) - withProjectDir(workingDir) + withProjectDir(testEnvironment.workingDir) withArguments(args.toList() + additionalArgs) forwardOutput() } @@ -49,6 +55,6 @@ data class TestProject( gradle(*args).withDebug(true) fun file(path: String): File = - workingDir.resolve(path) + testEnvironment.workingDir.resolve(path) } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProjects.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProjects.kt index 58cc9adf58..4635e94a09 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProjects.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProjects.kt @@ -8,4 +8,5 @@ object TestProjects { const val javaLogger = "application/javaLogger" const val macOptions = "application/macOptions" const val optionsWithSpaces = "application/optionsWithSpaces" + const val jsMpp = "misc/jsMpp" } \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProperties.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProperties.kt index 341b55654c..0a43ecbeb5 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProperties.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/TestProperties.kt @@ -1,9 +1,6 @@ package org.jetbrains.compose.test object TestProperties { - // __KOTLIN_COMPOSE_VERSION__ - val kotlinVersion: String = "1.4.31" - val composeVersion: String get() = System.getProperty("compose.plugin.version")!! diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/build.gradle b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/build.gradle new file mode 100644 index 0000000000..fec5a41ded --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/build.gradle @@ -0,0 +1,33 @@ +import org.jetbrains.compose.desktop.application.dsl.TargetFormat + +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.compose" +} + +repositories { + google() + mavenCentral() + jcenter() + maven { url "https://maven.pkg.jetbrains.space/public/p/compose/dev" } + maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } +} + +kotlin { + js(IR) { + browser() + } + jvm {} + + sourceSets { + named("commonMain") { + } + named("jsMain") { + } + named("jvmMain") { + dependencies { + implementation(compose.desktop.currentOs) + } + } + } +} diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/settings.gradle b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/settings.gradle new file mode 100644 index 0000000000..3250aca93b --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/settings.gradle @@ -0,0 +1,12 @@ +pluginManagement { + plugins { + id 'org.jetbrains.kotlin.multiplatform' version 'KOTLIN_VERSION_PLACEHOLDER' + id 'org.jetbrains.compose' version 'COMPOSE_VERSION_PLACEHOLDER' + } + repositories { + mavenLocal() + gradlePluginPortal() + maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } + } +} +rootProject.name = "jsMpp" \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/commonMain/kotlin/platform.kt b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/commonMain/kotlin/platform.kt new file mode 100644 index 0000000000..f60eab5ff6 --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/commonMain/kotlin/platform.kt @@ -0,0 +1 @@ +expect fun getPlatformName(): String \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jsMain/kotlin/platform.kt b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jsMain/kotlin/platform.kt new file mode 100644 index 0000000000..b6d4a07a09 --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jsMain/kotlin/platform.kt @@ -0,0 +1 @@ +actual fun getPlatformName(): String = "js" \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jvmMain/kotlin/platform.kt b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jvmMain/kotlin/platform.kt new file mode 100644 index 0000000000..144ba29291 --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/misc/jsMpp/src/jvmMain/kotlin/platform.kt @@ -0,0 +1 @@ +actual fun getPlatformName(): String = "jvm" \ No newline at end of file