selectModules.kts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env kotlin
  2. //install kotlin compiler: https://kotlinlang.org/docs/tutorials/command-line.html
  3. import java.io.File
  4. val includeRegex = Regex("include\\(\"(.*?)\"\\)")
  5. val projectRegex = "project\\(\"([^\"]+)\"(, configuration = \".*\")?\\)".toRegex()
  6. val keepModules = mutableSetOf<Module>()
  7. var root = ""
  8. main(args)
  9. fun main(args: Array<String>) {
  10. if (args.isEmpty()) {
  11. println("Usage: ./docs/contributing/selectModules.kts instrumentation/spring/spring-boot-autoconfigure/ <module to include2> ...")
  12. return
  13. }
  14. (args.map {
  15. moduleOfArg(
  16. File(File(it).absolutePath),
  17. "/" + it.trimStart('.', '/').trimEnd('/')
  18. )
  19. } + listOf(":javaagent"))
  20. .map { Module(it) }
  21. .forEach(Module::addSelfAndChildren)
  22. File("$root/conventions/src/main/kotlin").listFiles()!!
  23. .filter { it.name.endsWith(".kts") }
  24. .forEach {
  25. children(it).forEach(Module::addSelfAndChildren)
  26. }
  27. println("removing modules except:\n${keepModules.map { it.name }.sorted().joinToString("\n")}")
  28. val target = File("$root/settings.gradle.kts")
  29. val text = target.readText().lines().flatMap { line ->
  30. includeRegex.matchEntire(line)?.let { it.groupValues[1] }?.let { module ->
  31. if (Module(module) in keepModules) {
  32. listOf(line)
  33. } else {
  34. emptyList()
  35. }
  36. } ?: listOf(line)
  37. }.joinToString("\n")
  38. target.writeText(text)
  39. }
  40. data class Module(val name: String) {
  41. fun children(): List<Module> {
  42. val file = moduleFile()
  43. return children(file)
  44. }
  45. private fun moduleFile(): File = File("$root/${name.replace(":", "/")}/build.gradle.kts")
  46. fun addSelfAndChildren() {
  47. if (!keepModules.add(this)) {
  48. return
  49. }
  50. children().forEach(Module::addSelfAndChildren)
  51. }
  52. }
  53. fun moduleOfArg(file: File, name: String): String {
  54. val settings = File(file, "settings.gradle.kts")
  55. return if (settings.exists()) {
  56. root = file.absolutePath
  57. name.substringAfter(root).replace("/", ":")
  58. } else {
  59. moduleOfArg(file.parentFile, name)
  60. }
  61. }
  62. fun children(file: File) = file.readText().lines().flatMap { line ->
  63. projectRegex.find(line)?.let { it.groupValues[1] }?.let { module ->
  64. listOf(Module(module))
  65. } ?: emptyList()
  66. }