diff --git a/build.gradle.kts b/build.gradle.kts index 8b28faa7a0..f1986bfa07 100755 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -140,6 +140,48 @@ spotless { "termux/**/*", ) + // Dirs to PRUNE during the walk. Passing a fileTree to target() bypasses + // Spotless's auto-prune of .git/.gradle/build, so restore them. flox is the + // ADFA-4816 fix: its /nix/store symlinks (millions of files) made + // spotlessCheck take 12+ minutes. + // Bare dir names required: Gradle prunes a subtree only when an exclude + // matches the dir node itself; `dir/**` matches contents and forces a + // descend-and-filter (no pruning). + val traversalExcludes = + arrayOf( + "flox", + "**/.flox", + "**/.git", + "**/.gradle", + ) + + // Gradle build-output dirs (root + subprojects), relative to rootDir. Not + // "**/build": source packages named `build` exist (e.g. + // app/src/main/java/.../actions/build) and would be dropped. + val buildOutputExcludes = + allprojects + .map { + it.layout.buildDirectory + .get() + .asFile + .relativeTo(rootDir) + .invariantSeparatorsPath + }.filter { it.isNotEmpty() && !it.startsWith("..") } + .toTypedArray() + + // Target tree with the shared excludes applied; format-specific ones go in + // extraExcludes. + fun spotlessTarget( + vararg includes: String, + extraExcludes: Array = emptyArray(), + ) = fileTree(rootDir) { + include(*includes) + exclude(*commonTargetExcludes) + exclude(*traversalExcludes) + exclude(*buildOutputExcludes) + exclude(*extraExcludes) + } + // ALWAYS use line feeds (LF -- '\n') lineEndings = LineEnding.UNIX @@ -181,8 +223,7 @@ spotless { }, ) - target("**/src/*/java/**/*.java") - targetExclude(*commonTargetExcludes) + target(spotlessTarget("**/src/*/java/**/*.java")) } kotlin { @@ -197,10 +238,11 @@ spotless { endWithNewline() target( - "**/src/*/java/**/*.kt", - "**/src/*/kotlin/**/*.kt", + spotlessTarget( + "**/src/*/java/**/*.kt", + "**/src/*/kotlin/**/*.kt", + ), ) - targetExclude(*commonTargetExcludes) suppressLintsFor { // suppress the 'file name should conform PascalCase' errors @@ -215,8 +257,7 @@ spotless { trimTrailingWhitespace() endWithNewline() - target("**/*.gradle.kts") - targetExclude(*commonTargetExcludes) + target(spotlessTarget("**/*.gradle.kts")) } format("xml") { @@ -227,13 +268,13 @@ spotless { trimTrailingWhitespace() endWithNewline() - target("**/src/*/res/**/*.xml") - targetExclude(*commonTargetExcludes) - - // Formatting strings.xml with Eclipse WTP causes the strings to be - // split into multiple lines, which is not what we want. - // Exclude strings.xml from this rule. - targetExclude("**/src/*/res/values*/strings.xml") + // Eclipse WTP splits strings.xml entries across lines, so exclude it. + target( + spotlessTarget( + "**/src/*/res/**/*.xml", + extraExcludes = arrayOf("**/src/*/res/values*/strings.xml"), + ), + ) } format("misc") { @@ -241,8 +282,8 @@ spotless { trimTrailingWhitespace() endWithNewline() - target("**/.gitignore", "**/.gradle") - targetExclude(*commonTargetExcludes) + // Only .gitignore; `.gradle` is a cache dir, pruned via traversalExcludes. + target(spotlessTarget("**/.gitignore")) } shell {