From aa156c08c6910306b73afd8a2446ba9237bf08dd Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Sat, 2 Apr 2016 12:46:20 +0300 Subject: [PATCH] Maven: versions inspection: kotlin plugin and library should have the same version --- .../configuration/SameVersionInspection.kt | 57 +++++++++++++++++++ .../KotlinMavenInspectionTestGenerated.java | 6 ++ .../sameVersionPluginLibrary.xml | 37 ++++++++++++ idea/src/META-INF/maven.xml | 8 +++ 4 files changed, 108 insertions(+) create mode 100644 idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/SameVersionInspection.kt create mode 100644 idea/idea-maven/testData/maven-inspections/sameVersionPluginLibrary.xml diff --git a/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/SameVersionInspection.kt b/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/SameVersionInspection.kt new file mode 100644 index 00000000000..61d674e135c --- /dev/null +++ b/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/SameVersionInspection.kt @@ -0,0 +1,57 @@ +/* + * 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.idea.configuration + +import com.intellij.lang.annotation.HighlightSeverity +import com.intellij.util.xml.DomFileElement +import com.intellij.util.xml.highlighting.DomElementAnnotationHolder +import com.intellij.util.xml.highlighting.DomElementsInspection +import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel +import org.jetbrains.idea.maven.model.MavenId +import org.jetbrains.idea.maven.project.MavenProjectsManager + +class SameVersionInspection : DomElementsInspection(MavenDomProjectModel::class.java) { + override fun checkFileElement(domFileElement: DomFileElement?, holder: DomElementAnnotationHolder?) { + if (domFileElement == null || holder == null) { + return + } + + val file = domFileElement.file + val module = domFileElement.module ?: return + val manager = MavenProjectsManager.getInstance(module.project) ?: return + val project = manager.findProject(module) ?: return + + val stdlibVersion = project.findDependencies(KotlinMavenConfigurator.GROUP_ID, KotlinJavaMavenConfigurator.STD_LIB_ID).map { it.version }.distinct() + val pluginVersion = project.findPlugin(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID)?.version + + if (pluginVersion == null || stdlibVersion.isEmpty() || stdlibVersion.singleOrNull() == pluginVersion) { + return + } + + val pomFile = PomFile(file) + pomFile.findKotlinPlugins().filter { it.version.stringValue != stdlibVersion.singleOrNull() }.forEach { plugin -> + holder.createProblem(plugin.version, HighlightSeverity.WARNING, + "Plugin version (${plugin.version}) is not the same as library version (${stdlibVersion.joinToString(",", "", "")})") + } + + pomFile.findDependencies(MavenId(KotlinJavaMavenConfigurator.GROUP_ID, KotlinJavaMavenConfigurator.STD_LIB_ID, null)) + .filter { it.version.stringValue != pluginVersion } + .forEach { dependency -> + holder.createProblem(dependency.version, HighlightSeverity.WARNING, "Plugin version ($pluginVersion) is not the same as library version (${dependency.version})") + } + } +} \ No newline at end of file diff --git a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java index 34cc70f0c49..1aee60e619d 100644 --- a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java +++ b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java @@ -65,6 +65,12 @@ public class KotlinMavenInspectionTestGenerated extends AbstractKotlinMavenInspe doTest(fileName); } + @TestMetadata("sameVersionPluginLibrary.xml") + public void testSameVersionPluginLibrary() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-maven/testData/maven-inspections/sameVersionPluginLibrary.xml"); + doTest(fileName); + } + @TestMetadata("wrongJsExecution.xml") public void testWrongJsExecution() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-maven/testData/maven-inspections/wrongJsExecution.xml"); diff --git a/idea/idea-maven/testData/maven-inspections/sameVersionPluginLibrary.xml b/idea/idea-maven/testData/maven-inspections/sameVersionPluginLibrary.xml new file mode 100644 index 00000000000..ad073f1e806 --- /dev/null +++ b/idea/idea-maven/testData/maven-inspections/sameVersionPluginLibrary.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + org.jetbrains.kotlin.test + configure-maven-test + 1.0-SNAPSHOT + + + 1.0.1 + 1.0.1-2 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version.2} + + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version.1} + + + + + + + + \ No newline at end of file diff --git a/idea/src/META-INF/maven.xml b/idea/src/META-INF/maven.xml index 144c120a3cd..333174fa1d7 100644 --- a/idea/src/META-INF/maven.xml +++ b/idea/src/META-INF/maven.xml @@ -33,6 +33,14 @@ hasStaticDescription="true" level="WARNING" /> + + org.jetbrains.kotlin.idea.configuration.MavenPluginSourcesMoveToBuild Kotlin