Skip to content

Commit 4cafeb5

Browse files
authored
chore(build): improve detektAll checks (#11230)
2 parents caa7b98 + 44e0ff9 commit 4cafeb5

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

build-plugin/plugin/src/main/kotlin/net/thunderbird/gradle/plugin/quality/detekt/DetektPlugin.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package net.thunderbird.gradle.plugin.quality.detekt
33
import dev.detekt.gradle.Detekt
44
import dev.detekt.gradle.DetektCreateBaselineTask
55
import dev.detekt.gradle.extensions.DetektExtension
6+
import java.io.File
7+
import java.nio.file.Path
68
import net.thunderbird.gradle.plugin.ProjectConfig
79
import net.thunderbird.gradle.plugin.libs
810
import org.gradle.api.Plugin
911
import org.gradle.api.Project
12+
import org.gradle.api.file.FileTreeElement
1013
import org.gradle.kotlin.dsl.assign
1114
import org.gradle.kotlin.dsl.dependencies
1215
import org.gradle.kotlin.dsl.withType
@@ -47,13 +50,15 @@ class DetektPlugin : Plugin<Project> {
4750
private fun Project.configureDetektTasks() {
4851
with(tasks) {
4952
withType<Detekt>().configureEach {
53+
val isInProjectBuildDirectory = buildDirectoryExclusion(layout.buildDirectory.get().asFile)
54+
5055
if (name.contains("androidHostTest", ignoreCase = true)) {
5156
enabled = false
5257
}
5358

5459
jvmTarget = ProjectConfig.Compiler.jvmTarget.target
5560

56-
exclude(defaultExcludes)
61+
exclude(isInProjectBuildDirectory)
5762

5863
reports {
5964
checkstyle.required.set(false)
@@ -66,13 +71,15 @@ class DetektPlugin : Plugin<Project> {
6671
}
6772

6873
withType<DetektCreateBaselineTask>().configureEach {
74+
val isInProjectBuildDirectory = buildDirectoryExclusion(layout.buildDirectory.get().asFile)
75+
6976
if (name.contains("androidHostTest", ignoreCase = true)) {
7077
enabled = false
7178
}
7279

7380
jvmTarget = ProjectConfig.Compiler.jvmTarget.target
7481

75-
exclude(defaultExcludes)
82+
exclude(isInProjectBuildDirectory)
7683
}
7784

7885
register("detektAll") {
@@ -85,11 +92,12 @@ class DetektPlugin : Plugin<Project> {
8592
}
8693
}
8794

88-
private val defaultExcludes = listOf(
89-
"**/.gradle/**",
90-
"**/.idea/**",
91-
"**/build/**",
92-
"**/generated/**",
93-
".github/**",
94-
"gradle/**",
95-
)
95+
private fun buildDirectoryExclusion(buildDirectory: File): (FileTreeElement) -> Boolean {
96+
val buildDirectoryPath = buildDirectory.normalizedPath()
97+
98+
return { fileTreeElement ->
99+
fileTreeElement.file.normalizedPath().startsWith(buildDirectoryPath)
100+
}
101+
}
102+
103+
private fun File.normalizedPath(): Path = absoluteFile.toPath().normalize()

config/detekt/detekt.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ comments:
4747
endOfSentenceFormat: '([.?!][ \t\n\r\f<])|([.?!:]$)'
4848
KDocReferencesNonPublicProperty:
4949
active: false
50-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
50+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
5151
OutdatedDocumentation:
5252
active: false
5353
matchTypeParameters: true
@@ -56,7 +56,7 @@ comments:
5656
exhaustive: true
5757
UndocumentedPublicClass:
5858
active: false
59-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
59+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
6060
searchInNestedClass: true
6161
searchInInnerClass: true
6262
searchInInnerObject: true
@@ -65,11 +65,11 @@ comments:
6565
ignoreDefaultCompanionObject: false
6666
UndocumentedPublicFunction:
6767
active: false
68-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
68+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
6969
searchProtectedFunction: false
7070
UndocumentedPublicProperty:
7171
active: false
72-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
72+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
7373
searchProtectedProperty: false
7474
ignoreEnumEntries: false
7575

@@ -144,14 +144,14 @@ complexity:
144144
active: false
145145
StringLiteralDuplication:
146146
active: false
147-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
147+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
148148
allowedDuplications: 2
149149
ignoreAnnotation: true
150150
allowedWithLengthLessThan: 5
151151
ignoreStringsRegex: '$^'
152152
TooManyFunctions:
153153
active: true
154-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
154+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
155155
allowedFunctionsPerFile: 11
156156
allowedFunctionsPerClass: 11
157157
allowedFunctionsPerInterface: 11
@@ -237,7 +237,7 @@ exceptions:
237237
- 'toString'
238238
InstanceOfCheckForException:
239239
active: true
240-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
240+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
241241
NotImplementedDeclaration:
242242
active: false
243243
ObjectExtendsThrowable:
@@ -263,7 +263,7 @@ exceptions:
263263
active: false
264264
ThrowingExceptionsWithoutMessageOrCause:
265265
active: true
266-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
266+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
267267
exceptions:
268268
- 'ArrayIndexOutOfBoundsException'
269269
- 'Exception'
@@ -278,7 +278,7 @@ exceptions:
278278
active: true
279279
TooGenericExceptionCaught:
280280
active: true
281-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
281+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
282282
exceptionNames:
283283
- 'ArrayIndexOutOfBoundsException'
284284
- 'Error'
@@ -329,7 +329,7 @@ naming:
329329
FunctionNaming:
330330
active: true
331331
aliases: ['FunctionName']
332-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/nativeTest/**']
332+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/nativeTest/**']
333333
functionPattern: '[a-z][a-zA-Z0-9]*'
334334
excludeClassPattern: '$^'
335335
ignoreAnnotated:
@@ -405,10 +405,10 @@ performance:
405405
allowedOperations: 2
406406
ForEachOnRange:
407407
active: true
408-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
408+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
409409
SpreadOperator:
410410
active: true
411-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
411+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
412412
UnnecessaryInitOnArray:
413413
active: false
414414
UnnecessaryPartOfBinaryExpression:
@@ -492,7 +492,7 @@ potential-bugs:
492492
active: true
493493
LateinitUsage:
494494
active: false
495-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
495+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
496496
ignoreOnClassesPattern: ''
497497
MapGetWithNotNullAssertionOperator:
498498
active: true
@@ -540,7 +540,7 @@ potential-bugs:
540540
active: true
541541
UnsafeCallOnNullableType:
542542
active: true
543-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
543+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
544544
UnsafeCast:
545545
active: true
546546
aliases: ['UNCHECKED_CAST']
@@ -676,7 +676,7 @@ style:
676676
maxJumpCount: 1
677677
MagicNumber:
678678
active: true
679-
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts']
679+
excludes: ['**/test/**', '**/androidTest/**', '**/androidHostTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts']
680680
ignoreNumbers:
681681
- '-1'
682682
- '0'

0 commit comments

Comments
 (0)