AllOpen: Add IDE integration

This commit is contained in:
Yan Zhulanow
2016-12-01 17:43:00 +03:00
committed by Yan Zhulanow
parent 4d638c2cfd
commit 6abde4223b
53 changed files with 677 additions and 175 deletions
@@ -0,0 +1,66 @@
/*
* Copyright 2010-2016 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.externalSystem.model.DataNode
import com.intellij.openapi.externalSystem.model.project.ModuleData
import com.intellij.openapi.externalSystem.model.task.TaskData
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: (.*?); Compiler plugin classpath: (.*)".toRegex()
}
abstract val compilerPluginId: String
abstract val pluginName: String
abstract val annotationOptionName: String
abstract val dataStorageTaskName: String
abstract val pluginJarFileFromIdea: File
override fun invoke(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetup(sourceSetNode),
compilerPluginId = compilerPluginId,
pluginName = pluginName,
annotationOptionName = annotationOptionName)
}
private fun getPluginSetup(
sourceSetNode: DataNode<GradleSourceSetData>
): AnnotationBasedCompilerPluginSetup? {
val moduleNode = sourceSetNode.parent ?: return null
if (moduleNode.data !is ModuleData) return null
val dataStorageTaskData = moduleNode.children.firstOrNull {
val data = it.data as? TaskData ?: return@firstOrNull false
data.name == dataStorageTaskName
}?.data as? TaskData ?: return null
val dataStorageTaskDescription = dataStorageTaskData.description ?: return null
val (annotationFqNamesList, classpathList) = TASK_DESCRIPTION_REGEX.matchEntire(
dataStorageTaskDescription)?.groupValues?.drop(1) ?: return null
val annotationFqNames = annotationFqNamesList.split(',')
// For now we can't use plugins from Gradle cause they're shaded. So we use ones from the IDEA plugin
val classpath = listOf(pluginJarFileFromIdea.absolutePath)
return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath)
}
}
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2016 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 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
abstract class AbstractMavenImportHandler : MavenProjectImportHandler {
abstract val compilerPluginId: String
abstract val pluginName: String
abstract val annotationOptionName: String
abstract val mavenPluginArtifactName: String
override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) {
modifyCompilerArgumentsForPlugin(facet, getPluginSetup(mavenProject),
compilerPluginId = compilerPluginId,
pluginName = pluginName,
annotationOptionName = annotationOptionName)
}
abstract fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>?
private fun getPluginSetup(mavenProject: MavenProject): AnnotationBasedCompilerPluginSetup? {
val kotlinPlugin = mavenProject.plugins.firstOrNull {
it.groupId == KOTLIN_PLUGIN_GROUP_ID && it.artifactId == KOTLIN_PLUGIN_ARTIFACT_ID
} ?: return null
val runtimeJarFile = mavenProject.dependencies
.firstOrNull { it.groupId == KOTLIN_PLUGIN_GROUP_ID &&
(it.artifactId == "kotlin-runtime" || it.artifactId == "kotlin-stdlib") }
?.file ?: return null
val runtimeVersion = runtimeJarFile.parentFile.name
val mavenCompilerPluginJar = File(runtimeJarFile.parentFile.parentFile.parentFile,
"$mavenPluginArtifactName/$runtimeVersion/$mavenPluginArtifactName-$runtimeVersion.jar")
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>()
val annotationFqNames = getAnnotations(enabledCompilerPlugins, compilerPluginOptions) ?: return null
return AnnotationBasedCompilerPluginSetup(annotationFqNames, listOf(mavenCompilerPluginJar.absolutePath))
}
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,52 @@
/*
* Copyright 2010-2016 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 org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.idea.facet.KotlinFacet
internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List<String>, val classpath: List<String>)
internal fun modifyCompilerArgumentsForPlugin(
facet: KotlinFacet,
setup: AnnotationBasedCompilerPluginSetup?,
compilerPluginId: String,
pluginName: String,
annotationOptionName: String
) {
val compileInfo = facet.configuration.settings.compilerInfo
// investigate why copyBean() sometimes throws exceptions
val commonArguments = compileInfo.commonCompilerArguments ?: CommonCompilerArguments.DummyImpl()
/** See [CommonCompilerArguments.PLUGIN_OPTION_FORMAT] **/
val annotationOptions = setup?.annotationFqNames?.map { "plugin:$compilerPluginId:$annotationOptionName=$it" } ?: emptyList()
val oldPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") }
val newPluginOptions = oldPluginOptions + annotationOptions
val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) {
!it.substringAfterLast('/', missingDelimiterValue = "").matches("(kotlin-)?$pluginName-.*\\.jar".toRegex())
}
val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList())
commonArguments.pluginOptions = newPluginOptions.toTypedArray()
commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray()
compileInfo.commonCompilerArguments = commonArguments
}