Last active
May 17, 2023 10:11
-
-
Save monosoul/1b38535a2cda0c3e8994e5459a7aa3bb to your computer and use it in GitHub Desktop.
DSL for declaring Gradle modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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