|
1 | 1 | package tools.forma.includer |
2 | 2 |
|
| 3 | +import java.io.File |
| 4 | +import kotlin.system.measureTimeMillis |
| 5 | +import kotlinx.coroutines.Dispatchers |
| 6 | +import kotlinx.coroutines.async |
| 7 | +import kotlinx.coroutines.awaitAll |
3 | 8 | import kotlinx.coroutines.coroutineScope |
4 | 9 | import kotlinx.coroutines.runBlocking |
5 | | -import kotlinx.coroutines.* |
6 | 10 | import org.gradle.api.Plugin |
7 | 11 | import org.gradle.api.initialization.Settings |
8 | 12 | import org.gradle.api.logging.LogLevel |
| 13 | +import org.gradle.api.logging.Logger |
9 | 14 | import org.gradle.api.logging.Logging |
10 | | -import java.io.File |
11 | | -import kotlin.system.measureTimeMillis |
12 | 15 |
|
13 | | -val logger = Logging.getLogger(IncluderPlugin::class.java) |
| 16 | +val logger: Logger = Logging.getLogger(IncluderPlugin::class.java) |
14 | 17 |
|
15 | 18 | /** |
16 | | - * Once applied Includer will detect nested projects and automatically include it into the rootProject |
| 19 | + * Once applied, Includer will search for nested projects and automatically includes them in the |
| 20 | + * build. |
17 | 21 | */ |
18 | 22 | class IncluderPlugin : Plugin<Settings> { |
19 | 23 | override fun apply(settings: Settings) { |
20 | 24 | includeSubprojects(settings) |
21 | 25 | } |
| 26 | +} |
22 | 27 |
|
23 | | - private fun includeSubprojects(settings: Settings) { |
24 | | - measureTimeMillis { |
25 | | - runBlocking { |
26 | | - findGradleKtsFiles(settings.rootDir, settings.rootDir) |
27 | | - .forEach { |
28 | | - val moduleDir = it.parentFile |
29 | | - val relativePath = settings.rootDir.toPath().relativize(moduleDir.toPath()).toString() |
30 | | - val moduleName = ":" + relativePath.replace(File.separator, "-") |
31 | | - |
32 | | - settings.include(moduleName) |
| 28 | +private fun includeSubprojects(settings: Settings) { |
| 29 | + val measuredTime = measureTimeMillis { |
| 30 | + runBlocking { findGradleKtsFiles(settings.rootDir, settings.rootDir) } |
| 31 | + .forEach { buildFile -> |
| 32 | + val moduleDir = buildFile.parentFile |
| 33 | + val relativePath = |
| 34 | + settings.rootDir.toPath().relativize(moduleDir.toPath()).toString() |
| 35 | + // Avoid using `:` as separator for module names as it is used by gradle to |
| 36 | + // mark |
| 37 | + // intermittent nested projects, which created automatically. This behavior |
| 38 | + // leads to increased configuration time |
| 39 | + val moduleName = ":" + relativePath.replace(File.separator, "-") |
33 | 40 |
|
34 | | - val project = settings.findProject(moduleName)!! |
35 | | - project.projectDir = moduleDir |
36 | | - project.buildFileName = it.name |
| 41 | + settings.include(moduleName) |
37 | 42 |
|
38 | | - } |
| 43 | + val project = settings.findProject(moduleName)!! |
| 44 | + project.projectDir = moduleDir |
| 45 | + project.buildFileName = buildFile.name |
39 | 46 | } |
40 | | - }.let { logger.log(LogLevel.INFO, "Loaded in $it ms") } |
41 | 47 | } |
| 48 | + logger.log(LogLevel.INFO, "Loaded in $measuredTime ms") |
| 49 | +} |
42 | 50 |
|
43 | | - private suspend fun findGradleKtsFiles( |
44 | | - rootDir: File, |
45 | | - currentDir: File, |
46 | | - ignoredFilenames: List<String> = listOf("settings.gradle.kts", "settings.gradle"), |
47 | | - ignoredFolders: List<String> = listOf("build", "src", "buildSrc") |
48 | | - ): List<File> = coroutineScope { |
| 51 | +/** |
| 52 | + * Recursively finds all gradle files in the given directory, excluding the hidden files, given |
| 53 | + * filenames and folders. |
| 54 | + * |
| 55 | + * Ignores nested projects if the current directory contains a file from the `projectMarkerFiles` |
| 56 | + * list. |
| 57 | + * |
| 58 | + * @param rootDir root directory of the project |
| 59 | + * @param currentDir current directory to search |
| 60 | + * @param ignoredFilenames list of filenames to ignore |
| 61 | + * @param ignoredFolders list of folder names to ignore |
| 62 | + * @param projectMarkerFiles filenames that indicate that the directory as a gradle project |
| 63 | + */ |
| 64 | +private suspend fun findGradleKtsFiles( |
| 65 | + rootDir: File, |
| 66 | + currentDir: File, |
| 67 | + ignoredFilenames: List<String> = emptyList(), |
| 68 | + ignoredFolders: List<String> = listOf("build", "src", "buildSrc"), |
| 69 | + projectMarkerFiles: List<String> = listOf("settings.gradle.kts", "settings.gradle"), |
| 70 | +): List<File> = coroutineScope { |
| 71 | + val children = currentDir.listFiles() ?: emptyArray() |
49 | 72 |
|
50 | | - val children = currentDir.listFiles() ?: emptyArray() |
| 73 | + val (dirs, files) = children.partition { it.isDirectory } |
51 | 74 |
|
52 | | - val (dirs, files) = children.partition { it.isDirectory } |
| 75 | + val gradleKtsFiles = |
| 76 | + if (currentDir == rootDir) { |
| 77 | + // Root project's build.gradle(.kts) always included implicitly |
| 78 | + emptyList() |
| 79 | + } else { |
| 80 | + files |
| 81 | + // gradle files may have name that is different from `build.gradle(.kts)` so we |
| 82 | + // include files which follows `*.gradle(.kts)` pattern |
| 83 | + .filter { it.name.endsWith(".gradle.kts") || it.name.endsWith(".gradle") } |
| 84 | + .filterNot { it.isHidden || it.name in ignoredFilenames } |
| 85 | + } |
53 | 86 |
|
54 | | - val gradleKtsFiles = if (currentDir == rootDir) emptyList() else files |
55 | | - .filter { it.name.endsWith(".gradle.kts") || it.name.endsWith(".gradle") } |
56 | | - .filterNot { it.isHidden || it.name in ignoredFilenames } |
| 87 | + val skipNestedProject = currentDir != rootDir && files.any { it.name in projectMarkerFiles } |
57 | 88 |
|
58 | | - val dirJobs = dirs |
59 | | - .filterNot { it.isHidden || it.name in ignoredFolders } |
60 | | - .map { dir -> |
61 | | - async(Dispatchers.IO) { |
62 | | - findGradleKtsFiles(rootDir, dir, ignoredFilenames, ignoredFolders) |
| 89 | + if (skipNestedProject) { |
| 90 | + gradleKtsFiles |
| 91 | + } else { |
| 92 | + gradleKtsFiles + |
| 93 | + dirs |
| 94 | + .filterNot { it.isHidden || it.name in ignoredFolders } |
| 95 | + .map { dir -> |
| 96 | + async(Dispatchers.IO) { |
| 97 | + findGradleKtsFiles(rootDir, dir, ignoredFilenames, ignoredFolders) |
| 98 | + } |
63 | 99 | } |
64 | | - } |
65 | | - |
66 | | - val nestedGradleKtsFiles = dirJobs.awaitAll().flatten() |
67 | | - |
68 | | - gradleKtsFiles + nestedGradleKtsFiles |
| 100 | + .awaitAll() |
| 101 | + .flatten() |
69 | 102 | } |
70 | 103 | } |
0 commit comments