Configuration: Put 'buildscript' block before 'plugins' in build.gradle
#KT-23588 Fixed
This commit is contained in:
+15
-6
@@ -54,9 +54,9 @@ class GroovyBuildScriptManipulator(private val groovyScript: GroovyFile) : Gradl
|
||||
if (applyStatement != null) {
|
||||
groovyScript.addAfter(apply, applyStatement)
|
||||
} else {
|
||||
val buildScriptBlock = groovyScript.getBlockByName("buildscript")
|
||||
if (buildScriptBlock != null) {
|
||||
groovyScript.addAfter(apply, buildScriptBlock.parent)
|
||||
val anchorBlock = groovyScript.getBlockByName("plugins") ?: groovyScript.getBlockByName("buildscript")
|
||||
if (anchorBlock != null) {
|
||||
groovyScript.addAfter(apply, anchorBlock.parent)
|
||||
} else {
|
||||
groovyScript.addAfter(apply, groovyScript.statements.lastOrNull() ?: groovyScript.firstChild)
|
||||
}
|
||||
@@ -219,7 +219,11 @@ class GroovyBuildScriptManipulator(private val groovyScript: GroovyFile) : Gradl
|
||||
|
||||
private fun GrStatementOwner.getDependenciesBlock(): GrClosableBlock = getBlockOrCreate("dependencies")
|
||||
|
||||
private fun GrStatementOwner.getBuildScriptBlock() = getBlockOrCreate("buildscript")
|
||||
private fun GrStatementOwner.getBuildScriptBlock() = getBlockOrCreate("buildscript") { newBlock ->
|
||||
val pluginsBlock = getBlockByName("plugins") ?: return@getBlockOrCreate false
|
||||
addBefore(newBlock, pluginsBlock.parent)
|
||||
true
|
||||
}
|
||||
|
||||
private fun GrStatementOwner.getBuildScriptRepositoriesBlock(): GrClosableBlock =
|
||||
getBuildScriptBlock().getBlockOrCreate("repositories")
|
||||
@@ -227,12 +231,17 @@ class GroovyBuildScriptManipulator(private val groovyScript: GroovyFile) : Gradl
|
||||
private fun GrStatementOwner.getBuildScriptDependenciesBlock(): GrClosableBlock =
|
||||
getBuildScriptBlock().getBlockOrCreate("dependencies")
|
||||
|
||||
private fun GrStatementOwner.getBlockOrCreate(name: String): GrClosableBlock {
|
||||
private fun GrStatementOwner.getBlockOrCreate(
|
||||
name: String,
|
||||
customInsert: GrStatementOwner.(newBlock: PsiElement) -> Boolean = { false }
|
||||
): GrClosableBlock {
|
||||
var block = getBlockByName(name)
|
||||
if (block == null) {
|
||||
val factory = GroovyPsiElementFactory.getInstance(project)
|
||||
val newBlock = factory.createExpressionFromText("$name{\n}\n")
|
||||
addAfter(newBlock, statements.lastOrNull() ?: firstChild)
|
||||
if (!customInsert(newBlock)) {
|
||||
addAfter(newBlock, statements.lastOrNull() ?: firstChild)
|
||||
}
|
||||
block = getBlockByName(name)!!
|
||||
}
|
||||
return block
|
||||
|
||||
+70
@@ -111,6 +111,76 @@ class GradleConfiguratorTest : GradleImportingTestCase() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConfigureKotlinWithPluginsBlock() {
|
||||
createProjectSubFile("settings.gradle", "include ':app'")
|
||||
val file = createProjectSubFile(
|
||||
"app/build.gradle", """
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group 'testgroup'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
importProject()
|
||||
|
||||
runInEdtAndWait {
|
||||
runWriteAction {
|
||||
val module = ModuleManager.getInstance(myProject).findModuleByName("app")!!
|
||||
val configurator = findGradleModuleConfigurator()
|
||||
val collector = createConfigureKotlinNotificationCollector(myProject)
|
||||
configurator.configureWithVersion(myProject, listOf(module), "1.0.6", collector)
|
||||
|
||||
FileDocumentManager.getInstance().saveAllDocuments()
|
||||
val content = LoadTextUtil.loadText(file).toString()
|
||||
assertEquals(
|
||||
"""
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.0.6'
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${"$"}kotlin_version"
|
||||
}
|
||||
}
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
apply plugin: 'kotlin'
|
||||
|
||||
group 'testgroup'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:${"$"}kotlin_version"
|
||||
}
|
||||
""".trimIndent(), content
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun findGradleModuleConfigurator() = Extensions.findExtension(
|
||||
KotlinProjectConfigurator.EP_NAME,
|
||||
KotlinGradleModuleConfigurator::class.java
|
||||
|
||||
Reference in New Issue
Block a user