[lombok] Support import from gradle to IDE
Introduce base module for ide compiler plugins
This commit is contained in:
committed by
TeamCityServer
parent
b58bea6fa1
commit
8afb6d2761
@@ -7,6 +7,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":plugins:lombok:lombok-compiler-plugin"))
|
||||
implementation(project(":plugins:base-compiler-plugins-ide-support"))
|
||||
|
||||
compileOnly(project(":idea"))
|
||||
compileOnly(project(":idea:idea-jvm"))
|
||||
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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.lombok.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.lombok.ide.CompilerPluginSetup.PluginOption
|
||||
|
||||
abstract class AbstractMavenImportHandler : MavenProjectImportHandler {
|
||||
abstract val compilerPluginId: String
|
||||
abstract val pluginName: String
|
||||
// abstract val mavenPluginArtifactName: 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>()
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.lombok.ide
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
import org.jetbrains.kotlin.lombok.LombokCommandLineProcessor
|
||||
import org.jetbrains.kotlin.lombok.LombokCommandLineProcessor.Companion.CONFIG_FILE_OPTION
|
||||
import org.jetbrains.kotlin.plugin.ide.AbstractGradleImportHandler
|
||||
import org.jetbrains.kotlin.plugin.ide.CompilerPluginSetup
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
import java.io.File
|
||||
|
||||
class LombokGradleProjectImportHandler : AbstractGradleImportHandler<LombokModel>() {
|
||||
|
||||
override val modelKey: Key<LombokModel> = LombokGradleProjectResolverExtension.KEY
|
||||
override val pluginJarFileFromIdea: File = PathUtil.kotlinPathsForIdeaPlugin.lombokPluginJarPath
|
||||
override val compilerPluginId: String = LombokCommandLineProcessor.PLUGIN_ID
|
||||
override val pluginName: String = "lombok"
|
||||
|
||||
override fun getOptions(model: LombokModel): List<CompilerPluginSetup.PluginOption> =
|
||||
listOfNotNull(
|
||||
model.configurationFile?.let {
|
||||
CompilerPluginSetup.PluginOption(CONFIG_FILE_OPTION.optionName, it.path)
|
||||
}
|
||||
)
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.lombok.ide
|
||||
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.project.ModuleData
|
||||
import com.intellij.openapi.util.Key
|
||||
import org.gradle.tooling.model.idea.IdeaModule
|
||||
import org.jetbrains.plugins.gradle.service.project.AbstractProjectResolverExtension
|
||||
|
||||
class LombokGradleProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
|
||||
private val modelClass: Class<LombokModel> = LombokModel::class.java
|
||||
private val userDataKey: Key<LombokModel> = KEY
|
||||
|
||||
override fun getExtraProjectModelClasses() = setOf(modelClass)
|
||||
|
||||
override fun getToolingExtensionsClasses() = setOf(
|
||||
modelClass,
|
||||
LombokGradleProjectResolverExtension::class.java,
|
||||
Unit::class.java
|
||||
)
|
||||
|
||||
override fun populateModuleExtraModels(gradleModule: IdeaModule, ideModule: DataNode<ModuleData>) {
|
||||
val model = resolverCtx.getExtraProject(gradleModule, modelClass)
|
||||
|
||||
if (model != null) {
|
||||
ideModule.putCopyableUserData(userDataKey, model)
|
||||
}
|
||||
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val KEY = Key<LombokModel>("LombokModel")
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -7,7 +7,8 @@ package org.jetbrains.kotlin.lombok.ide
|
||||
|
||||
import org.jetbrains.kotlin.lombok.LombokCommandLineProcessor
|
||||
import org.jetbrains.kotlin.lombok.LombokCommandLineProcessor.Companion.CONFIG_FILE_OPTION
|
||||
import org.jetbrains.kotlin.lombok.ide.CompilerPluginSetup.PluginOption
|
||||
import org.jetbrains.kotlin.plugin.ide.AbstractMavenImportHandler
|
||||
import org.jetbrains.kotlin.plugin.ide.CompilerPluginSetup.PluginOption
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
import java.io.File
|
||||
|
||||
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.lombok.ide
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import java.io.File
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user