forked from detekt/detekt
/
DetektExtension.kt
134 lines (109 loc) · 5.04 KB
/
DetektExtension.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package io.gitlab.arturbosch.detekt.extensions
import org.gradle.api.Action
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.quality.CodeQualityExtension
import java.io.File
import java.util.Properties
import javax.inject.Inject
open class DetektExtension @Inject constructor(objects: ObjectFactory) : CodeQualityExtension() {
init {
toolVersion = loadDetektVersion(DetektExtension::class.java.classLoader)
}
var ignoreFailures: Boolean
@JvmName("ignoreFailures_")
get() = isIgnoreFailures
@JvmName("ignoreFailures_")
set(value) {
isIgnoreFailures = value
}
@Deprecated("Use reportsDir which is equivalent", ReplaceWith("reportsDir"))
val customReportsDir: File?
get() = reportsDir
@Deprecated("Customise the reports on the Detekt task(s) instead.", level = DeprecationLevel.WARNING)
val reports: DetektReports = objects.newInstance(DetektReports::class.java)
@Deprecated(message = "Please use the source property instead.", replaceWith = ReplaceWith("source"))
var input: ConfigurableFileCollection
get() = source
set(value) {
source = value
}
var source: ConfigurableFileCollection = objects.fileCollection()
.from(
DEFAULT_SRC_DIR_JAVA,
DEFAULT_TEST_SRC_DIR_JAVA,
DEFAULT_SRC_DIR_KOTLIN,
DEFAULT_TEST_SRC_DIR_KOTLIN,
)
var baseline: File? = objects
.fileProperty()
.fileValue(File("detekt-baseline.xml"))
.get()
.asFile
var basePath: String? = null
var config: ConfigurableFileCollection = objects.fileCollection()
var debug: Boolean = DEFAULT_DEBUG_VALUE
var parallel: Boolean = DEFAULT_PARALLEL_VALUE
@Deprecated("Please use the buildUponDefaultConfig and allRules flags instead.", ReplaceWith("allRules"))
var failFast: Boolean = DEFAULT_FAIL_FAST_VALUE
var allRules: Boolean = DEFAULT_ALL_RULES_VALUE
var buildUponDefaultConfig: Boolean = DEFAULT_BUILD_UPON_DEFAULT_CONFIG_VALUE
var disableDefaultRuleSets: Boolean = DEFAULT_DISABLE_RULESETS_VALUE
var autoCorrect: Boolean = DEFAULT_AUTO_CORRECT_VALUE
/**
* List of Android build variants for which no detekt task should be created.
*
* This is a combination of build types and flavors, such as fooDebug or barRelease.
*/
var ignoredVariants: List<String> = emptyList()
/**
* List of Android build types for which no detekt task should be created.
*/
var ignoredBuildTypes: List<String> = emptyList()
/**
* List of Android build flavors for which no detekt task should be created
*/
var ignoredFlavors: List<String> = emptyList()
@Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION")
@Deprecated("Customise the reports on the Detekt task(s) instead.", level = DeprecationLevel.WARNING)
fun reports(configure: Action<DetektReports>) {
configure.execute(reports)
}
companion object {
const val DEFAULT_SRC_DIR_JAVA = "src/main/java"
const val DEFAULT_TEST_SRC_DIR_JAVA = "src/test/java"
const val DEFAULT_SRC_DIR_KOTLIN = "src/main/kotlin"
const val DEFAULT_TEST_SRC_DIR_KOTLIN = "src/test/kotlin"
const val DEFAULT_DEBUG_VALUE = false
const val DEFAULT_PARALLEL_VALUE = false
const val DEFAULT_AUTO_CORRECT_VALUE = false
const val DEFAULT_DISABLE_RULESETS_VALUE = false
const val DEFAULT_REPORT_ENABLED_VALUE = true
const val DEFAULT_FAIL_FAST_VALUE = false
const val DEFAULT_ALL_RULES_VALUE = false
const val DEFAULT_BUILD_UPON_DEFAULT_CONFIG_VALUE = false
}
}
internal fun loadDetektVersion(classLoader: ClassLoader): String {
// Other Gradle plugins can also have a versions.properties.
val distinctVersions = classLoader.getResources("versions.properties").toList().mapNotNull { versions ->
Properties().run {
val inputStream = versions.openConnection()
/*
* Due to https://bugs.openjdk.java.net/browse/JDK-6947916 and https://bugs.openjdk.java.net/browse/JDK-8155607,
* it is necessary to disallow caches to maintain stability on JDK 8 and 11 (and possibly more).
* Otherwise, simultaneous invocations of Detekt in the same VM can fail spuriously. A similar bug is referenced in
* https://github.com/detekt/detekt/issues/3396. The performance regression is likely unnoticeable.
* Due to https://github.com/detekt/detekt/issues/4332 it is included for all JDKs.
*/
.apply { useCaches = false }
.getInputStream()
load(inputStream)
getProperty("detektVersion")
}
}.distinct()
return distinctVersions.singleOrNull() ?: error(
"You're importing two Detekt plugins which have different versions. " +
"(${distinctVersions.joinToString()}) Make sure to align the versions."
)
}