Fix and refactor scripting subplugin application logic

so warning is not shown when discovery configuration is not populated
fix #KT-31124
refactor code for clarity on the way
also apply scripting subplugin after main plugin to ensure that
configurations are created before scripting is initialized
This commit is contained in:
Ilya Chernikov
2019-04-23 13:58:44 +02:00
parent 917fc56f36
commit c3a439a829
3 changed files with 24 additions and 11 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.gradle
import org.gradle.api.logging.LogLevel
import org.jetbrains.kotlin.gradle.plugin.MULTIPLE_KOTLIN_PLUGINS_LOADED_WARNING
import org.jetbrains.kotlin.gradle.plugin.MULTIPLE_KOTLIN_PLUGINS_SPECIFIC_PROJECTS_WARNING
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
import org.jetbrains.kotlin.gradle.tasks.USING_JVM_INCREMENTAL_COMPILATION_MESSAGE
import org.jetbrains.kotlin.gradle.util.*
import org.junit.Test
@@ -995,4 +996,12 @@ class KotlinGradleIT : BaseGradleIT() {
assertSuccessful()
}
}
@Test
fun testNoScriptingWarning() = with(Project("simpleProject")) {
// KT-31124
build {
assertNotContains(ScriptingGradleSubplugin.MISCONFIGURATION_MESSAGE_SUFFIX)
}
}
}
@@ -569,9 +569,9 @@ internal open class KotlinPlugin(
}
(project.kotlinExtension as KotlinJvmProjectExtension).target = target
project.pluginManager.apply(ScriptingGradleSubplugin::class.java)
super.apply(project)
project.pluginManager.apply(ScriptingGradleSubplugin::class.java)
}
}
@@ -27,8 +27,6 @@ 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
@@ -36,6 +34,8 @@ private const val SCRIPTING_LOG_PREFIX = "kotlin scripting plugin:"
class ScriptingGradleSubplugin : Plugin<Project> {
companion object {
const val MISCONFIGURATION_MESSAGE_SUFFIX = "the plugin is probably applied by a mistake"
fun isEnabled(project: Project) = project.plugins.findPlugin(ScriptingGradleSubplugin::class.java) != null
fun configureForSourceSet(project: Project, sourceSetName: String) {
@@ -44,6 +44,7 @@ class ScriptingGradleSubplugin : Plugin<Project> {
isCanBeConsumed = false
description = "Script filename extensions discovery classpath configuration"
}
project.logger.info("$SCRIPTING_LOG_PREFIX created the scripting discovery configuration: ${discoveryConfiguration.name}")
configureDiscoveryTransformation(project, discoveryConfiguration, getDiscoveryResultsConfigurationName(sourceSetName))
}
@@ -61,14 +62,17 @@ class ScriptingGradleSubplugin : Plugin<Project> {
try {
val discoveryClasspathConfigurationName = getDiscoveryClasspathConfigurationName(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.")
val discoveryClasspathConfiguration = project.configurations.findByName(discoveryClasspathConfigurationName)
when {
discoveryClasspathConfiguration == null ->
project.logger.warn("$SCRIPTING_LOG_PREFIX $project.${task.name} - configuration not found: $discoveryClasspathConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX")
discoveryClasspathConfiguration.allDependencies.isEmpty() -> {
// skip further checks - user did not configured any discovery sources
}
} else {
project.logger.warn("$SCRIPTING_LOG_PREFIX $project.${task.name} - configuration not found: $discoveryClasspathConfigurationName, $MISCONFIGURATION_MESSAGE_SUFFIX")
!isGradleVersionAtLeast(MIN_SUPPORTED_GRADLE_MAJOR_VERSION, MIN_SUPPORTED_GRADLE_MINOR_VERSION) ->
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 ->
configureScriptsExtensions(project, javaPluginConvention, task.sourceSetName)
}
} catch (e: IllegalStateException) {
project.logger.warn("$SCRIPTING_LOG_PREFIX applied in the non-supported environment (error received: ${e.message})")