From 6fb437da81ff14481519edd923507519aa0bb51c Mon Sep 17 00:00:00 2001 From: Tony Robalik Date: Mon, 19 Aug 2024 15:12:46 -0700 Subject: [PATCH] feat(testkit): add SettingsScript.Builder and withSettingsScript. --- .../com/autonomousapps/AbstractProject.groovy | 4 ++- .../projects/AbstractAndroidProject.groovy | 4 ++- .../com/autonomousapps/kit/RootProject.kt | 16 +++++++++- .../kit/gradle/SettingsScript.kt | 32 +++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy b/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy index edc7fb258..d4863e3ad 100644 --- a/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy @@ -71,7 +71,9 @@ abstract class AbstractProject extends AbstractGradleProject { return super.newGradleProjectBuilder(dslKind) .withRootProject { r -> r.gradleProperties += additionalProperties - r.settingsScript.plugins = new com.autonomousapps.kit.gradle.Plugins(plugins) + r.withSettingsScript { s -> + s.plugins(plugins) + } } } } diff --git a/src/functionalTest/groovy/com/autonomousapps/android/projects/AbstractAndroidProject.groovy b/src/functionalTest/groovy/com/autonomousapps/android/projects/AbstractAndroidProject.groovy index 472379f23..60b2b68c8 100644 --- a/src/functionalTest/groovy/com/autonomousapps/android/projects/AbstractAndroidProject.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/android/projects/AbstractAndroidProject.groovy @@ -85,7 +85,9 @@ abstract class AbstractAndroidProject extends AbstractProject { return newSettingsProjectBuilder(dslKind, withKotlin) .withRootProject { root -> root.gradleProperties += GradleProperties.minimalAndroidProperties() - root.settingsScript.buildscript = BuildscriptBlock.defaultAndroidBuildscriptBlock(agpVersion) + root.withSettingsScript { s -> + s.buildscript = BuildscriptBlock.defaultAndroidBuildscriptBlock(agpVersion) + } } } } diff --git a/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/RootProject.kt b/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/RootProject.kt index 53ba5ba74..3737fc3d5 100644 --- a/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/RootProject.kt +++ b/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/RootProject.kt @@ -35,9 +35,23 @@ public class RootProject( public var variant: String? = null public val files: MutableList = mutableListOf() - // sub-builders + /* + * sub-builders + */ + + private var settingsScriptBuilder: SettingsScript.Builder? = null private var buildScriptBuilder: BuildScript.Builder? = null + public fun withSettingsScript(block: SettingsScript.Builder.() -> Unit) { + val builder = settingsScriptBuilder ?: SettingsScript.Builder() + settingsScript = with(builder) { + block(this) + // store for later building-upon + settingsScriptBuilder = this + build() + } + } + public fun withBuildScript(block: BuildScript.Builder.() -> Unit) { val builder = buildScriptBuilder ?: defaultBuildScriptBuilder() buildScript = with(builder) { diff --git a/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/SettingsScript.kt b/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/SettingsScript.kt index 85e26533f..46a177663 100644 --- a/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/SettingsScript.kt +++ b/testkit/gradle-testkit-support/src/main/kotlin/com/autonomousapps/kit/gradle/SettingsScript.kt @@ -51,4 +51,36 @@ public class SettingsScript @JvmOverloads constructor( DslKind.GROOVY -> "include ':$subproject'" DslKind.KOTLIN -> "include(\":$subproject\")" } + + public class Builder { + public var pluginManagement: PluginManagement = PluginManagement.DEFAULT + public var buildscript: BuildscriptBlock? = null + public var plugins: Plugins = Plugins.EMPTY + public var dependencyResolutionManagement: DependencyResolutionManagement? = DependencyResolutionManagement.DEFAULT + public var rootProjectName: String = "the-project" + public var subprojects: Set = emptySet() + + /** For random stuff, as-yet unmodeled. */ + public var additions: String = "" + + public fun plugins(vararg plugins: Plugin) { + this.plugins = Plugins(plugins.toMutableList()) + } + + public fun plugins(plugins: Iterable) { + this.plugins = Plugins(plugins.toMutableList()) + } + + public fun build(): SettingsScript { + return SettingsScript( + pluginManagement = pluginManagement, + buildscript = buildscript, + plugins = plugins, + dependencyResolutionManagement = dependencyResolutionManagement, + rootProjectName = rootProjectName, + subprojects = subprojects, + additions = additions, + ) + } + } }