-
Notifications
You must be signed in to change notification settings - Fork 156
/
KtLintWorkAction.kt
193 lines (170 loc) · 6.81 KB
/
KtLintWorkAction.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package org.jlleitschuh.gradle.ktlint.worker
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.ParseException
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import net.swiftzer.semver.SemVer
import org.apache.commons.io.input.MessageDigestCalculatingInputStream
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.jlleitschuh.gradle.ktlint.worker.KtLintWorkAction.FormatTaskSnapshot.Companion.contentHash
import java.io.File
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import java.util.ServiceLoader
@Suppress("UnstableApiUsage")
abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParameters> {
private val logger = Logging.getLogger("ktlint-worker")
override fun execute() {
val ruleSets = loadRuleSetsAndFilterThem(
parameters.enableExperimental.getOrElse(false),
parameters.disabledRules.getOrElse(emptySet())
)
val additionalEditorConfig = parameters
.additionalEditorconfigFile
.orNull
?.asFile
?.absolutePath
val userData = generateUserData()
val debug = parameters.debug.get()
val formatSource = parameters.formatSource.getOrElse(false)
resetEditorconfigCache()
val result = mutableListOf<LintErrorResult>()
val formattedFiles = mutableMapOf<File, ByteArray>()
parameters.filesToLint.files.forEach {
val errors = mutableListOf<Pair<LintError, Boolean>>()
val ktLintParameters = KtLint.Params(
fileName = it.absolutePath,
text = it.readText(),
ruleSets = ruleSets,
userData = userData,
debug = debug,
editorConfigPath = additionalEditorConfig,
script = !it.name.endsWith(".kt", ignoreCase = true),
cb = { lintError, isCorrected ->
errors.add(lintError to isCorrected)
}
)
try {
if (formatSource) {
val currentFileContent = it.readText()
val updatedFileContent = KtLint.format(ktLintParameters)
if (updatedFileContent != currentFileContent) {
formattedFiles[it] = contentHash(it)
it.writeText(updatedFileContent)
}
} else {
KtLint.lint(ktLintParameters)
}
} catch (e: ParseException) {
throw GradleException(
"KtLint failed to parse file: ${it.absolutePath}",
e
)
}
result.add(
LintErrorResult(
lintedFile = it,
lintErrors = errors
)
)
}
KtLintClassesSerializer
.create(
SemVer.parse(parameters.ktLintVersion.get())
)
.saveErrors(
result,
parameters.discoveredErrorsFile.asFile.get()
)
if (formattedFiles.isNotEmpty()) {
val snapshotFile = parameters.formatSnapshot.get().asFile
.also { if (!it.exists()) it.createNewFile() }
val snapshot = FormatTaskSnapshot(formattedFiles)
FormatTaskSnapshot.writeIntoFile(snapshotFile, snapshot)
}
}
private fun resetEditorconfigCache() {
if (parameters.editorconfigFilesWereChanged.get()) {
logger.info("Resetting KtLint caches")
// Calling trimMemory() will also reset internal loaded `.editorconfig` cache
KtLint.trimMemory()
}
}
private fun generateUserData(): Map<String, String> {
val userData = mutableMapOf(
"android" to parameters.android.get().toString()
)
val disabledRules = parameters.disabledRules.get()
if (disabledRules.isNotEmpty()) {
userData["disabled_rules"] = disabledRules.joinToString(separator = ",")
}
return userData.toMap()
}
private fun loadRuleSetsAndFilterThem(
enableExperimental: Boolean,
disabledRules: Set<String>
): Set<RuleSet> = loadRuleSetsFromClasspath()
.filterKeys { enableExperimental || it != "experimental" }
.filterKeys { !(disabledRules.contains("standard") && it == "\u0000standard") }
.toSortedMap()
.mapValues { it.value.get() }
.values
.toSet()
private fun loadRuleSetsFromClasspath(): Map<String, RuleSetProvider> = ServiceLoader
.load(RuleSetProvider::class.java)
.associateBy {
val key = it.get().id
// Adapted from KtLint CLI module
if (key == "standard") "\u0000$key" else key
}
interface KtLintWorkParameters : WorkParameters {
val filesToLint: ConfigurableFileCollection
val android: Property<Boolean>
val disabledRules: SetProperty<String>
val enableExperimental: Property<Boolean>
val debug: Property<Boolean>
val additionalEditorconfigFile: RegularFileProperty
val formatSource: Property<Boolean>
val discoveredErrorsFile: RegularFileProperty
val ktLintVersion: Property<String>
val editorconfigFilesWereChanged: Property<Boolean>
val formatSnapshot: RegularFileProperty
}
/**
* Represents pre-formatted files snapshot (file + it contents hash).
*/
internal class FormatTaskSnapshot(
val formattedSources: Map<File, ByteArray>
) : Serializable {
companion object {
private const val serialVersionUID = 1L
fun readFromFile(snapshotFile: File) =
ObjectInputStream(snapshotFile.inputStream().buffered())
.use {
it.readObject() as FormatTaskSnapshot
}
fun writeIntoFile(
snapshotFile: File,
formatSnapshot: FormatTaskSnapshot
) = ObjectOutputStream(snapshotFile.outputStream().buffered())
.use {
it.writeObject(formatSnapshot)
}
fun contentHash(file: File): ByteArray {
return MessageDigestCalculatingInputStream(file.inputStream().buffered()).use {
it.readBytes()
it.messageDigest.digest()
}
}
}
}
}