diff --git a/testkit/src/main/kotlin/com/autonomousapps/kit/GradleProject.kt b/testkit/src/main/kotlin/com/autonomousapps/kit/GradleProject.kt index b29c36eb1..9b21c3a8f 100644 --- a/testkit/src/main/kotlin/com/autonomousapps/kit/GradleProject.kt +++ b/testkit/src/main/kotlin/com/autonomousapps/kit/GradleProject.kt @@ -211,7 +211,7 @@ public class GradleProject( settingsScript.subprojects = subprojectNames }.build() - val includedBuilds2 = includedProjectMap.map { it.value.build() } + val includedBuilds = includedProjectMap.map { it.value.build() } val subprojects = subprojectMap.map { it.value.build() } + androidSubprojectMap.map { it.value.build() } @@ -220,7 +220,7 @@ public class GradleProject( dslKind = dslKind, buildSrc = buildSrcBuilder?.build(), rootProject = rootProject, - includedBuilds = includedBuilds2, + includedBuilds = includedBuilds, subprojects = subprojects ) } diff --git a/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/BuildScript.kt b/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/BuildScript.kt index 58f9b21c8..2d81022ab 100644 --- a/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/BuildScript.kt +++ b/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/BuildScript.kt @@ -19,6 +19,8 @@ public class BuildScript( public val additions: String = "", ) { + private val groupVersion = GroupVersion(group = group, version = version) + public fun render(scribe: Scribe): String = buildString { buildscript?.let { bs -> appendLine(scribe.use { s -> bs.render(s) }) @@ -28,24 +30,11 @@ public class BuildScript( appendLine(scribe.use { s -> plugins.render(s) }) } - // TODO push this into a new model type // These two should be grouped together for aesthetic reasons - if (group != null || version != null) { - appendLine( - scribe.use { s -> - // s.line {} appends to an internal buffer. That's why we use `=` and not `+=` below. One or both of these two - // are guaranteed to be non-null, so we'll have a non-empty string. - var value = "" - if (group != null) { - value = s.line { s.append("group = \"$group\"") } - } - if (version != null) { - value = s.line { s.append("version = \"$version\"") } - } - - value - } - ) + scribe.use { s -> + // One or both might be null + val text = groupVersion.render(s) + if (text.isNotBlank()) appendLine(text) } if (!repositories.isEmpty) { diff --git a/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/GroupVersion.kt b/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/GroupVersion.kt new file mode 100644 index 000000000..6bc7039f6 --- /dev/null +++ b/testkit/src/main/kotlin/com/autonomousapps/kit/gradle/GroupVersion.kt @@ -0,0 +1,34 @@ +package com.autonomousapps.kit.gradle + +import com.autonomousapps.kit.render.Element +import com.autonomousapps.kit.render.Scribe + +public class GroupVersion( + private val group: String? = null, + private val version: String? = null, +) : Element.Line { + + /** + * Will return an empty string if both [group] and [version] are null. It is the responsibility of the caller to + * handle this correctly. + */ + override fun render(scribe: Scribe): String { + if (group == null && version == null) return "" + + // TODO I feel like this is wanting an Element.MultiLine or something. + var addLine = false + return scribe.line { s -> + group?.let { g -> + addLine = true + s.append("group = ") + s.quoted(g) + } + version?.let { v -> + if (addLine) s.appendLine() + + s.append("version = ") + s.quoted(v) + } + } + } +} diff --git a/testkit/src/main/kotlin/com/autonomousapps/kit/render/Scribe.kt b/testkit/src/main/kotlin/com/autonomousapps/kit/render/Scribe.kt index 955f0da20..aaf7b769d 100644 --- a/testkit/src/main/kotlin/com/autonomousapps/kit/render/Scribe.kt +++ b/testkit/src/main/kotlin/com/autonomousapps/kit/render/Scribe.kt @@ -70,6 +70,10 @@ public class Scribe @JvmOverloads constructor( buffer.append(obj.toString()) } + internal fun appendLine() { + buffer.appendLine() + } + private fun indent() { buffer.append(" ".repeat(start)) } diff --git a/testkit/src/test/kotlin/com/autonomousapps/kit/render/ScribeTest.kt b/testkit/src/test/kotlin/com/autonomousapps/kit/render/ScribeTest.kt index d3cb711d1..a66348b1f 100644 --- a/testkit/src/test/kotlin/com/autonomousapps/kit/render/ScribeTest.kt +++ b/testkit/src/test/kotlin/com/autonomousapps/kit/render/ScribeTest.kt @@ -345,7 +345,7 @@ internal class ScribeTest { val text = buildScript.render(scribe) // Then - assertThat(text).isEqualTo("group = \"$group\"\n\n") + assertThat(text).isEqualTo("group = '$group'\n\n") } @Test fun `can render a build script that has a version without a group`() { @@ -359,7 +359,7 @@ internal class ScribeTest { val text = buildScript.render(scribe) // Then - assertThat(text).isEqualTo("version = \"$version\"\n\n") + assertThat(text).isEqualTo("version = '$version'\n\n") } @Test fun `can render a build script`() { @@ -413,8 +413,8 @@ internal class ScribeTest { id 'groovy' } - group = "$group" - version = "$version" + group = '$group' + version = '$version' android { namespace 'ankh.morpork'