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
This commit is contained in:
Ilya Chernikov
2019-04-12 18:02:29 +02:00
parent 72736d78e4
commit 7c4efb2772
3 changed files with 57 additions and 29 deletions
@@ -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))
}
}
}
}
@@ -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<Project> {
companion object {
fun isEnabled(project: Project) = project.plugins.findPlugin(ScriptingGradleSubplugin::class.java) != null
@@ -55,12 +61,17 @@ class ScriptingGradleSubplugin : Plugin<Project> {
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<Project> {
} 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<Project> {
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<String>()
} 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() {
@@ -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"))