Skip to content

Instantly share code, notes, and snippets.

@pavlospt
Created August 20, 2020 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pavlospt/18572058f1a885e160387dfec6ee7b7e to your computer and use it in GitHub Desktop.
Save pavlospt/18572058f1a885e160387dfec6ee7b7e to your computer and use it in GitHub Desktop.
Automatically discover modules
/**
* 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