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
@@ -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<GradleSourceSetData>) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode),
compilerPluginId = compilerPluginId,
pluginName = pluginName,
annotationOptionName = annotationOptionName)
pluginName = pluginName)
}
override fun importByModule(facet: KotlinFacet, moduleNode: DataNode<ModuleData>) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetupByModule(moduleNode),
compilerPluginId = compilerPluginId,
pluginName = pluginName,
annotationOptionName = annotationOptionName)
pluginName = pluginName)
}
protected open fun getAnnotationsForPreset(presetName: String): List<String> = 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<GradleSourceSetData>) =
@@ -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<String>, compilerPluginOptions: List<String>): List<String>?
abstract fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>?
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
@@ -31,14 +31,15 @@ fun Module.getSpecialAnnotations(prefix: String): List<String> {
?: 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(
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