BuildScriptManipulator: implement new syntax for enable experimental features in MPP

#KT-28295 Fixed
This commit is contained in:
Dmitry Gridin
2019-07-15 18:34:43 +03:00
parent 8887031962
commit cb432acfb7
13 changed files with 120 additions and 83 deletions
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.idea.configuration
@@ -25,6 +14,7 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.buildArgumentString
import org.jetbrains.kotlin.cli.common.arguments.CliArgumentStringBuilder.replaceLanguageFeature
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.idea.configuration.GroovyBuildScriptManipulator.Companion.getBlockOrCreate
import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator.Companion.getBuildScriptSettingsPsiFile
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.util.module
@@ -165,6 +155,14 @@ class GroovyBuildScriptManipulator(
state: LanguageFeature.State,
forTests: Boolean
): PsiElement? {
if (usesNewMultiplatform()) {
val kotlinBlock = scriptFile.getBlockOrCreate("kotlin")
val sourceSetsBlock = kotlinBlock.getBlockOrCreate("sourceSets")
val allBlock = sourceSetsBlock.getBlockOrCreate("all")
allBlock.addLastExpressionInBlockIfNeeded("languageSettings.enableLanguageFeature(\"${feature.name}\")")
return allBlock.statements.lastOrNull()
}
val featureArgumentString = feature.buildArgumentString(state)
val parameterName = "freeCompilerArgs"
return addOrReplaceKotlinTaskParameter(
@@ -255,7 +253,7 @@ class GroovyBuildScriptManipulator(
private fun usesNewMultiplatform(): Boolean {
val fileText = runReadAction { scriptFile.text }
return fileText.contains("kotlin-multiplatform")
return fileText.contains("multiplatform")
}
private fun GrClosableBlock.addParameterAssignment(
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.idea.configuration
@@ -310,11 +299,23 @@ class KotlinBuildScriptManipulator(
}
}
private fun usesNewMultiplatform(): Boolean {
val fileText = runReadAction { scriptFile.text }
return fileText.contains("multiplatform")
}
private fun KtFile.changeLanguageFeatureConfiguration(
feature: LanguageFeature,
state: LanguageFeature.State,
forTests: Boolean
): PsiElement? {
if (usesNewMultiplatform()) {
return findOrCreateScriptInitializer("kotlin")
?.findOrCreateBlock("sourceSets")
?.findOrCreateBlock("all")
?.addExpressionIfMissing("languageSettings.enableLanguageFeature(\"${feature.name}\")")
}
val featureArgumentString = feature.buildArgumentString(state)
val parameterName = "freeCompilerArgs"
return addOrReplaceKotlinTaskParameter(
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -13,6 +13,22 @@ import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions
import org.junit.Test
class GradleConfiguratorPlatformSpecificTest : GradleImportingTestCase() {
@TargetVersions("4.7+")
@Test
fun testDisableFeatureSupportMultiplatform() {
val files = importProjectFromTestData()
// now it's impossible to disable feature in multiplatform
runInEdtAndWait {
myTestFixture.project.executeWriteCommand("") {
KotlinWithGradleConfigurator.changeFeatureConfiguration(
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.DISABLED, false
)
}
checkFiles(files)
}
}
@TargetVersions("4.7+")
@Test
fun testEnableFeatureSupportMultiplatform() {
@@ -31,7 +47,7 @@ class GradleConfiguratorPlatformSpecificTest : GradleImportingTestCase() {
@TargetVersions("4.7+")
@Test
fun testEnableFeatureSupportMultiplatformWithDots() {
fun testEnableFeatureSupportMultiplatform2() {
val files = importProjectFromTestData()
runInEdtAndWait {
@@ -61,6 +77,22 @@ class GradleConfiguratorPlatformSpecificTest : GradleImportingTestCase() {
}
}
@TargetVersions("4.7+")
@Test
fun testEnableFeatureSupportMultiplatformKts() {
val files = importProjectFromTestData()
runInEdtAndWait {
myTestFixture.project.executeWriteCommand("") {
KotlinWithGradleConfigurator.changeFeatureConfiguration(
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.ENABLED, false
)
}
checkFiles(files)
}
}
override fun testDataDirName(): String {
return "configurator"
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -584,22 +584,6 @@ class GradleConfiguratorTest : GradleImportingTestCase() {
}
}
@TargetVersions("4.7+")
@Test
fun testDisableFeatureSupportMultiplatform() {
val files = importProjectFromTestData()
runInEdtAndWait {
myTestFixture.project.executeWriteCommand("") {
KotlinWithGradleConfigurator.changeFeatureConfiguration(
myTestFixture.module, LanguageFeature.InlineClasses, LanguageFeature.State.DISABLED, false
)
}
checkFiles(files)
}
}
@Test
fun testEnableFeatureSupport() {
val files = importProjectFromTestData()