Created
August 20, 2020 09:03
-
-
Save pavlospt/18572058f1a885e160387dfec6ee7b7e to your computer and use it in GitHub Desktop.
Automatically discover 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
/** | |
* Dynamically discover and add modules in our project | |
* */ | |
val moduleBuildFileName = "build.gradle.kts" | |
// A pattern to match the prefix of our modules | |
val eligibleModuleNamePattern = "a_pattern_to_match_our_module_names".toRegex() // (e.g.: (app|android-|kotlin-).*) | |
// Filter directories that indicate modules | |
val moduleFilter: FileFilter = FileFilter { pathname: File -> | |
// Early exit if we are not introspecting a directory | |
if (!pathname.isDirectory) { | |
return@FileFilter false | |
} | |
// Keep a directory if it contains a build file | |
val isGradleModuleFilter = FileFilter { file: File -> | |
file.name == moduleBuildFileName | |
} | |
// Early exit if directory has no build files | |
val gradleModuleFiles = pathname | |
.listFiles(isGradleModuleFilter) | |
.orEmpty() | |
// Early exit if the directory is empty | |
if (gradleModuleFiles.isEmpty()) { | |
return@FileFilter false | |
} | |
// Check if the directory name matches the module name pattern | |
return@FileFilter pathname.name.matches(eligibleModuleNamePattern) | |
} | |
settingsDir | |
.listFiles(moduleFilter) | |
.orEmpty() | |
.forEach { dir -> include(":${dir.name}") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment