Add enable / disable feature in Gradle (Kotlin DSL) project
Part of KT-26775
This commit is contained in:
+46
-12
@@ -114,7 +114,7 @@ class KotlinBuildScriptManipulator(
|
|||||||
state: LanguageFeature.State,
|
state: LanguageFeature.State,
|
||||||
forTests: Boolean
|
forTests: Boolean
|
||||||
): PsiElement? =
|
): PsiElement? =
|
||||||
scriptFile.changeLanguageFeatureConfiguration(feature, state)
|
scriptFile.changeLanguageFeatureConfiguration(feature, state, forTests)
|
||||||
|
|
||||||
override fun changeLanguageVersion(version: String, forTests: Boolean): PsiElement? =
|
override fun changeLanguageVersion(version: String, forTests: Boolean): PsiElement? =
|
||||||
scriptFile.changeKotlinTaskParameter("languageVersion", version, forTests)
|
scriptFile.changeKotlinTaskParameter("languageVersion", version, forTests)
|
||||||
@@ -308,29 +308,63 @@ class KotlinBuildScriptManipulator(
|
|||||||
|
|
||||||
private fun KtFile.changeLanguageFeatureConfiguration(
|
private fun KtFile.changeLanguageFeatureConfiguration(
|
||||||
feature: LanguageFeature,
|
feature: LanguageFeature,
|
||||||
state: LanguageFeature.State
|
state: LanguageFeature.State,
|
||||||
|
forTests: Boolean
|
||||||
): PsiElement? {
|
): PsiElement? {
|
||||||
// TODO: Kotlin DSL
|
val sign = when (state) {
|
||||||
return null
|
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 parameterName = "freeCompilerArgs"
|
||||||
|
return addOrReplaceKotlinTaskParameter(
|
||||||
|
parameterName,
|
||||||
|
"listOf(\"$featureArgumentString\")",
|
||||||
|
forTests
|
||||||
|
) {
|
||||||
|
val existingFeatureIndex = text.indexOf(feature.name)
|
||||||
|
val languagePrefixIndex = text.lastIndexOf(languagePrefix, existingFeatureIndex)
|
||||||
|
val newText = if (languagePrefixIndex != -1) {
|
||||||
|
text.substring(0, languagePrefixIndex) + featureArgumentString + text.substring(existingFeatureIndex + feature.name.length)
|
||||||
|
} else {
|
||||||
|
val splitText = text.split(")")
|
||||||
|
if (splitText.size != 2) {
|
||||||
|
"$parameterName = listOf(\"$featureArgumentString\")"
|
||||||
|
} else {
|
||||||
|
splitText[0] + ", \"$featureArgumentString\")" + splitText[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
replace(psiFactory.createExpression(newText))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtFile.changeKotlinTaskParameter(parameterName: String, parameterValue: String, forTests: Boolean): PsiElement? {
|
private fun KtFile.addOrReplaceKotlinTaskParameter(
|
||||||
val snippet = "$parameterName = \"$parameterValue\""
|
parameterName: String,
|
||||||
|
defaultValue: String,
|
||||||
|
forTests: Boolean,
|
||||||
|
replaceIt: KtExpression.() -> PsiElement
|
||||||
|
): PsiElement? {
|
||||||
val taskName = if (forTests) "compileTestKotlin" else "compileKotlin"
|
val taskName = if (forTests) "compileTestKotlin" else "compileKotlin"
|
||||||
val optionsBlock = findScriptInitializer("$taskName.kotlinOptions")?.getBlock()
|
val optionsBlock = findScriptInitializer("$taskName.kotlinOptions")?.getBlock()
|
||||||
return if (optionsBlock != null) {
|
return if (optionsBlock != null) {
|
||||||
val assignment = optionsBlock.statements.find {
|
val assignment = optionsBlock.statements.find {
|
||||||
(it as? KtBinaryExpression)?.left?.text == parameterName
|
(it as? KtBinaryExpression)?.left?.text == parameterName
|
||||||
}
|
}
|
||||||
if (assignment != null) {
|
assignment?.replaceIt() ?: optionsBlock.addExpressionIfMissing("$parameterName = $defaultValue")
|
||||||
assignment.replace(psiFactory.createExpression(snippet))
|
|
||||||
} else {
|
|
||||||
optionsBlock.addExpressionIfMissing(snippet)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
addImportIfMissing("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")
|
addImportIfMissing("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")
|
||||||
script?.blockExpression?.addDeclarationIfMissing("val $taskName: KotlinCompile by tasks")
|
script?.blockExpression?.addDeclarationIfMissing("val $taskName: KotlinCompile by tasks")
|
||||||
addTopLevelBlock("$taskName.kotlinOptions")?.addExpressionIfMissing(snippet)
|
addTopLevelBlock("$taskName.kotlinOptions")?.addExpressionIfMissing("$parameterName = $defaultValue")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun KtFile.changeKotlinTaskParameter(parameterName: String, parameterValue: String, forTests: Boolean): PsiElement? {
|
||||||
|
return addOrReplaceKotlinTaskParameter(parameterName, "\"$parameterValue\"", forTests) {
|
||||||
|
replace(psiFactory.createExpression("$parameterName = \"$parameterValue\""))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+45
@@ -611,6 +611,51 @@ class GradleConfiguratorTest : GradleImportingTestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testChangeFeatureSupportGSK() {
|
||||||
|
val files = importProjectFromTestData()
|
||||||
|
|
||||||
|
runInEdtAndWait {
|
||||||
|
myTestFixture.project.executeWriteCommand("") {
|
||||||
|
KotlinWithGradleConfigurator.changeFeatureConfiguration(
|
||||||
|
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.DISABLED, false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkFiles(files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDisableFeatureSupportGSK() {
|
||||||
|
val files = importProjectFromTestData()
|
||||||
|
|
||||||
|
runInEdtAndWait {
|
||||||
|
myTestFixture.project.executeWriteCommand("") {
|
||||||
|
KotlinWithGradleConfigurator.changeFeatureConfiguration(
|
||||||
|
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.DISABLED, false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkFiles(files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEnableFeatureSupportGSK() {
|
||||||
|
val files = importProjectFromTestData()
|
||||||
|
|
||||||
|
runInEdtAndWait {
|
||||||
|
myTestFixture.project.executeWriteCommand("") {
|
||||||
|
KotlinWithGradleConfigurator.changeFeatureConfiguration(
|
||||||
|
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.ENABLED, false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkFiles(files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun importProjectFromTestData(): List<VirtualFile> {
|
private fun importProjectFromTestData(): List<VirtualFile> {
|
||||||
val files = configureByFiles()
|
val files = configureByFiles()
|
||||||
importProject()
|
importProject()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
|
val compileKotlin: KotlinCompile by tasks
|
||||||
|
compileKotlin.kotlinOptions {
|
||||||
|
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
|
val compileKotlin: KotlinCompile by tasks
|
||||||
|
compileKotlin.kotlinOptions {
|
||||||
|
freeCompilerArgs = listOf("-XXLanguage:-InlineClasses")
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
|
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
|
|
||||||
|
val compileKotlin: KotlinCompile by tasks
|
||||||
|
compileKotlin.kotlinOptions {
|
||||||
|
freeCompilerArgs = listOf("-XXLanguage:-InlineClasses")
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.70"
|
||||||
|
}
|
||||||
|
val compileKotlin: KotlinCompile by tasks
|
||||||
|
compileKotlin.kotlinOptions {
|
||||||
|
freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user