From 8c4daef9695012fb06f81dede56d10f8d6c29082 Mon Sep 17 00:00:00 2001 From: Leonid Startsev Date: Fri, 17 Nov 2017 20:28:47 +0300 Subject: [PATCH] Maven and Gradle project import handlers --- .../build.gradle.kts | 5 ++ .../src/META-INF/plugin.xml | 3 + .../idea/KotlinSerializationImportHandler.kt | 71 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/idea/KotlinSerializationImportHandler.kt diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/build.gradle.kts b/plugins/kotlin-serialization/kotlin-serialization-compiler/build.gradle.kts index 45aab860288..aebd4ea8942 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/build.gradle.kts +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/build.gradle.kts @@ -5,12 +5,17 @@ apply { plugin("kotlin") } dependencies { compileOnly(ideaSdkCoreDeps("intellij-core")) + compileOnly(ideaPluginDeps("maven", plugin = "maven")) compileOnly(project(":jps-plugin")) compileOnly(project(":compiler:plugin-api")) compileOnly(project(":compiler:frontend")) compileOnly(project(":compiler:backend")) compileOnly(project(":js:js.frontend")) compileOnly(project(":js:js.translator")) + compileOnly(project(":idea")) + compileOnly(project(":idea:idea-jps-common")) + compileOnly(project(":idea:idea-gradle")) + compileOnly(project(":idea:idea-maven")) runtime(projectRuntimeJar(":kotlin-compiler")) runtime(projectDist(":kotlin-stdlib")) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/META-INF/plugin.xml b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/META-INF/plugin.xml index c577b332d8d..3b9aa950cb8 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/META-INF/plugin.xml +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/META-INF/plugin.xml @@ -10,6 +10,9 @@ + + + diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/idea/KotlinSerializationImportHandler.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/idea/KotlinSerializationImportHandler.kt new file mode 100644 index 00000000000..9430b3ecaf7 --- /dev/null +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/idea/KotlinSerializationImportHandler.kt @@ -0,0 +1,71 @@ +/* + * 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.kotlinx.serialization.idea + +import com.intellij.openapi.externalSystem.model.DataNode +import com.intellij.openapi.externalSystem.model.project.ModuleData +import com.intellij.util.PathUtil +import org.jetbrains.idea.maven.project.MavenProject +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler +import org.jetbrains.kotlin.idea.facet.KotlinFacet +import org.jetbrains.kotlin.idea.maven.MavenProjectImportHandler +import org.jetbrains.kotlinx.serialization.idea.KotlinSerializationImportHandler.modifyCompilerArguments +import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData +import java.io.File + +class KotlinSerializationGradleImportHandler : GradleProjectImportHandler { + override fun importBySourceSet(facet: KotlinFacet, sourceSetNode: DataNode) { + modifyCompilerArguments(facet, PLUGIN_GRADLE_JAR) + } + + override fun importByModule(facet: KotlinFacet, moduleNode: DataNode) { + modifyCompilerArguments(facet, PLUGIN_GRADLE_JAR) + } + + private val PLUGIN_GRADLE_JAR = "kotlinx-gradle-serialization-plugin" +} + +class KotlinSerializationMavenImportHandler: MavenProjectImportHandler { + override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) { + modifyCompilerArguments(facet, PLUGIN_MAVEN_JAR) + } + + private val PLUGIN_MAVEN_JAR = "kotlinx-maven-serialization-plugin" +} + +internal object KotlinSerializationImportHandler { + private val PLUGIN_JPS_JAR :String + get() = File(PathUtil.getJarPathForClass(this::class.java)).absolutePath + + fun modifyCompilerArguments(facet: KotlinFacet, buildSystemPluginJar: String) { + val facetSettings = facet.configuration.settings + val commonArguments = facetSettings.compilerArguments ?: CommonCompilerArguments.DummyImpl() + + 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("$buildSystemPluginJar-.*\\.jar".toRegex()) + } + + val newPluginClasspaths = oldPluginClasspaths + PLUGIN_JPS_JAR + commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray() + facetSettings.compilerArguments = commonArguments + } +} \ No newline at end of file