Created
August 24, 2020 04:38
-
-
Save BeokBeok/7911da370cd11320f5f2d4a212e0f981 to your computer and use it in GitHub Desktop.
Dynamic multi module
This file contains 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