Skip to content

Instantly share code, notes, and snippets.

@bgogetap
Last active November 14, 2022 22:09
Show Gist options
  • Save bgogetap/c1610fbb4f83e2953b541b838c47e62a to your computer and use it in GitHub Desktop.
Save bgogetap/c1610fbb4f83e2953b541b838c47e62a to your computer and use it in GitHub Desktop.
Snippet to add to settings.gradle.kts to auto-add any gradle module (no more explicit Include(":mymodule") )
val modules = mutableSetOf<String>()
val ignoredDirectories = setOf("build", "buildSrc", "gradle", ".idea")
fun fillModules(files: List<File>, targetPath: String) {
files.forEach { file ->
if (file.isFile) {
if (file.name == "build.gradle" || file.name == "build.gradle.kts") {
modules.add(targetPath)
}
} else {
if (ignoredDirectories.contains(file.name)) {
return@forEach
}
val updatedParentPath = "$targetPath:${file.name}"
val childFiles =
file.listFiles()?.filter { it.isDirectory || it.name.contains(".gradle") }
if (childFiles != null) {
fillModules(childFiles, updatedParentPath)
}
}
}
}
val projectDirectories = checkNotNull(
rootProject.projectDir.listFiles()
?.filter { it.isDirectory }
) { "No directories in project" }
fillModules(
files = projectDirectories, targetPath = ""
)
modules.forEach { include(it) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment