Skip to content

Commit

Permalink
Modeled Group + Version in BuildScript.
Browse files Browse the repository at this point in the history
  • Loading branch information
autonomousapps committed Oct 30, 2023
1 parent 9a5e87f commit b86c125
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() }

Expand All @@ -220,7 +220,7 @@ public class GradleProject(
dslKind = dslKind,
buildSrc = buildSrcBuilder?.build(),
rootProject = rootProject,
includedBuilds = includedBuilds2,
includedBuilds = includedBuilds,
subprojects = subprojects
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand All @@ -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`() {
Expand Down Expand Up @@ -413,8 +413,8 @@ internal class ScribeTest {
id 'groovy'
}
group = "$group"
version = "$version"
group = '$group'
version = '$version'
android {
namespace 'ankh.morpork'
Expand Down

0 comments on commit b86c125

Please sign in to comment.