Skip to content

Commit

Permalink
Automatically detect modules in settings.gradle (#253)
Browse files Browse the repository at this point in the history
* Simplify settings.gradle

* Convert settings.gradle to kts
  • Loading branch information
fornewid authored Dec 9, 2023
1 parent 42f11cf commit 626837c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 58 deletions.
58 changes: 0 additions & 58 deletions settings.gradle

This file was deleted.

93 changes: 93 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
pluginManagement {
includeBuild("build-logic")
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google {
content {
includeGroupByRegex("com.android.*")
includeGroupByRegex("androidx.*")
includeGroupByRegex("com.google.*")
}
}
maven {
url = uri("https://jitpack.io")
content {
includeGroup("com.github.fornewid.metronome")
}
}
maven {
url = uri("https://devrepo.kakao.com/nexus/content/groups/public/")
content {
includeGroup("com.kakao.sdk")
}
}
maven {
url = uri("https://naver.jfrog.io/artifactory/maven/")
content {
includeGroup("com.naver.maps")
}
}

mavenCentral()
}
}

// Automatically detect modules.
ModuleDetector.modules(rootDir).forEach { module ->
include(module)
}

private object ModuleDetector {

fun modules(rootDir: File) : List<String> {
return rootDir.listDirs().flatMap { dir ->
findModules(parent = "", dir = dir)
}
}

private fun findModules(parent: String, dir: File): List<String> {
if (dir.isDirectory.not() || dir.isProject()) {
return emptyList()
}

val current: String = parent + ":" + dir.name
return if (dir.isModule()) {
println("include '$current'")
listOf(current)
} else {
dir.listDirs().flatMap { subDir ->
findModules(parent = current, dir = subDir)
}
}
}

private fun File.isProject(): Boolean {
if (isDirectory.not()) {
return false
}
return listFiles().orEmpty().any {
it.isFile && (it.name == "settings.gradle" || it.name == "settings.gradle.kts")
}
}

private fun File.isModule(): Boolean {
if (isDirectory.not()) {
return false
}
return listFiles().orEmpty().any {
it.isFile && (it.name == "build.gradle" || it.name == "build.gradle.kts")
}
}

private fun File.listDirs(): List<File> {
return listFiles().orEmpty().filter { it.isDirectory }
}
}

0 comments on commit 626837c

Please sign in to comment.