dsvoronin

include all

Apr 5th, 2022 (edited)
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.02 KB | None | 0 0
  1. ////settings.gradle.kts
  2. storeInGradleExtras(includeAllSubdirectories("avito-app"))
  3.  
  4.  
  5. /**
  6.  * @return module's paths that was included
  7.  */
  8. fun includeAllSubdirectories(dir: String): ModulePaths {
  9.  
  10.     val paths = HashSet<String>()
  11.  
  12.     file(dir).listFiles()
  13.         .filter { file -> file.isDirectory && (file.name != "build") }
  14.         .forEach { currentFolder ->
  15.             if (isGradleModuleDir(currentFolder)) {
  16.                 val path = ":${dir.replace('/', ':')}:${currentFolder.name}"
  17.                 paths.add(path)
  18.                 include(path)
  19.             } else {
  20.                 paths.addAll(includeAllSubdirectories("$dir/${currentFolder.name}").paths)
  21.             }
  22.         }
  23.     return ModulePaths(directory = dir, paths = paths)
  24. }
  25.  
  26. data class ModulePaths(val directory: String, val paths: Set<String>)
  27.  
  28. /**
  29.  * Will be used in build scripts in modules.
  30.  * See `modulesGroup` function usages.
  31.  */
  32. fun storeInGradleExtras(paths: ModulePaths) {
  33.     (gradle as ExtensionAware).extensions.extraProperties.set(paths.directory, paths.paths)
  34. }
  35.  
  36. fun isGradleModuleDir(folder: File): Boolean {
  37.     return hasFile(folder, "build.gradle") || hasFile(folder, "build.gradle.kts")
  38. }
  39.  
  40. fun hasFile(folder: File, fileName: String): Boolean {
  41.     val file = File(folder, fileName)
  42.     return file.exists() && file.isFile
  43. }
  44.  
  45.  
  46.  
  47. //// build.gradle.kts
  48.  
  49. dependencies{
  50.  
  51.         gradle.modulesGroup("avito-test").forEach { modulePath -> androidTestImplementation(project(modulePath)) }
  52.  
  53.  
  54. ///
  55.  
  56. fun Gradle.modulesGroup(name: String): Set<String> {
  57.  
  58.     @Suppress("UNCHECKED_CAST")
  59.     val modules = (this as ExtensionAware).extensions.extraProperties[name] as? Set<String>
  60.  
  61.     if (modules == null) {
  62.         throw IllegalStateException(
  63.             "Trying to access modules group, named: $name\n" +
  64.                 "it should contain set of module names but it's not\n" +
  65.                 "Check settings.gradle or settings plugins where it should be set"
  66.         )
  67.     } else {
  68.         return modules
  69.     }
  70. }
  71.  
Add Comment
Please, Sign In to add comment