[lombok] Support import from gradle to IDE

Introduce base module for ide compiler plugins
This commit is contained in:
Andrey Zinovyev
2021-04-23 14:50:29 +03:00
committed by TeamCityServer
parent b58bea6fa1
commit 8afb6d2761
35 changed files with 321 additions and 28 deletions
@@ -0,0 +1,26 @@
plugins {
kotlin("jvm")
id("jps-compatible")
}
dependencies {
compile(project(":compiler:util"))
compile(project(":compiler:frontend"))
compile(project(":compiler:cli-common"))
compile(project(":idea"))
compile(project(":idea:idea-jvm"))
compile(project(":idea:idea-jps-common"))
compile(project(":idea:idea-gradle"))
compile(project(":idea:idea-maven"))
excludeInAndroidStudio(rootProject) { compileOnly(intellijPluginDep("maven")) }
compileOnly(intellijPluginDep("gradle"))
compileOnly(intellijDep())
compileOnly(project(":idea:kotlin-gradle-tooling"))
}
sourceSets {
"main" { projectDefault() }
"test" {}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.plugin.ide
import com.intellij.openapi.externalSystem.model.DataNode
import com.intellij.openapi.externalSystem.model.ProjectKeys
import com.intellij.openapi.externalSystem.model.project.ModuleData
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.util.Key
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
abstract class AbstractGradleImportHandler<T> : GradleProjectImportHandler {
abstract val modelKey: Key<T>
abstract val pluginJarFileFromIdea: File
abstract val compilerPluginId: String
abstract val pluginName: String
abstract fun getOptions(model: T): List<CompilerPluginSetup.PluginOption>?
override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) {
modifyCompilerArgumentsForPlugin(
facet, getPluginSetupBySourceSet(sourceSetNode),
compilerPluginId = compilerPluginId,
pluginName = pluginName
)
}
override fun importByModule(facet: KotlinFacet, moduleNode: DataNode<ModuleData>) {
modifyCompilerArgumentsForPlugin(
facet, getPluginSetupByModule(moduleNode),
compilerPluginId = compilerPluginId,
pluginName = pluginName
)
}
private fun getPluginSetupByModule(moduleNode: DataNode<ModuleData>): CompilerPluginSetup? {
val pluginModel = moduleNode.getCopyableUserData(modelKey) ?: return null
val options = getOptions(pluginModel) ?: return null
// 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 CompilerPluginSetup(options, classpath)
}
private fun getPluginSetupBySourceSet(sourceSetNode: DataNode<GradleSourceSetData>) =
ExternalSystemApiUtil.findParent(sourceSetNode, ProjectKeys.MODULE)?.let { getPluginSetupByModule(it) }
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.plugin.ide
import org.jdom.Element
import org.jdom.Text
import org.jetbrains.idea.maven.project.MavenProject
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
import org.jetbrains.kotlin.idea.maven.KotlinMavenImporter.Companion.KOTLIN_PLUGIN_ARTIFACT_ID
import java.io.File
import org.jetbrains.kotlin.plugin.ide.CompilerPluginSetup.PluginOption
abstract class AbstractMavenImportHandler : MavenProjectImportHandler {
abstract val compilerPluginId: String
abstract val pluginName: String
abstract val pluginJarFileFromIdea: File
override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) {
modifyCompilerArgumentsForPlugin(
facet, getPluginSetup(mavenProject),
compilerPluginId = compilerPluginId,
pluginName = pluginName
)
}
abstract fun getOptions(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<PluginOption>?
private fun getPluginSetup(mavenProject: MavenProject): CompilerPluginSetup? {
val kotlinPlugin = mavenProject.plugins.firstOrNull {
it.groupId == KOTLIN_PLUGIN_GROUP_ID && it.artifactId == KOTLIN_PLUGIN_ARTIFACT_ID
} ?: return null
val configuration = kotlinPlugin.configurationElement ?: return null
val enabledCompilerPlugins = configuration.getElement("compilerPlugins")
?.getElements()
?.flatMap { plugin -> plugin.content.mapNotNull { (it as? Text)?.text } }
?: emptyList()
val compilerPluginOptions = configuration.getElement("pluginOptions")
?.getElements()
?.flatMap { it.content }
?.mapTo(mutableListOf()) { (it as Text).text }
?: mutableListOf<String>()
// We can't use the plugin from Gradle as it may have the incompatible version
val classpath = listOf(pluginJarFileFromIdea.absolutePath)
val options = getOptions(enabledCompilerPlugins, compilerPluginOptions) ?: return null
return CompilerPluginSetup(options, classpath)
}
private fun Element.getElement(name: String) = content.firstOrNull { it is Element && it.name == name } as? Element
@Suppress("UNCHECKED_CAST")
private fun Element.getElements() = content.filterIsInstance<Element>()
}
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.plugin.ide
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import java.io.File
//todo this is basically a copy-paste of the same file from annotation-based-compiler-plugins-ide-support
//and should be merged together later
class CompilerPluginSetup(val options: List<PluginOption>, val classpath: List<String>) {
class PluginOption(val key: String, val value: String)
}
internal fun modifyCompilerArgumentsForPlugin(
facet: KotlinFacet,
setup: CompilerPluginSetup?,
compilerPluginId: String,
pluginName: String
) {
val facetSettings = facet.configuration.settings
// investigate why copyBean() sometimes throws exceptions
val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl()
/** See [CommonCompilerArguments.PLUGIN_OPTION_FORMAT] **/
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 oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) {
val lastIndexOfFile = it.lastIndexOfAny(charArrayOf('/', File.separatorChar))
if (lastIndexOfFile < 0) {
return@filterTo true
}
!it.drop(lastIndexOfFile + 1).matches("(kotlin-)?(maven-)?$pluginName-.*\\.jar".toRegex())
}
val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList())
commonArguments.pluginOptions = newAllPluginOptions.toTypedArray()
commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray()
facetSettings.compilerArguments = commonArguments
}