Skip to content

Commit

Permalink
feat(testkit): allow adding custom content to android blocks (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
evant authored Dec 14, 2024
1 parent 2ffa7c3 commit be6e969
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.autonomousapps.kit.gradle.android
import com.autonomousapps.kit.GradleProject.DslKind
import com.autonomousapps.kit.render.Element
import com.autonomousapps.kit.render.Scribe
import org.intellij.lang.annotations.Language

/**
* The `android` block, for use by projects build with the Android Gradle Plugin.
Expand All @@ -21,6 +22,9 @@ public class AndroidBlock @JvmOverloads constructor(
public var defaultConfig: DefaultConfig = DefaultConfig.DEFAULT_APP,
public var compileOptions: CompileOptions = CompileOptions.DEFAULT,
public var kotlinOptions: KotlinOptions? = null,
public var additions: String = "",
private val usesGroovy: Boolean = false,
private val usesKotlin: Boolean = false,
) : Element.Block {

override val name: String = "android"
Expand All @@ -45,6 +49,13 @@ public class AndroidBlock @JvmOverloads constructor(
defaultConfig.render(s)
compileOptions.render(s)
kotlinOptions?.render(s)

if (additions.isNotBlank()) {
if (usesKotlin) {
error("You called withGroovy() but you're using Kotlin DSL")
}
s.line { it.append(additions) }
}
}

private fun renderKotlin(scribe: Scribe): String = scribe.block(this) { s ->
Expand All @@ -62,6 +73,13 @@ public class AndroidBlock @JvmOverloads constructor(
defaultConfig.render(s)
compileOptions.render(s)
kotlinOptions?.render(s)

if (additions.isNotBlank()) {
if (usesGroovy) {
error("You called withKotlin() but you're using Groovy DSL")
}
s.line { it.append(additions) }
}
}

public class Builder {
Expand All @@ -71,13 +89,28 @@ public class AndroidBlock @JvmOverloads constructor(
public var compileOptions: CompileOptions = CompileOptions.DEFAULT
public var kotlinOptions: KotlinOptions? = null

public var additions: String = ""
private var usesGroovy = false
private var usesKotlin = false

public fun withGroovy(@Language("Groovy") script: String) {
additions = script.trimIndent()
usesGroovy = true
}

public fun withKotlin(@Language("kt") script: String) {
additions = script.trimIndent()
usesKotlin = true
}

public fun build(): AndroidBlock {
return AndroidBlock(
namespace = namespace,
compileSdkVersion = compileSdkVersion,
defaultConfig = defaultConfig,
compileOptions = compileOptions,
kotlinOptions = kotlinOptions,
additions = additions,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,41 @@ internal class ScribeTestGroovy {
""".trimIndent()
)
}

@Test fun `can render custom android content`() {
// Given
val buildScript = BuildScript(
android = AndroidBlock.Builder().apply {
withGroovy("custom { config = true }")
}.build(),
)

// When
val text = buildScript.render(scribe)

// Then
assertThat(text).isEqualTo(
"""
android {
compileSdkVersion 34
defaultConfig {
applicationId 'com.example'
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName '1.0'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
custom { config = true }
}
""".trimIndent()
)
}
}

@Nested inner class PluginsTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,42 @@ internal class ScribeTestKotlin {
""".trimIndent()
)
}

@Test fun `can render custom android content`() {
// Given
val buildScript = BuildScript(
android = AndroidBlock.Builder().apply {
withKotlin("custom { config = true }")
}.build(),
usesKotlin = true,
)

// When
val text = buildScript.render(scribe)

// Then
assertThat(text).isEqualTo(
"""
android {
compileSdk = 34
defaultConfig {
applicationId = "com.example"
minSdk = 21
targetSdk = 29
versionCode = 1
versionName = "1.0"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
custom { config = true }
}
""".trimIndent()
)
}
}

@Nested inner class PluginsTest {
Expand Down

0 comments on commit be6e969

Please sign in to comment.