Skip to content

Instantly share code, notes, and snippets.

@monosoul
Last active May 17, 2023 10:11
Show Gist options
  • Save monosoul/1b38535a2cda0c3e8994e5459a7aa3bb to your computer and use it in GitHub Desktop.
Save monosoul/1b38535a2cda0c3e8994e5459a7aa3bb to your computer and use it in GitHub Desktop.
DSL for declaring Gradle modules
// usage example
includeTree {
module("app") {
dir("domain") {
module("domain-api")
module("domain-impl")
}
dir("persistence) {
module("persistence-api")
module("persistence-impl")
}
module("web")
}
}
// DSL itself
data class IncludeTree(
private val path: String,
private val parentProject: String
) {
fun dir(path: String, block: IncludeTree.() -> Unit) {
val nestedPath = "${this.path}/$path"
includeTree(nestedPath, parentProject, block)
}
fun module(name: String, block: IncludeTree.() -> Unit = {}) {
val projectName = "$parentProject:$name"
val projectDir = "$path/$name"
include(projectName)
project(projectName).also {
it.projectDir = file(projectDir)
it.buildFile.takeUnless(File::exists)
?.also { buildFile ->
buildFile.parentFile.mkdirs()
file("${buildFile.absolutePath}.kts").createNewFile()
}
}
includeTree(projectDir, projectName, block)
}
}
fun includeTree(path: String = ".", parentProject: String = "", block: IncludeTree.() -> Unit) {
IncludeTree(path, parentProject).block()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment