Add enable / disable feature in Maven project

Part of KT-26775
This commit is contained in:
Mikhail Glukhikh
2018-09-18 16:27:16 +03:00
parent 106db57975
commit 8a7ff4cd24
9 changed files with 220 additions and 29 deletions
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.cli.common.arguments
import org.jetbrains.kotlin.config.LanguageFeature
object CliArgumentStringBuilder {
const val languagePrefix = "-XXLanguage:"
private val LanguageFeature.State.sign: String
get() = when (this) {
LanguageFeature.State.ENABLED -> "+"
LanguageFeature.State.DISABLED -> "-"
LanguageFeature.State.ENABLED_WITH_WARNING -> "+" // not supported normally
LanguageFeature.State.ENABLED_WITH_ERROR -> "-" // not supported normally
}
fun LanguageFeature.buildArgumentString(state: LanguageFeature.State): String {
return "$languagePrefix${state.sign}$name"
}
}
@@ -21,6 +21,8 @@ import com.intellij.openapi.roots.ExternalLibraryDescriptor
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.codeStyle.CodeStyleManager
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.buildArgumentString
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.languagePrefix
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator.Companion.getBuildScriptSettingsPsiFile
import org.jetbrains.kotlin.idea.util.application.runReadAction
@@ -156,14 +158,7 @@ class GroovyBuildScriptManipulator(
state: LanguageFeature.State,
forTests: Boolean
): PsiElement? {
val sign = when (state) {
LanguageFeature.State.ENABLED -> "+"
LanguageFeature.State.DISABLED -> "-"
LanguageFeature.State.ENABLED_WITH_WARNING -> "+" // not supported normally
LanguageFeature.State.ENABLED_WITH_ERROR -> "-" // not supported normally
}
val languagePrefix = "-XXLanguage:"
val featureArgumentString = "$languagePrefix$sign${feature.name}"
val featureArgumentString = feature.buildArgumentString(state)
val parameterName = "freeCompilerArgs"
return addOrReplaceKotlinTaskParameter(
scriptFile,
@@ -21,6 +21,8 @@ import com.intellij.openapi.roots.ExternalLibraryDescriptor
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.buildArgumentString
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.languagePrefix
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator.Companion.getBuildScriptSettingsPsiFile
import org.jetbrains.kotlin.idea.inspections.gradle.GradleHeuristicHelper.PRODUCTION_DEPENDENCY_STATEMENTS
@@ -311,14 +313,7 @@ class KotlinBuildScriptManipulator(
state: LanguageFeature.State,
forTests: Boolean
): PsiElement? {
val sign = when (state) {
LanguageFeature.State.ENABLED -> "+"
LanguageFeature.State.DISABLED -> "-"
LanguageFeature.State.ENABLED_WITH_WARNING -> "+" // not supported normally
LanguageFeature.State.ENABLED_WITH_ERROR -> "-" // not supported normally
}
val languagePrefix = "-XXLanguage:"
val featureArgumentString = "$languagePrefix$sign${feature.name}"
val featureArgumentString = feature.buildArgumentString(state)
val parameterName = "freeCompilerArgs"
return addOrReplaceKotlinTaskParameter(
parameterName,
@@ -39,7 +39,9 @@ import org.jetbrains.idea.maven.project.MavenProject
import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.idea.maven.utils.MavenArtifactScope
import org.jetbrains.jps.model.java.JavaSourceRootType
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.buildArgumentString
import org.jetbrains.kotlin.config.KotlinSourceRootType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.configuration.RepositoryDescription
import org.jetbrains.kotlin.idea.maven.configuration.KotlinMavenConfigurator
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
@@ -441,18 +443,6 @@ class PomFile private constructor(private val xmlFile: XmlFile, val domModel: Ma
null
)
private fun MavenDomElement.createChildTag(name: String, value: String? = null) = xmlTag?.createChildTag(name, value)
private fun XmlTag.createChildTag(name: String, value: String? = null) = createChildTag(name, namespace, value, false)!!
private tailrec fun XmlTag.deleteCascade() {
val oldParent = this.parentTag
delete()
if (oldParent != null && oldParent.subTags.isEmpty()) {
oldParent.deleteCascade()
}
}
private fun ensureElement(projectElement: XmlTag, localName: String): XmlTag {
require(localName in recommendedElementsOrder) { "You can only ensure presence or the elements from the recommendation list" }
@@ -700,3 +690,40 @@ fun PomFile.changeCoroutineConfiguration(value: String): PsiElement? {
) ?: return null
return changeConfigurationOrProperty(kotlinPlugin, "experimentalCoroutines", "kotlin.compiler.experimental.coroutines", value)
}
fun PomFile.changeFeatureConfiguration(
feature: LanguageFeature,
state: LanguageFeature.State
): PsiElement? {
val kotlinPlugin = findPlugin(
MavenId(
KotlinMavenConfigurator.GROUP_ID,
KotlinMavenConfigurator.MAVEN_PLUGIN_ID,
null
)
) ?: return null
val configurationTag = kotlinPlugin.configuration.ensureTagExists()
val argsSubTag = configurationTag.findSubTags("args").firstOrNull()
?: run {
val childTag = configurationTag.createChildTag("args")
configurationTag.add(childTag) as XmlTag
}
argsSubTag.findSubTags("arg").filter { feature.name in it.value.text }.forEach { it.deleteCascade() }
val featureArgumentString = feature.buildArgumentString(state)
val childTag = argsSubTag.createChildTag("arg", featureArgumentString)
return argsSubTag.add(childTag)
}
private fun MavenDomElement.createChildTag(name: String, value: String? = null) = xmlTag?.createChildTag(name, value)
private fun XmlTag.createChildTag(name: String, value: String? = null) = createChildTag(name, namespace, value, false)!!
private tailrec fun XmlTag.deleteCascade() {
val oldParent = this.parentTag
delete()
if (oldParent != null && oldParent.subTags.isEmpty()) {
oldParent.deleteCascade()
}
}
@@ -290,7 +290,15 @@ protected constructor(
)
return
}
// TODO: here we should make something like https://kotlinlang.org/docs/reference/using-maven.html#specifying-compiler-options
val element = changeMavenFeatureConfiguration(
module, feature, state, messageTitle
)
if (element != null) {
OpenFileDescriptor(module.project, element.containingFile.virtualFile, element.textRange.startOffset).navigate(true)
}
}
private fun changeMavenCoroutineConfiguration(
@@ -315,6 +323,25 @@ protected constructor(
return element
}
private fun changeMavenFeatureConfiguration(
module: Module,
feature: LanguageFeature,
state: LanguageFeature.State,
messageTitle: String
): PsiElement? {
val psi = KotlinMavenConfigurator.findModulePomFile(module) as? XmlFile ?: return null
val pom = PomFile.forFileOrNull(psi) ?: return null
val element = pom.changeFeatureConfiguration(feature, state)
if (element == null) {
Messages.showErrorDialog(
module.project,
"Failed to update.pom.xml. Please update the file manually.",
messageTitle
)
}
return element
}
companion object {
const val GROUP_ID = "org.jetbrains.kotlin"
const val MAVEN_PLUGIN_ID = "kotlin-maven-plugin"
@@ -74,6 +74,11 @@ class MavenUpdateConfigurationQuickFixTest : MavenImportingTestCase() {
doTest("Enable coroutine support in the current module")
}
@Test
fun testEnableInlineClasses() {
doTest("Enable inline classes support in the current module")
}
@Test
fun testAddKotlinReflect() {
doTest("Add kotlin-reflect.jar to the classpath")
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maventest</groupId>
<artifactId>maventest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>$VERSION$</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>wrong-goal</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maventest</groupId>
<artifactId>maventest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>$VERSION$</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>wrong-goal</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-XXLanguage:+InlineClasses</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1 @@
inline class My(val x: Int)