Created
August 19, 2021 15:34
-
-
Save crgarridos/f5f686a206a3e12a0c9f33c17fbccc78 to your computer and use it in GitHub Desktop.
Auto discover 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
includeModulesDirs( | |
"features", | |
"usecases", | |
"repositories", | |
"libraries", | |
"templates", | |
) | |
/** | |
* Look into [dirs] for existing modules. | |
* | |
* Use [excludedModules] to filter modules that you want to ignore, | |
* each item being a full module qualifier (example ":usecases:toto"). | |
* | |
*/ | |
fun includeModulesDirs(vararg dirs: String, excludedModules: Array<String> = emptyArray()) { | |
val foundModules = dirs | |
.map(::File) | |
.flatMap(::findGradleModules) | |
.map { it.path.replace(File.separatorChar, ':') } | |
.filterNot { it in excludedModules } | |
.sorted() | |
include(*foundModules.toTypedArray()) | |
} | |
fun findGradleModules(dir: File): List<File> { | |
return dir.listFiles { potentialModule -> | |
potentialModule.isDirectory && | |
potentialModule.isNotBlacklisted && | |
potentialModule.containsGradleScript() | |
}.orEmpty().flatMap { | |
findGradleModules(it) + it | |
}.toList() | |
} | |
val File.isNotBlacklisted: Boolean | |
get() = name !in arrayOf("build", "src") | |
fun File.containsGradleScript(): Boolean { | |
val gradleScripts = listFiles { file -> file.isGradleScript }.orEmpty() | |
if (gradleScripts.size > 1) { | |
throw kotlin.IllegalStateException("More than one .gradle[.kts] files found in $this") | |
} | |
return gradleScripts.isNotEmpty() | |
} | |
val File.isGradleScript: Boolean get() = name.endsWith(".gradle") || name.endsWith(".gradle.kts") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment