Skip to content

Instantly share code, notes, and snippets.

@gerin98
Last active August 19, 2024 16:10
Show Gist options
  • Save gerin98/fcf59e9e52cf5cd4ab9d18f35f3411a5 to your computer and use it in GitHub Desktop.
Save gerin98/fcf59e9e52cf5cd4ab9d18f35f3411a5 to your computer and use it in GitHub Desktop.
[Blogpost] Modules changed component
class ModulesChangedComponent(project: Project) {
private val rootProject = project.rootProject
private val rootDirLength = rootProject.projectDir.toString().length + 1
private fun Project.relativePath(): String = this.projectDir.toString().drop(rootDirLength)
private fun Project.isTopLevelModule(): Boolean = this.projectDir.listFiles()!!.none { it.name == "build.gradle" }
fun affectedModules(filesChanged: Set<String>): Set<String> {
val modules = parseModules()
return modulesChanged(filesChanged, modules)
}
private fun parseModules(): Set<String> {
val modules = mutableSetOf("app")
rootProject.subprojects.forEach {
if (!it.isTopLevelModule()) {
modules.add(it.relativePath())
}
}
return modules
}
private fun modulesChanged(filePaths: Set<String>, filePathPrefixes: Set<String>): Set<String> {
return filePathPrefixes.mapNotNull { prefix ->
if (filePaths.any { it.startsWith(prefix) }) {
prefix
} else {
null
}
}.toSet()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment