Skip to content

Commit b616fc2

Browse files
feat(testkit-support): support exclusiveContent repositories.
1 parent 1174326 commit b616fc2

5 files changed

Lines changed: 98 additions & 13 deletions

File tree

testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/Repositories.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ import com.autonomousapps.kit.render.Scribe
2121
* maven(url = "https://repo.spring.io/release")
2222
* }
2323
* ```
24+
*
25+
* @see [Repository]
2426
*/
2527
public class Repositories @JvmOverloads constructor(
26-
public val repositories: MutableList<Repository> = mutableListOf(),
28+
public val repositories: MutableList<out Element> = mutableListOf(),
2729
) : Element.Block {
2830

29-
public constructor(vararg repositories: Repository) : this(repositories.toMutableList())
31+
public interface Element {
32+
// Duplicating Scribe's primary interface method
33+
public fun render(scribe: Scribe): String
34+
}
35+
36+
public constructor(vararg repositories: Element) : this(repositories.toMutableList())
3037

3138
public val isEmpty: Boolean = repositories.isEmpty()
3239

testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/Repository.kt

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,56 @@ import com.autonomousapps.kit.render.escape
2323
* // 2
2424
* maven(url = "https://repo.spring.io/release")
2525
* mavenCentral()
26+
*
27+
* // 3
28+
* exclusiveContent {
29+
* forRepository {
30+
* maven(url = "https://repo.spring.io/release")
31+
* }
32+
* filter {
33+
* includeGroup("...")
34+
* }
35+
* }
2636
* ```
2737
*/
28-
public sealed class Repository : Element.Line {
38+
public sealed class Repository : Repositories.Element {
39+
40+
public data class ExclusiveContent(
41+
private val repo: Repository,
42+
private val filters: List<String>,
43+
) : Repository(), Element.Block {
44+
45+
override val name: String = "exclusiveContent"
46+
47+
override fun render(scribe: Scribe): String {
48+
return scribe.block(this) { s ->
49+
s.block("forRepository") { s ->
50+
repo.render(s)
51+
}
52+
s.block("filter") { s ->
53+
filters.forEach { filter ->
54+
s.line { it.append(filter) }
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
public data class FlatDir(private val repoUrl: String) : Repository(), Element.Line {
62+
override fun render(scribe: Scribe): String = scribe.line { s ->
63+
s.append("flatDir { ")
64+
s.appendQuoted(repoUrl)
65+
s.append(" }")
66+
}
67+
}
2968

30-
private data class Method(private val repoCall: String) : Repository() {
69+
public data class Method(private val repoCall: String) : Repository(), Element.Line {
3170
override fun render(scribe: Scribe): String = scribe.line { s ->
3271
s.append(repoCall)
3372
}
3473
}
3574

36-
private data class Url(private val repoUrl: String) : Repository() {
75+
public data class Url(private val repoUrl: String) : Repository(), Element.Line {
3776
override fun render(scribe: Scribe): String = when (scribe.dslKind) {
3877
DslKind.GROOVY -> renderGroovy(scribe)
3978
DslKind.KOTLIN -> renderKotlin(scribe)
@@ -52,14 +91,6 @@ public sealed class Repository : Element.Line {
5291
}
5392
}
5493

55-
private data class FlatDir(private val repoUrl: String) : Repository() {
56-
override fun render(scribe: Scribe): String = scribe.line { s ->
57-
s.append("flatDir { ")
58-
s.appendQuoted(repoUrl)
59-
s.append(" }")
60-
}
61-
}
62-
6394
public companion object {
6495
@JvmField public val GOOGLE: Repository = Method("google()")
6596
@JvmField public val GRADLE_PLUGIN_PORTAL: Repository = Method("gradlePluginPortal()")

testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/render/Scribe.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,39 @@ public class Scribe @JvmOverloads constructor(
8888
return buffer.toString()
8989
}
9090

91+
/**
92+
* This method is *not* indent-aware. If you want an indent, do something like this:
93+
* ```
94+
* scribe.line { it.append(obj) }
95+
* ```
96+
* @see [line]
97+
*/
9198
public fun append(obj: Any?): Scribe {
9299
buffer.append(obj.toString())
93100
return this
94101
}
95102

103+
/**
104+
* This method is *not* indent-aware. If you want an indent, do something like this:
105+
* ```
106+
* scribe.line { it.appendQuoted(obj) }
107+
* ```
108+
* @see [line]
109+
*/
96110
public fun appendQuoted(obj: Any?): Scribe {
97111
append(quote())
98112
append(obj.toString())
99113
append(quote())
100114
return this
101115
}
102116

117+
/**
118+
* This method is *not* indent-aware. If you want an indent, do something like this:
119+
* ```
120+
* scribe.line { it.appendLine() }
121+
* ```
122+
* @see [line]
123+
*/
103124
public fun appendLine(): Scribe {
104125
buffer.appendLine()
105126
return this

testkit/gradle-testkit-support/src/test/kotlin/com/autonomousapps/kit/render/ScribeTestGroovy.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ internal class ScribeTestGroovy {
3333

3434
@Test fun `can render repositories block`() {
3535
// Given
36+
val exclusiveContentRepo = Repository.ExclusiveContent(
37+
repo = Repository.ofMaven("https://repo.foo.bar/"),
38+
filters = listOf("includeGroup('com.foo.bar')")
39+
)
3640
val repositories = Repositories(
3741
Repository.GOOGLE,
3842
Repository.MAVEN_CENTRAL,
3943
Repository.SNAPSHOTS,
44+
exclusiveContentRepo,
4045
)
4146

4247
// When
@@ -49,6 +54,14 @@ internal class ScribeTestGroovy {
4954
google()
5055
mavenCentral()
5156
maven { url = 'https://central.sonatype.com/repository/maven-snapshots/' }
57+
exclusiveContent {
58+
forRepository {
59+
maven { url = 'https://repo.foo.bar/' }
60+
}
61+
filter {
62+
includeGroup('com.foo.bar')
63+
}
64+
}
5265
}
5366
5467
""".trimIndent()

testkit/gradle-testkit-support/src/test/kotlin/com/autonomousapps/kit/render/ScribeTestKotlin.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ internal class ScribeTestKotlin {
3333

3434
@Test fun `can render repositories block`() {
3535
// Given
36+
val exclusiveContentRepo = Repository.ExclusiveContent(
37+
repo = Repository.ofMaven("https://repo.foo.bar/"),
38+
filters = listOf("includeGroup(\"com.foo.bar\")")
39+
)
3640
val repositories = Repositories(
3741
Repository.GOOGLE,
3842
Repository.MAVEN_CENTRAL,
3943
Repository.SNAPSHOTS,
44+
exclusiveContentRepo,
4045
)
4146

4247
// When
@@ -49,6 +54,14 @@ internal class ScribeTestKotlin {
4954
google()
5055
mavenCentral()
5156
maven(url = "https://central.sonatype.com/repository/maven-snapshots/")
57+
exclusiveContent {
58+
forRepository {
59+
maven(url = "https://repo.foo.bar/")
60+
}
61+
filter {
62+
includeGroup("com.foo.bar")
63+
}
64+
}
5265
}
5366
5467
""".trimIndent()

0 commit comments

Comments
 (0)