Compiler plugins: Refactor Maven import handlers in order to support other plugin options

This commit is contained in:
Yan Zhulanow
2017-12-15 18:32:02 +09:00
parent a85b4ddb0f
commit 3512675d96
6 changed files with 28 additions and 27 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.allopen.ide
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedCompilerPluginSetup.PluginOption
import org.jetbrains.kotlin.utils.PathUtil import org.jetbrains.kotlin.utils.PathUtil
class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
@@ -27,11 +28,10 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
override val pluginName = "allopen" override val pluginName = "allopen"
override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-allopen" override val mavenPluginArtifactName = "kotlin-maven-allopen"
override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? { override fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>? {
if ("all-open" !in enabledCompilerPlugins && "spring" !in enabledCompilerPlugins) { if ("all-open" !in enabledCompilerPlugins && "spring" !in enabledCompilerPlugins) {
return null return null
} }
@@ -49,6 +49,6 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
text.substring(ANNOTATION_PARAMETER_PREFIX.length) text.substring(ANNOTATION_PARAMETER_PREFIX.length)
}) })
return annotations return annotations.map { PluginOption(AllOpenCommandLineProcessor.ANNOTATION_OPTION.name, it) }
} }
} }
@@ -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.project.ModuleData
import com.intellij.openapi.externalSystem.model.task.TaskData import com.intellij.openapi.externalSystem.model.task.TaskData
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil 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.configuration.GradleProjectImportHandler
import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
@@ -40,15 +41,13 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler {
override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) { override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode), modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode),
compilerPluginId = compilerPluginId, compilerPluginId = compilerPluginId,
pluginName = pluginName, pluginName = pluginName)
annotationOptionName = annotationOptionName)
} }
override fun importByModule(facet: KotlinFacet, moduleNode: DataNode<ModuleData>) { override fun importByModule(facet: KotlinFacet, moduleNode: DataNode<ModuleData>) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetupByModule(moduleNode), modifyCompilerArgumentsForPlugin(facet, getPluginSetupByModule(moduleNode),
compilerPluginId = compilerPluginId, compilerPluginId = compilerPluginId,
pluginName = pluginName, pluginName = pluginName)
annotationOptionName = annotationOptionName)
} }
protected open fun getAnnotationsForPreset(presetName: String): List<String> = emptyList() protected open fun getAnnotationsForPreset(presetName: String): List<String> = emptyList()
@@ -66,12 +65,13 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler {
val (annotationFqNamesList, presets) = matchResult val (annotationFqNamesList, presets) = matchResult
val annotationFqNames = annotationFqNamesList.split(',') + presets.split(',').flatMap { getAnnotationsForPreset(it) } 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. // 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. // So we use ones from the IDEA plugin.
val classpath = listOf(pluginJarFileFromIdea.absolutePath) val classpath = listOf(pluginJarFileFromIdea.absolutePath)
return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath) return AnnotationBasedCompilerPluginSetup(options, classpath)
} }
private fun getPluginSetupBySourceSet(sourceSetNode: DataNode<GradleSourceSetData>) = private fun getPluginSetupBySourceSet(sourceSetNode: DataNode<GradleSourceSetData>) =
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.annotation.plugin.ide
import org.jdom.Element import org.jdom.Element
import org.jdom.Text import org.jdom.Text
import org.jetbrains.idea.maven.project.MavenProject 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.facet.KotlinFacet
import org.jetbrains.kotlin.idea.maven.MavenProjectImportHandler import org.jetbrains.kotlin.idea.maven.MavenProjectImportHandler
import org.jetbrains.kotlin.idea.maven.KotlinMavenImporter.Companion.KOTLIN_PLUGIN_GROUP_ID 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 class AbstractMavenImportHandler : MavenProjectImportHandler {
abstract val compilerPluginId: String abstract val compilerPluginId: String
abstract val pluginName: String abstract val pluginName: String
abstract val annotationOptionName: String
abstract val mavenPluginArtifactName: String abstract val mavenPluginArtifactName: String
abstract val pluginJarFileFromIdea: File abstract val pluginJarFileFromIdea: File
override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) { override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetup(mavenProject), modifyCompilerArgumentsForPlugin(facet, getPluginSetup(mavenProject),
compilerPluginId = compilerPluginId, compilerPluginId = compilerPluginId,
pluginName = pluginName, pluginName = pluginName)
annotationOptionName = annotationOptionName)
} }
abstract fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? abstract fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>?
private fun getPluginSetup(mavenProject: MavenProject): AnnotationBasedCompilerPluginSetup? { private fun getPluginSetup(mavenProject: MavenProject): AnnotationBasedCompilerPluginSetup? {
val kotlinPlugin = mavenProject.plugins.firstOrNull { 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 // We can't use the plugin from Gradle as it may have the incompatible version
val classpath = listOf(pluginJarFileFromIdea.absolutePath) val classpath = listOf(pluginJarFileFromIdea.absolutePath)
val annotationFqNames = getAnnotations(enabledCompilerPlugins, compilerPluginOptions) ?: return null val options = getOptions(enabledCompilerPlugins, compilerPluginOptions) ?: return null
return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath) return AnnotationBasedCompilerPluginSetup(options, classpath)
} }
private fun Element.getElement(name: String) = content.firstOrNull { it is Element && it.name == name } as? Element private fun Element.getElement(name: String) = content.firstOrNull { it is Element && it.name == name } as? Element
@@ -31,14 +31,15 @@ fun Module.getSpecialAnnotations(prefix: String): List<String> {
?: emptyList() ?: emptyList()
} }
internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List<String>, val classpath: List<String>) class AnnotationBasedCompilerPluginSetup(val options: List<PluginOption>, val classpath: List<String>) {
class PluginOption(val key: String, val value: String)
}
internal fun modifyCompilerArgumentsForPlugin( internal fun modifyCompilerArgumentsForPlugin(
facet: KotlinFacet, facet: KotlinFacet,
setup: AnnotationBasedCompilerPluginSetup?, setup: AnnotationBasedCompilerPluginSetup?,
compilerPluginId: String, compilerPluginId: String,
pluginName: String, pluginName: String
annotationOptionName: String
) { ) {
val facetSettings = facet.configuration.settings val facetSettings = facet.configuration.settings
@@ -46,10 +47,10 @@ internal fun modifyCompilerArgumentsForPlugin(
val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl()
/** See [CommonCompilerArguments.PLUGIN_OPTION_FORMAT] **/ /** 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 oldAllPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") }
val newPluginOptions = oldPluginOptions + annotationOptions val newAllPluginOptions = oldAllPluginOptions + newOptionsForPlugin
val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) { val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) {
val lastIndexOfFile = it.lastIndexOfAny(charArrayOf('/', File.separatorChar)) val lastIndexOfFile = it.lastIndexOfAny(charArrayOf('/', File.separatorChar))
@@ -61,7 +62,7 @@ internal fun modifyCompilerArgumentsForPlugin(
val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList()) val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList())
commonArguments.pluginOptions = newPluginOptions.toTypedArray() commonArguments.pluginOptions = newAllPluginOptions.toTypedArray()
commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray() commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray()
facetSettings.compilerArguments = commonArguments facetSettings.compilerArguments = commonArguments
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.noarg.ide package org.jetbrains.kotlin.noarg.ide
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler 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.noarg.NoArgCommandLineProcessor
import org.jetbrains.kotlin.utils.PathUtil import org.jetbrains.kotlin.utils.PathUtil
@@ -27,11 +28,10 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() {
override val compilerPluginId = NoArgCommandLineProcessor.PLUGIN_ID override val compilerPluginId = NoArgCommandLineProcessor.PLUGIN_ID
override val pluginName = "noarg" override val pluginName = "noarg"
override val annotationOptionName = NoArgCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-noarg" override val mavenPluginArtifactName = "kotlin-maven-noarg"
override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.noArgPluginJarPath override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.noArgPluginJarPath
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? { override fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>? {
if ("no-arg" !in enabledCompilerPlugins && "jpa" !in enabledCompilerPlugins) { if ("no-arg" !in enabledCompilerPlugins && "jpa" !in enabledCompilerPlugins) {
return null return null
} }
@@ -48,6 +48,6 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() {
text.substring(ANNOTATATION_PARAMETER_PREFIX.length) text.substring(ANNOTATATION_PARAMETER_PREFIX.length)
}) })
return annotations return annotations.mapTo(mutableListOf()) { PluginOption(NoArgCommandLineProcessor.ANNOTATION_OPTION.name, it) }
} }
} }
@@ -17,6 +17,8 @@
package org.jetbrains.kotlin.samWithReceiver.ide package org.jetbrains.kotlin.samWithReceiver.ide
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler 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.samWithReceiver.SamWithReceiverCommandLineProcessor
import org.jetbrains.kotlin.utils.PathUtil import org.jetbrains.kotlin.utils.PathUtil
@@ -27,11 +29,10 @@ class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() {
override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID
override val pluginName = "samWithReceiver" override val pluginName = "samWithReceiver"
override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-sam-with-receiver" override val mavenPluginArtifactName = "kotlin-maven-sam-with-receiver"
override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath override val pluginJarFileFromIdea = PathUtil.kotlinPathsForIdeaPlugin.allOpenPluginJarPath
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? { override fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>? {
if ("sam-with-receiver" !in enabledCompilerPlugins) { if ("sam-with-receiver" !in enabledCompilerPlugins) {
return null return null
} }
@@ -49,6 +50,6 @@ class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() {
text.substring(ANNOTATION_PARAMETER_PREFIX.length) text.substring(ANNOTATION_PARAMETER_PREFIX.length)
}) })
return annotations return annotations.map { PluginOption(SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name, it) }
} }
} }