Skip to content

Instantly share code, notes, and snippets.

@efemoney
Last active October 23, 2022 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efemoney/9eeeefd95dbf081026e1d6a88a924827 to your computer and use it in GitHub Desktop.
Save efemoney/9eeeefd95dbf081026e1d6a88a924827 to your computer and use it in GitHub Desktop.
Gradle Settings script/code that allows you to have any (*matching*) directory structure for your gradle modules
val ProjectDescriptor.descendants: Sequence<ProjectDescriptor>
get() = children.asSequence().flatMap { sequenceOf(it) + it.descendants }
gradle.settingsEvaluated {
rootProject.descendants.forEach {
// Given path - :tooling:idea-plugin OR :tooling-idea-plugin
val path = it.path
// after splitting - [tooling, idea, plugin]
val splitPath = path.trim(':').replace(':', '-').split('-')
// path candidates - /tooling/idea/plugin, /tooling/idea-plugin, /tooling-idea-plugin
val filePathCandidates = generateSequence(splitPath) {
if (it.size <= 1) null else it.dropLast(2) + it.takeLast(2).joinToString("-")
}
// first directory that exists becomes project path
// todo: maybe first directory with a build.gradle.kts?? 🤔
filePathCandidates
.map { file(it.joinToString(File.separator)) }
.firstOrNull(File::isDirectory)
?.let { project(path).projectDir = it }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment