Use Gradle API to import annotations from compiler plugins instead of nasty data storage tasks
Tasks itself will be left for some time until all users migrate to the newer IDE plugin versions.
This commit is contained in:
+11
-17
@@ -19,25 +19,22 @@ package org.jetbrains.kotlin.annotation.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.model.task.TaskData
|
||||
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
|
||||
import com.intellij.openapi.util.Key
|
||||
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
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractGradleImportHandler : GradleProjectImportHandler {
|
||||
private companion object {
|
||||
private val TASK_DESCRIPTION_REGEX = "Supported annotations: ([^; ]*); (?:Presets: (.*?); )?Compiler plugin classpath: (.*?)".toRegex()
|
||||
}
|
||||
|
||||
abstract class AbstractGradleImportHandler<T : AnnotationBasedPluginModel> : GradleProjectImportHandler {
|
||||
abstract val compilerPluginId: String
|
||||
abstract val pluginName: String
|
||||
abstract val annotationOptionName: String
|
||||
abstract val dataStorageTaskName: String
|
||||
abstract val pluginJarFileFromIdea: File
|
||||
|
||||
abstract val modelKey: Key<T>
|
||||
|
||||
override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) {
|
||||
modifyCompilerArgumentsForPlugin(facet, getPluginSetupBySourceSet(sourceSetNode),
|
||||
compilerPluginId = compilerPluginId,
|
||||
@@ -52,20 +49,17 @@ abstract class AbstractGradleImportHandler : GradleProjectImportHandler {
|
||||
|
||||
protected open fun getAnnotationsForPreset(presetName: String): List<String> = emptyList()
|
||||
|
||||
protected open fun getAdditionalOptions(model: T): List<PluginOption> = emptyList()
|
||||
|
||||
private fun getPluginSetupByModule(
|
||||
moduleNode: DataNode<ModuleData>
|
||||
): AnnotationBasedCompilerPluginSetup? {
|
||||
val dataStorageTaskData = moduleNode.children.firstOrNull {
|
||||
val data = it.data as? TaskData ?: return@firstOrNull false
|
||||
data.name == dataStorageTaskName
|
||||
}?.data as? TaskData ?: return null
|
||||
val pluginModel = moduleNode.getUserData(modelKey)?.takeIf { it.isEnabled } ?: return null
|
||||
val annotations = pluginModel.annotations
|
||||
val presets = pluginModel.presets
|
||||
|
||||
val dataStorageTaskDescription = dataStorageTaskData.description ?: return null
|
||||
val matchResult = TASK_DESCRIPTION_REGEX.matchEntire(dataStorageTaskDescription)?.groupValues?.drop(1) ?: return null
|
||||
val (annotationFqNamesList, presets) = matchResult
|
||||
|
||||
val annotationFqNames = annotationFqNamesList.split(',') + presets.split(',').flatMap { getAnnotationsForPreset(it) }
|
||||
val options = annotationFqNames.map { PluginOption(annotationOptionName, it) }
|
||||
val allAnnotations = annotations + presets.flatMap { getAnnotationsForPreset(it) }
|
||||
val options = allAnnotations.map { PluginOption(annotationOptionName, it) } + getAdditionalOptions(pluginModel)
|
||||
|
||||
// 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.
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.annotation.plugin.ide
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.project.*
|
||||
import com.intellij.openapi.util.Key
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.tooling.model.idea.IdeaModule
|
||||
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder
|
||||
import org.jetbrains.plugins.gradle.service.project.AbstractProjectResolverExtension
|
||||
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
|
||||
import java.io.Serializable
|
||||
import java.lang.Exception
|
||||
|
||||
interface AnnotationBasedPluginModel : Serializable {
|
||||
val annotations: List<String>
|
||||
val presets: List<String>
|
||||
|
||||
val isEnabled get() = annotations.isNotEmpty() || presets.isNotEmpty()
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
abstract class AnnotationBasedPluginProjectResolverExtension<T : AnnotationBasedPluginModel> : AbstractProjectResolverExtension() {
|
||||
private companion object {
|
||||
private val LOG = Logger.getInstance(AnnotationBasedPluginProjectResolverExtension::class.java)
|
||||
}
|
||||
|
||||
abstract val modelClass: Class<T>
|
||||
abstract val userDataKey: Key<T>
|
||||
|
||||
override fun getExtraProjectModelClasses() = setOf(modelClass)
|
||||
|
||||
override fun getToolingExtensionsClasses() = setOf(
|
||||
modelClass,
|
||||
AnnotationBasedPluginProjectResolverExtension::class.java,
|
||||
Unit::class.java)
|
||||
|
||||
override fun populateModuleExtraModels(gradleModule: IdeaModule, ideModule: DataNode<ModuleData>) {
|
||||
val model = resolverCtx.getExtraProject(gradleModule, modelClass)
|
||||
|
||||
if (model != null) {
|
||||
ideModule.putUserData(userDataKey, model)
|
||||
}
|
||||
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AnnotationBasedPluginModelBuilderService<T : AnnotationBasedPluginModel> : AbstractKotlinGradleModelBuilder() {
|
||||
abstract val gradlePluginNames: List<String>
|
||||
abstract val extensionName: String
|
||||
|
||||
abstract val modelClass: Class<T>
|
||||
abstract fun createModel(annotations: List<String>, presets: List<String>, extension: Any?): T
|
||||
|
||||
override fun getErrorMessageBuilder(project: Project, e: Exception): ErrorMessageBuilder {
|
||||
return ErrorMessageBuilder.create(project, e, "Gradle import errors")
|
||||
.withDescription("Unable to build $gradlePluginNames plugin configuration")
|
||||
}
|
||||
|
||||
override fun canBuild(modelName: String?): Boolean = modelName == modelClass.name
|
||||
|
||||
override fun buildAll(modelName: String?, project: Project): Any {
|
||||
val plugin: Plugin<*>? = project.findPlugin(gradlePluginNames)
|
||||
val extension: Any? = project.extensions.findByName(extensionName)
|
||||
|
||||
val annotations = mutableListOf<String>()
|
||||
val presets = mutableListOf<String>()
|
||||
|
||||
if (plugin != null && extension != null) {
|
||||
annotations += extension.getList("myAnnotations")
|
||||
presets += extension.getList("myPresets")
|
||||
return createModel(annotations, presets, extension)
|
||||
}
|
||||
|
||||
return createModel(emptyList(), emptyList(), null)
|
||||
}
|
||||
|
||||
private fun Project.findPlugin(names: List<String>): Plugin<*>? {
|
||||
for (name in names) {
|
||||
plugins.findPlugin(name)?.let { return it }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun Any.getList(fieldName: String): List<String> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return getFieldValue(fieldName) as? List<String> ?: emptyList()
|
||||
}
|
||||
|
||||
protected fun Any.getFieldValue(fieldName: String, clazz: Class<*> = this.javaClass): Any? {
|
||||
val field = clazz.declaredFields.firstOrNull { it.name == fieldName }
|
||||
?: return getFieldValue(fieldName, clazz.superclass ?: return null)
|
||||
|
||||
val oldIsAccessible = field.isAccessible
|
||||
try {
|
||||
field.isAccessible = true
|
||||
return field.get(this)
|
||||
} finally {
|
||||
field.isAccessible = oldIsAccessible
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user