From 7c4efb27724f677c6655b9e4d21ad3cfee4d46f2 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Fri, 12 Apr 2019 18:02:29 +0200 Subject: [PATCH] Disable ScriptingGradleSubplugin if applied in the gradle prior to 5.0 Allows to avoid limiting scripting infrastructure to languageVersion 1.2, since kotlin compiler 1.3+ is bundled with gradle only starting from 5.0 --- .../jetbrains/kotlin/gradle/SubpluginsIT.kt | 40 ++++++++++++----- .../internal/ScriptingGradleSubplugin.kt | 45 ++++++++++++------- .../scripting-compiler/build.gradle.kts | 1 - 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SubpluginsIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SubpluginsIT.kt index 4473b49fccf..168c4e80656 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SubpluginsIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SubpluginsIT.kt @@ -5,10 +5,12 @@ package org.jetbrains.kotlin.gradle +import org.gradle.util.GradleVersion import org.jetbrains.kotlin.gradle.util.checkBytecodeContains import org.jetbrains.kotlin.gradle.util.modify import org.junit.Test import java.io.File +import kotlin.test.assertEquals import kotlin.test.assertTrue class SubpluginsIT : BaseGradleIT() { @@ -144,25 +146,39 @@ class SubpluginsIT : BaseGradleIT() { val worldGreet = project.projectFile("world.greet") val greetScriptTemplateKt = project.projectFile("GreetScriptTemplate.kt") + var isFailed = false project.build("build", options = options) { - assertSuccessful() val classesDir = kotlinClassesDir("app", "main") - assertFileExists("${classesDir}World.class") - assertFileExists("${classesDir}Alice.class") - assertFileExists("${classesDir}Bob.class") + if (project.testGradleVersionAtLeast("5.0")) { + assertSuccessful() + assertFileExists("${classesDir}World.class") + assertFileExists("${classesDir}Alice.class") + assertFileExists("${classesDir}Bob.class") - if (withIC) { - // compile iterations are not logged when IC is disabled - assertCompiledKotlinSources(project.relativize(bobGreet, aliceGreet, worldGreet, greetScriptTemplateKt)) + if (withIC) { + // compile iterations are not logged when IC is disabled + assertCompiledKotlinSources(project.relativize(bobGreet, aliceGreet, worldGreet, greetScriptTemplateKt)) + } + } else { + val usedGradleVersion = + GradleVersion.version( + System.getProperty("kotlin.gradle.version.for.tests") + ?: project.gradleVersionRequirement.minVersion + ) + assertEquals(true, usedGradleVersion.version.substringBefore('.').toIntOrNull()?.let { it < 5 }, "Expected gradle version < 5, got ${usedGradleVersion.version}") + assertContains("kotlin scripting plugin: incompatible Gradle version") + isFailed = true } } - bobGreet.modify { it.replace("Bob", "Uncle Bob") } - project.build("build", options = options) { - assertSuccessful() + if (!isFailed) { + bobGreet.modify { it.replace("Bob", "Uncle Bob") } + project.build("build", options = options) { + assertSuccessful() - if (withIC) { - assertCompiledKotlinSources(project.relativize(bobGreet)) + if (withIC) { + assertCompiledKotlinSources(project.relativize(bobGreet)) + } } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/scripting/internal/ScriptingGradleSubplugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/scripting/internal/ScriptingGradleSubplugin.kt index b9124b36179..2d16f5c92b1 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/scripting/internal/ScriptingGradleSubplugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/scripting/internal/ScriptingGradleSubplugin.kt @@ -23,11 +23,17 @@ import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.scripting.ScriptingExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.useLazyTaskConfiguration +import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource import java.io.File private const val MISCONFIGURATION_MESSAGE_SUFFIX = "the plugin is probably applied by a mistake" +private const val MIN_SUPPORTED_GRADLE_MAJOR_VERSION = 5 +private const val MIN_SUPPORTED_GRADLE_MINOR_VERSION = 0 + +private const val SCRIPTING_LOG_PREFIX = "kotlin scripting plugin:" + class ScriptingGradleSubplugin : Plugin { companion object { fun isEnabled(project: Project) = project.plugins.findPlugin(ScriptingGradleSubplugin::class.java) != null @@ -55,12 +61,17 @@ class ScriptingGradleSubplugin : Plugin { try { val discoveryClasspathConfigurationName = getDiscoveryClasspathConfigurationName(task.sourceSetName) - project.configurations.findByName(discoveryClasspathConfigurationName)?.let { _ -> - configureScriptsExtensions(project, javaPluginConvention, task.sourceSetName) + if (project.configurations.findByName(discoveryClasspathConfigurationName)?.allDependencies?.isEmpty() == false) { + if (isGradleVersionAtLeast(MIN_SUPPORTED_GRADLE_MAJOR_VERSION, MIN_SUPPORTED_GRADLE_MINOR_VERSION)) { + configureScriptsExtensions(project, javaPluginConvention, task.sourceSetName) + } else { + project.logger.warn("$SCRIPTING_LOG_PREFIX incompatible Gradle version. Please use the plugin with Gradle version $MIN_SUPPORTED_GRADLE_MAJOR_VERSION.$MIN_SUPPORTED_GRADLE_MINOR_VERSION or newer.") + } + } else { + project.logger.warn("$SCRIPTING_LOG_PREFIX $project.${task.name} - configuration not found: $discoveryClasspathConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX") } - ?: project.logger.warn("kotlin scripting plugin: $project.${task.name} - configuration not found: $discoveryClasspathConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX") } catch (e: IllegalStateException) { - project.logger.warn("kotlin scripting plugin: applied in the non-supported environment (error received: ${e.message})") + project.logger.warn("$SCRIPTING_LOG_PREFIX applied in the non-supported environment (error received: ${e.message})") } } } @@ -72,7 +83,7 @@ class ScriptingGradleSubplugin : Plugin { } else { // TODO: implement support for discovery in MPP project: use KotlinSourceSet directly and do not rely on java convevtion sourcesets if (project.multiplatformExtensionOrNull == null) { - project.logger.warn("kotlin scripting plugin: applied to a non-JVM project $project, $MISCONFIGURATION_MESSAGE_SUFFIX") + project.logger.warn("$SCRIPTING_LOG_PREFIX applied to a non-JVM project $project, $MISCONFIGURATION_MESSAGE_SUFFIX") } } } @@ -89,19 +100,19 @@ class ScriptingGradleSubplugin : Plugin { val kotlinSourceSet = sourceSet.getConvention(KOTLIN_DSL_NAME) as? KotlinSourceSet if (kotlinSourceSet == null) { - project.logger.warn("kotlin scripting plugin: kotlin source set not found: $project.$sourceSet, $MISCONFIGURATION_MESSAGE_SUFFIX") + project.logger.warn("$SCRIPTING_LOG_PREFIX kotlin source set not found: $project.$sourceSet, $MISCONFIGURATION_MESSAGE_SUFFIX") } else { val extensions by lazy { val discoveryResultsConfiguration = project.configurations.findByName(discoveryResultsConfigurationName) if (discoveryResultsConfiguration == null) { - project.logger.warn("kotlin scripting plugin: discovery results not found: $project.$discoveryResultsConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX") + project.logger.warn("$SCRIPTING_LOG_PREFIX discovery results not found: $project.$discoveryResultsConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX") emptySet() } else { discoveryResultsConfiguration.files.flatMapTo(HashSet()) { it.readLines().filter(String::isNotBlank) }.also { kotlinSourceSet.addCustomSourceFilesExtensions(it.toList()) - project.logger.debug("kotlin scripting plugin: $project.$sourceSet: discovered script extensions: $it") + project.logger.debug("$SCRIPTING_LOG_PREFIX $project.$sourceSet: discovered script extensions: $it") } } } @@ -133,14 +144,16 @@ private fun configureDiscoveryTransformation( project.configurations.maybeCreate(discoveryResultsConfigurationName).apply { isCanBeConsumed = false } - project.dependencies.apply { - add( - discoveryResultsConfigurationName, - project.withRegisteredDiscoverScriptExtensionsTransform { - discoveryConfiguration.discoverScriptExtensionsFiles() - } - ) - } + if (isGradleVersionAtLeast(MIN_SUPPORTED_GRADLE_MAJOR_VERSION, MIN_SUPPORTED_GRADLE_MINOR_VERSION)) { + project.dependencies.apply { + add( + discoveryResultsConfigurationName, + project.withRegisteredDiscoverScriptExtensionsTransform { + discoveryConfiguration.discoverScriptExtensionsFiles() + } + ) + } + } // otherwise the warning should already be reported in the ScriptingGradleSubplugin.apply } internal class DiscoverScriptExtensionsTransform : ArtifactTransform() { diff --git a/plugins/scripting/scripting-compiler/build.gradle.kts b/plugins/scripting/scripting-compiler/build.gradle.kts index 313c35a7b84..0b306f33a64 100644 --- a/plugins/scripting/scripting-compiler/build.gradle.kts +++ b/plugins/scripting/scripting-compiler/build.gradle.kts @@ -17,7 +17,6 @@ dependencies { compile(project(":kotlin-scripting-compiler-impl")) compile(kotlinStdlib()) compileOnly(project(":kotlin-reflect-api")) - compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false } compileOnly(intellijCoreDep()) { includeJars("intellij-core") } testCompile(project(":compiler:frontend"))