From 1fbece9e7df999b90616109a6f0e130a1652900e Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 7 Jun 2018 18:29:23 +0200 Subject: [PATCH] Clean scripting plugin options and classpath on importing from gradle should prevent problems that may appear if JPS will try to load gradle scripting subplugin (cherrypicked with update from 1.2.50) --- idea/idea-core/build.gradle.kts | 2 +- idea/src/META-INF/gradle-java.xml.182 | 1 + idea/src/META-INF/gradle.xml | 1 + idea/src/META-INF/gradle.xml.172 | 1 + .../scripting/scripting-idea/build.gradle.kts | 3 + .../ScriptingGradleProjectImportHandler.kt | 76 +++++++++++++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 plugins/scripting/scripting-idea/src/org/jetbrains/kotlin/scripting/idea/plugin/ScriptingGradleProjectImportHandler.kt diff --git a/idea/idea-core/build.gradle.kts b/idea/idea-core/build.gradle.kts index 40b3207966d..3602013187a 100644 --- a/idea/idea-core/build.gradle.kts +++ b/idea/idea-core/build.gradle.kts @@ -18,7 +18,7 @@ dependencies { compile(project(":idea:ide-common")) compile(project(":idea:idea-jps-common")) compile(project(":plugins:android-extensions-compiler")) - compile(project(":kotlin-scripting-idea")) + compile(project(":kotlin-scripting-compiler")) compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false } compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8")) { isTransitive = false } compileOnly(intellijCoreDep()) { includeJars("intellij-core") } diff --git a/idea/src/META-INF/gradle-java.xml.182 b/idea/src/META-INF/gradle-java.xml.182 index c205b628f8e..8b53827b6af 100644 --- a/idea/src/META-INF/gradle-java.xml.182 +++ b/idea/src/META-INF/gradle-java.xml.182 @@ -56,6 +56,7 @@ + diff --git a/idea/src/META-INF/gradle.xml b/idea/src/META-INF/gradle.xml index efd662ffd06..255ff015624 100644 --- a/idea/src/META-INF/gradle.xml +++ b/idea/src/META-INF/gradle.xml @@ -64,6 +64,7 @@ + diff --git a/idea/src/META-INF/gradle.xml.172 b/idea/src/META-INF/gradle.xml.172 index 25004ee6b4c..d6ea42e8826 100644 --- a/idea/src/META-INF/gradle.xml.172 +++ b/idea/src/META-INF/gradle.xml.172 @@ -67,6 +67,7 @@ + diff --git a/plugins/scripting/scripting-idea/build.gradle.kts b/plugins/scripting/scripting-idea/build.gradle.kts index 7eb1a1b6c76..241f6e32f2d 100644 --- a/plugins/scripting/scripting-idea/build.gradle.kts +++ b/plugins/scripting/scripting-idea/build.gradle.kts @@ -8,6 +8,9 @@ plugins { dependencies { compile(project(":kotlin-scripting-compiler")) + compileOnly(project(":idea:idea-gradle")) + compileOnly(project(":idea:idea-core")) + compileOnly(intellijDep()) } sourceSets { diff --git a/plugins/scripting/scripting-idea/src/org/jetbrains/kotlin/scripting/idea/plugin/ScriptingGradleProjectImportHandler.kt b/plugins/scripting/scripting-idea/src/org/jetbrains/kotlin/scripting/idea/plugin/ScriptingGradleProjectImportHandler.kt new file mode 100644 index 00000000000..b72da4d90ad --- /dev/null +++ b/plugins/scripting/scripting-idea/src/org/jetbrains/kotlin/scripting/idea/plugin/ScriptingGradleProjectImportHandler.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.scripting.idea.plugin + +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler +import org.jetbrains.kotlin.idea.facet.KotlinFacet +import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCommandLineProcessor +import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData +import java.io.File + +class ScriptingGradleProjectImportHandler : GradleProjectImportHandler { + + val compilerPluginId = ScriptingCommandLineProcessor.PLUGIN_ID + val gradlePluginJars = listOf( + "scripting-gradle", // obsolete artifact name, only for compatibility with 1.2.5x, where it was introduced (and immediately dropped afterwards) + "scripting-compiler", + "scripting-compiler-embeddable" + ) + + override fun importBySourceSet( + facet: KotlinFacet, + sourceSetNode: com.intellij.openapi.externalSystem.model.DataNode + ) { + modifyCompilerArgumentsForPlugin(facet, compilerPluginId, gradlePluginJars) + } + + override fun importByModule( + facet: KotlinFacet, + moduleNode: com.intellij.openapi.externalSystem.model.DataNode + ) { + modifyCompilerArgumentsForPlugin(facet, compilerPluginId, gradlePluginJars) + } +} + +// NOTE: partially copied from idePluginUtil.kt, it is not possible to reuse it right now without refactoring +internal fun modifyCompilerArgumentsForPlugin( + facet: KotlinFacet, + compilerPluginId: String, + pluginJarNames: List +) { + val facetSettings = facet.configuration.settings + + // investigate why copyBean() sometimes throws exceptions + val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() + + // TODO: find out where new options should come from (or maybe they are not needed here at all) +// val newOptionsForPlugin = setup?.options?.map { "plugin:$compilerPluginId:${it.key}=${it.value}" } ?: emptyList() + + val oldAllPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") } + val newAllPluginOptions = oldAllPluginOptions // + newOptionsForPlugin + + val filterRegexes = pluginJarNames.map { + "(kotlin-)?(maven-)?$it-.*\\.jar".toRegex() + } + val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) { + val lastIndexOfFile = it.lastIndexOfAny(charArrayOf('/', File.separatorChar)) + if (lastIndexOfFile < 0) { + return@filterTo true + } + !it.drop(lastIndexOfFile + 1).let { fileName -> + filterRegexes.any { fileName.matches(it) } + } + } + + // TODO: find out how to make it - see comment to the newOptionsForPlugin above + val newPluginClasspaths = oldPluginClasspaths // + (setup?.classpath ?: emptyList()) + + commonArguments.pluginOptions = newAllPluginOptions.toTypedArray() + commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray() + + facetSettings.compilerArguments = commonArguments +}