From 3512675d96bd3b7c4c06df4afb687e6ff2d4f3ab Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Fri, 15 Dec 2017 18:32:02 +0900 Subject: [PATCH] Compiler plugins: Refactor Maven import handlers in order to support other plugin options --- .../src/AllOpenMavenProjectImportHandler.kt | 6 +++--- .../src/AbstractGradleImportHandler.kt | 10 +++++----- .../src/AbstractMavenImportHandler.kt | 11 +++++------ .../src/idePluginUtils.kt | 15 ++++++++------- .../src/NoArgMavenProjectImportHandler.kt | 6 +++--- .../SamWithReceiverMavenProjectImportHandler.kt | 7 ++++--- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt b/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt index 1c8b82db04d..35c21090d48 100644 --- a/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt +++ b/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.allopen.ide import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption import org.jetbrains.kotlin.utils.PathUtil class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { @@ -27,11 +28,10 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID override val pluginName = "allopen" - override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name override val mavenPluginArtifactName = "kotlin-maven-allopen" override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath - override fun getAnnotations(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { + override fun getOptions(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { if ("all-open" !in enabledCompilerPlugins && "spring" !in enabledCompilerPlugins) { return null } @@ -49,6 +49,6 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { text.substring(ANNOTATION_PARAMETER_PREFIX.length) }) - return annotations + return annotations.map { PluginOption(AllOpenCommandLineProcessor.ANNOTATION_OPTION.name, it) } } } diff --git a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt index d0a3edeff43..6d196fbe9e2 100644 --- a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt +++ b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractGradleImportHandler.kt @@ -21,6 +21,7 @@ import com.intellij.openapi.externalSystem.model.ProjectKeys import com.intellij.openapi.externalSystem.model.project.ModuleData import com.intellij.openapi.externalSystem.model.task.TaskData import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData @@ -40,15 +41,13 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler { override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) { modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode), compilerPluginId = compilerPluginId, - pluginName = pluginName, - annotationOptionName = annotationOptionName) + pluginName = pluginName) } override fun importByModule(facet: KotlinFacet, moduleNode: DataNode) { modifyCompilerArgumentsForPlugin(facet, getPluginSetupByModule(moduleNode), compilerPluginId = compilerPluginId, - pluginName = pluginName, - annotationOptionName = annotationOptionName) + pluginName = pluginName) } protected open fun getAnnotationsForPreset(presetName: String): List = emptyList() @@ -66,12 +65,13 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler { val (annotationFqNamesList, presets) = matchResult val annotationFqNames = annotationFqNamesList.split(',') + presets.split(',').flatMap { getAnnotationsForPreset(it) } + val options = annotationFqNames.map { PluginOption(annotationOptionName, it) } // For now we can't use plugins from Gradle cause they're shaded and may have an incompatible version. // So we use ones from the IDEA plugin. val classpath = listOf(pluginJarFileFromIdea.absolutePath) - return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath) + return AnnotationBasedCompilerPluginSetup(options, classpath) } private fun getPluginSetupBySourceSet(sourceSetNode: DataNode) = diff --git a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractMavenImportHandler.kt b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractMavenImportHandler.kt index 32d2dc0d119..c3fe243686c 100644 --- a/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractMavenImportHandler.kt +++ b/plugins/annotation-based-compiler-plugins-ide-support/src/AbstractMavenImportHandler.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.annotation.plugin.ide import org.jdom.Element import org.jdom.Text import org.jetbrains.idea.maven.project.MavenProject +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.jetbrains.kotlin.idea.maven.MavenProjectImportHandler import org.jetbrains.kotlin.idea.maven.KotlinMavenImporter.Companion.KOTLIN_PLUGIN_GROUP_ID @@ -28,18 +29,16 @@ import java.io.File abstract class AbstractMavenImportHandler : MavenProjectImportHandler { abstract val compilerPluginId: String abstract val pluginName: String - abstract val annotationOptionName: String abstract val mavenPluginArtifactName: String abstract val pluginJarFileFromIdea: File override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) { modifyCompilerArgumentsForPlugin(facet, getPluginSetup(mavenProject), compilerPluginId = compilerPluginId, - pluginName = pluginName, - annotationOptionName = annotationOptionName) + pluginName = pluginName) } - abstract fun getAnnotations(enabledCompilerPlugins: List, compilerPluginOptions: List): List? + abstract fun getOptions(enabledCompilerPlugins: List, compilerPluginOptions: List): List? private fun getPluginSetup(mavenProject: MavenProject): AnnotationBasedCompilerPluginSetup? { val kotlinPlugin = mavenProject.plugins.firstOrNull { @@ -62,8 +61,8 @@ abstract class AbstractMavenImportHandler : MavenProjectImportHandler { // We can't use the plugin from Gradle as it may have the incompatible version val classpath = listOf(pluginJarFileFromIdea.absolutePath) - val annotationFqNames = getAnnotations(enabledCompilerPlugins, compilerPluginOptions) ?: return null - return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath) + val options = getOptions(enabledCompilerPlugins, compilerPluginOptions) ?: return null + return AnnotationBasedCompilerPluginSetup(options, classpath) } private fun Element.getElement(name: String) = content.firstOrNull { it is Element && it.name == name } as? Element diff --git a/plugins/annotation-based-compiler-plugins-ide-support/src/idePluginUtils.kt b/plugins/annotation-based-compiler-plugins-ide-support/src/idePluginUtils.kt index 13a53e593ef..01a5b11f973 100644 --- a/plugins/annotation-based-compiler-plugins-ide-support/src/idePluginUtils.kt +++ b/plugins/annotation-based-compiler-plugins-ide-support/src/idePluginUtils.kt @@ -31,14 +31,15 @@ fun Module.getSpecialAnnotations(prefix: String): List { ?: emptyList() } -internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List, val classpath: List) +class AnnotationBasedCompilerPluginSetup(val options: List, val classpath: List) { + class PluginOption(val key: String, val value: String) +} internal fun modifyCompilerArgumentsForPlugin( facet: KotlinFacet, setup: AnnotationBasedCompilerPluginSetup?, compilerPluginId: String, - pluginName: String, - annotationOptionName: String + pluginName: String ) { val facetSettings = facet.configuration.settings @@ -46,10 +47,10 @@ internal fun modifyCompilerArgumentsForPlugin( val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() /** See [CommonCompilerArguments.PLUGIN_OPTION_FORMAT] **/ - val annotationOptions = setup?.annotationFqNames?.map { "plugin:$compilerPluginId:$annotationOptionName=$it" } ?: emptyList() + val newOptionsForPlugin = setup?.options?.map { "plugin:$compilerPluginId:${it.key}=${it.value}" } ?: emptyList() - val oldPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") } - val newPluginOptions = oldPluginOptions + annotationOptions + val oldAllPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") } + val newAllPluginOptions = oldAllPluginOptions + newOptionsForPlugin val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) { val lastIndexOfFile = it.lastIndexOfAny(charArrayOf('/', File.separatorChar)) @@ -61,7 +62,7 @@ internal fun modifyCompilerArgumentsForPlugin( val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList()) - commonArguments.pluginOptions = newPluginOptions.toTypedArray() + commonArguments.pluginOptions = newAllPluginOptions.toTypedArray() commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray() facetSettings.compilerArguments = commonArguments diff --git a/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt b/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt index 7ede4ef6eed..dc23e182924 100644 --- a/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt +++ b/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.noarg.ide import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor import org.jetbrains.kotlin.utils.PathUtil @@ -27,11 +28,10 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() { override val compilerPluginId = NoArgCommandLineProcessor.PLUGIN_ID override val pluginName = "noarg" - override val annotationOptionName = NoArgCommandLineProcessor.ANNOTATION_OPTION.name override val mavenPluginArtifactName = "kotlin-maven-noarg" override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.noArgPluginJarPath - override fun getAnnotations(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { + override fun getOptions(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { if ("no-arg" !in enabledCompilerPlugins && "jpa" !in enabledCompilerPlugins) { return null } @@ -48,6 +48,6 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() { text.substring(ANNOTATATION_PARAMETER_PREFIX.length) }) - return annotations + return annotations.mapTo(mutableListOf()) { PluginOption(NoArgCommandLineProcessor.ANNOTATION_OPTION.name, it) } } } diff --git a/plugins/sam-with-receiver/sam-with-receiver-ide/src/SamWithReceiverMavenProjectImportHandler.kt b/plugins/sam-with-receiver/sam-with-receiver-ide/src/SamWithReceiverMavenProjectImportHandler.kt index b838214383d..bf29c986eff 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-ide/src/SamWithReceiverMavenProjectImportHandler.kt +++ b/plugins/sam-with-receiver/sam-with-receiver-ide/src/SamWithReceiverMavenProjectImportHandler.kt @@ -17,6 +17,8 @@ package org.jetbrains.kotlin.samWithReceiver.ide import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup +import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor import org.jetbrains.kotlin.utils.PathUtil @@ -27,11 +29,10 @@ class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() { override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID override val pluginName = "samWithReceiver" - override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name override val mavenPluginArtifactName = "kotlin-maven-sam-with-receiver" override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath - override fun getAnnotations(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { + override fun getOptions(enabledCompilerPlugins: List, compilerPluginOptions: List): List? { if ("sam-with-receiver" !in enabledCompilerPlugins) { return null } @@ -49,6 +50,6 @@ class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() { text.substring(ANNOTATION_PARAMETER_PREFIX.length) }) - return annotations + return annotations.map { PluginOption(SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name, it) } } }