From d08a32c7cbf35812950a4ed0471aa1341585415a Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Mon, 4 Jun 2018 17:40:55 +0300 Subject: [PATCH] Kapt: Ignore annotation-processing-gradle compiler plugin we got from Gradle (KT-24714) Cause it's built for the shaded Kotlin compiler and won't work with JPS. --- idea/src/META-INF/gradle-java.xml | 1 + idea/src/META-INF/gradle-java.xml.as33 | 1 + idea/src/META-INF/gradle.xml.181 | 1 + plugins/kapt3/kapt3-idea/build.gradle.kts | 3 +- .../idea/KaptGradleProjectImportHandler.kt | 48 +++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 plugins/kapt3/kapt3-idea/src/org/jetbrains/kotlin/kapt/idea/KaptGradleProjectImportHandler.kt diff --git a/idea/src/META-INF/gradle-java.xml b/idea/src/META-INF/gradle-java.xml index e2d40de3035..8e52e90bb2a 100644 --- a/idea/src/META-INF/gradle-java.xml +++ b/idea/src/META-INF/gradle-java.xml @@ -70,6 +70,7 @@ + diff --git a/idea/src/META-INF/gradle-java.xml.as33 b/idea/src/META-INF/gradle-java.xml.as33 index 550f2ec19a2..ee4572a2149 100644 --- a/idea/src/META-INF/gradle-java.xml.as33 +++ b/idea/src/META-INF/gradle-java.xml.as33 @@ -69,6 +69,7 @@ + diff --git a/idea/src/META-INF/gradle.xml.181 b/idea/src/META-INF/gradle.xml.181 index dba51c1f2ef..6914645dc25 100644 --- a/idea/src/META-INF/gradle.xml.181 +++ b/idea/src/META-INF/gradle.xml.181 @@ -78,6 +78,7 @@ + diff --git a/plugins/kapt3/kapt3-idea/build.gradle.kts b/plugins/kapt3/kapt3-idea/build.gradle.kts index 6f4e45e0cf3..44ac02d931d 100644 --- a/plugins/kapt3/kapt3-idea/build.gradle.kts +++ b/plugins/kapt3/kapt3-idea/build.gradle.kts @@ -11,7 +11,8 @@ dependencies { compile(project(":compiler:frontend")) compile(project(":idea")) { isTransitive = false } compile(project(":idea:kotlin-gradle-tooling")) - compile(project(":kotlin-annotation-processing")) + compile(project(":idea:idea-core")) + compile(project(":idea:idea-gradle")) compileOnly(intellijDep()) compileOnly(intellijPluginDep("gradle")) compileOnly(intellijPluginDep("android")) diff --git a/plugins/kapt3/kapt3-idea/src/org/jetbrains/kotlin/kapt/idea/KaptGradleProjectImportHandler.kt b/plugins/kapt3/kapt3-idea/src/org/jetbrains/kotlin/kapt/idea/KaptGradleProjectImportHandler.kt new file mode 100644 index 00000000000..da882bb7ab0 --- /dev/null +++ b/plugins/kapt3/kapt3-idea/src/org/jetbrains/kotlin/kapt/idea/KaptGradleProjectImportHandler.kt @@ -0,0 +1,48 @@ +/* + * 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.kapt.idea + +import com.intellij.openapi.externalSystem.model.DataNode +import com.intellij.openapi.externalSystem.model.project.ModuleData +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.plugins.gradle.model.data.GradleSourceSetData +import java.io.File + +class KaptGradleProjectImportHandler : GradleProjectImportHandler { + override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) { + modifyCompilerArgumentsForPlugin(facet) + } + + override fun importByModule(facet: KotlinFacet, moduleNode: DataNode) { + modifyCompilerArgumentsForPlugin(facet) + } + + private fun modifyCompilerArgumentsForPlugin(facet: KotlinFacet) { + val facetSettings = facet.configuration.settings + + // Can't reuse const in Kapt3CommandLineProcessor, we don't have Kapt in the IDEA plugin + val compilerPluginId = "org.jetbrains.kotlin.kapt3" + val compilerArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() + + val newPluginOptions = (compilerArguments.pluginOptions ?: emptyArray()).filter { !it.startsWith("plugin:$compilerPluginId:") } + val newPluginClasspath = (compilerArguments.pluginClasspaths ?: emptyArray()).filter { !isKaptCompilerPluginPath(it) } + + fun List.toArrayIfNotEmpty() = takeIf { it.isNotEmpty() }?.toTypedArray() + + compilerArguments.pluginOptions = newPluginOptions.toArrayIfNotEmpty() + compilerArguments.pluginClasspaths = newPluginClasspath.toArrayIfNotEmpty() + + facetSettings.compilerArguments = compilerArguments + } + + private fun isKaptCompilerPluginPath(path: String): Boolean { + val lastIndexOfFile = path.lastIndexOfAny(charArrayOf('/', File.separatorChar)).takeIf { it >= 0 } ?: return false + val fileName = path.drop(lastIndexOfFile + 1).toLowerCase() + return fileName.matches("kotlin\\-annotation\\-processing(\\-gradle)?\\-[0-9].*\\.jar".toRegex()) + } +} \ No newline at end of file