-
Notifications
You must be signed in to change notification settings - Fork 270
/
VerifyPluginTask.kt
413 lines (367 loc) · 15.9 KB
/
VerifyPluginTask.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.intellij.platform.gradle.tasks
import org.apache.tools.ant.util.TeeOutputStream
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Optional
import org.gradle.kotlin.dsl.named
import org.jetbrains.intellij.platform.gradle.Constants.Configurations
import org.jetbrains.intellij.platform.gradle.Constants.Plugin
import org.jetbrains.intellij.platform.gradle.Constants.Tasks
import org.jetbrains.intellij.platform.gradle.extensions.IntelliJPlatformExtension
import org.jetbrains.intellij.platform.gradle.tasks.aware.PluginVerifierAware
import org.jetbrains.intellij.platform.gradle.tasks.aware.RuntimeAware
import org.jetbrains.intellij.platform.gradle.utils.Logger
import org.jetbrains.intellij.platform.gradle.utils.asLenient
import org.jetbrains.intellij.platform.gradle.utils.asPath
import org.jetbrains.intellij.platform.gradle.utils.extensionProvider
import java.io.ByteArrayOutputStream
import java.util.*
import kotlin.io.path.exists
import kotlin.io.path.pathString
/**
* Runs the IntelliJ Plugin Verifier CLI tool to check the binary compatibility with specified IDE builds.
*
* @see IntelliJPlatformExtension.VerifyPlugin
* @see <a href="https://github.com/JetBrains/intellij-plugin-verifier">IntelliJ Plugin Verifier</a>
* @see <a href="https://plugins.jetbrains.com/docs/intellij/verifying-plugin-compatibility.html">Verifying Plugin Compatibility</a>
*
* TODO: Use Reporting for handling verification report output? See: https://docs.gradle.org/current/dsl/org.gradle.api.reporting.Reporting.html
* TODO: Parallel run? https://docs.gradle.org/current/userguide/worker_api.html#converting_to_worker_api
*/
@UntrackedTask(because = "Should always run")
abstract class VerifyPluginTask : JavaExec(), RuntimeAware, PluginVerifierAware {
/**
* Holds a reference to IntelliJ Platform IDEs which will be used by the IntelliJ Plugin Verifier CLI tool for verification.
*
* The list of IDEs is controlled with the [IntelliJPlatformExtension.VerifyPlugin.Ides] extension.
*
* @see [IntelliJPlatformExtension.VerifyPlugin.Ides]
*/
@get:Classpath
abstract val ides: ConfigurableFileCollection
/**
* Input ZIP archive file of the plugin to verify.
* If empty, the task will be skipped.
*
* Default value: [BuildPluginTask.archiveFile]
*/
@get:InputFile
@get:SkipWhenEmpty
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val archiveFile: RegularFileProperty
/**
* The list of class prefixes from the external libraries.
* The Plugin Verifier will not report `No such class` for classes of these packages.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.externalPrefixes]
*
* @see IntelliJPlatformExtension.VerifyPlugin.externalPrefixes
*/
@get:Input
@get:Optional
abstract val externalPrefixes: ListProperty<String>
/**
* Defines the verification level at which the task should fail if any reported issue matches.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.failureLevel]
*
* @see FailureLevel
* @see IntelliJPlatformExtension.VerifyPlugin.failureLevel
*/
@get:Input
abstract val failureLevel: ListProperty<FailureLevel>
/**
* The list of free arguments is passed directly to the IntelliJ Plugin Verifier CLI tool.
*
* They can be used in addition to the arguments that are provided by dedicated options.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.freeArgs]
*
* @see IntelliJPlatformExtension.VerifyPlugin.freeArgs
*/
@get:Input
@get:Optional
abstract val freeArgs: ListProperty<String>
/**
* A file that contains a list of problems that will be ignored in a report.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.ignoredProblemsFile]
*
* @see IntelliJPlatformExtension.VerifyPlugin.ignoredProblemsFile
*/
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:Optional
abstract val ignoredProblemsFile: RegularFileProperty
/**
* Determines if the operation is running in offline mode.
*
* Default value: [StartParameter.offline]
*
* @see StartParameter
* @see <a href="https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_execution_options">Command Line Execution options</a>
*/
@get:Internal
abstract val offline: Property<Boolean>
/**
* Specifies which subsystems of IDE should be checked.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.subsystemsToCheck]
*
* @see Subsystems
* @see IntelliJPlatformExtension.VerifyPlugin.subsystemsToCheck
*/
@get:Input
@get:Optional
abstract val subsystemsToCheck: Property<Subsystems>
/**
* A flag that controls the output format - if set to `true`, the TeamCity compatible output will be returned to stdout.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.teamCityOutputFormat]
*
* @see IntelliJPlatformExtension.VerifyPlugin.teamCityOutputFormat
*/
@get:Input
@get:Optional
abstract val teamCityOutputFormat: Property<Boolean>
/**
* The path to the directory where verification reports will be saved.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.verificationReportsDirectory]
*
* @see IntelliJPlatformExtension.VerifyPlugin.verificationReportsDirectory
*/
@get:OutputDirectory
@get:Optional
abstract val verificationReportsDirectory: DirectoryProperty
/**
* The output formats of the verification reports.
*
* Default value: [IntelliJPlatformExtension.VerifyPlugin.verificationReportsFormats]
*
* @see VerificationReportsFormats
* @see IntelliJPlatformExtension.VerifyPlugin.verificationReportsFormats
*/
@get:Input
@get:Optional
abstract val verificationReportsFormats: ListProperty<VerificationReportsFormats>
private val log = Logger(javaClass)
init {
group = Plugin.GROUP_NAME
description = "Runs the IntelliJ Plugin Verifier tool to check the binary compatibility with specified IDE builds."
mainClass.set("com.jetbrains.pluginverifier.PluginVerifierMain")
}
/**
* Runs the IntelliJ Plugin Verifier against the plugin artifact.
*/
@TaskAction
override fun exec() {
val file = archiveFile.orNull?.asPath
if (file == null || !file.exists()) {
throw IllegalStateException("Plugin file does not exist: $file")
}
log.debug("Distribution file: $file")
val executable = pluginVerifierExecutable.orNull
?.asPath
?: throw GradleException(
"""
No IntelliJ Plugin Verifier executable found.
Please ensure the `pluginVerifier()` entry is present in the project dependencies section or `intellijPlatform.verifyPlugin.cliPath` extension property
See: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform-verifyPlugin
""".trimIndent()
)
log.debug("Verifier path: $executable")
classpath = objectFactory.fileCollection().from(executable)
with(ides.files) {
if (isEmpty()) {
throw GradleException(
"""
No IDE resolved for verification with the IntelliJ Plugin Verifier.
Please ensure the `intellijPlatform.verifyPlugin.ides` extension block is configured along with the `defaultRepositories()` (or at least `localPlatformArtifacts()`) entry in the repositories section.
See: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform-verifyPlugin-ides
""".trimIndent()
)
}
args(listOf("check-plugin") + getOptions() + file.pathString + map {
when {
it.isDirectory -> it.absolutePath
else -> it.readText()
}
})
}
ByteArrayOutputStream().use { os ->
standardOutput = TeeOutputStream(System.out, os)
super.exec()
verifyOutput(os.toString())
}
}
/**
* Collects all the options for the Plugin Verifier CLI provided with the task configuration.
*
* @return array with all available CLI options
*/
private fun getOptions(): List<String> {
val args = mutableListOf(
"-verification-reports-dir", verificationReportsDirectory.asPath.pathString,
"-runtime-dir", runtimeDirectory.asPath.pathString,
)
externalPrefixes.get().takeIf { it.isNotEmpty() }?.let {
args.add("-external-prefixes")
args.add(it.joinToString(":"))
}
if (teamCityOutputFormat.get()) {
args.add("-team-city")
}
if (subsystemsToCheck.orNull != null) {
args.add("-subsystems-to-check")
args.add(subsystemsToCheck.get().toString())
}
if (offline.get()) {
args.add("-offline")
}
// TODO check PV version
args.add("-verification-reports-formats")
args.add(verificationReportsFormats.get().joinToString(","))
if (ignoredProblemsFile.orNull != null) {
args.add("-ignored-problems")
args.add(ignoredProblemsFile.asPath.pathString)
}
freeArgs.orNull?.let {
args.addAll(it)
}
return args
}
/**
* @throws GradleException
*/
@Throws(GradleException::class)
private fun verifyOutput(output: String) {
log.debug("Current failure levels: ${FailureLevel.values().joinToString(", ")}")
val invalidFilesMessage = "The following files specified for the verification are not valid plugins:"
if (output.contains(invalidFilesMessage)) {
val errorMessage = output.lines()
.dropWhile { it != invalidFilesMessage }
.dropLastWhile { !it.startsWith(" ") }
.joinToString("\n")
throw GradleException(errorMessage)
}
FailureLevel.values().forEach { level ->
if (failureLevel.get().contains(level) && output.contains(level.sectionHeading)) {
log.debug("Failing task on '$failureLevel' failure level")
throw GradleException(
"$level: ${level.message} Check Plugin Verifier report for more details.\n" +
"Incompatible API Changes: https://jb.gg/intellij-api-changes"
)
}
}
}
companion object : Registrable {
override fun register(project: Project) =
project.registerTask<VerifyPluginTask>(Tasks.VERIFY_PLUGIN) {
val verifyPluginProvider = project.extensionProvider.map { it.verifyPlugin }
val intellijPluginVerifierIdesConfigurations = project.provider { // TODO: use .named when Gradle 8.6+
project.configurations.filter { configuration ->
with(Configurations.INTELLIJ_PLUGIN_VERIFIER_IDES) {
configuration.name == this || configuration.name.startsWith("${this}_")
}
}.map { it.asLenient }
}
val buildPluginTaskProvider = project.tasks.named<BuildPluginTask>(Tasks.BUILD_PLUGIN)
freeArgs.convention(verifyPluginProvider.flatMap { it.freeArgs })
failureLevel.convention(verifyPluginProvider.flatMap { it.failureLevel })
verificationReportsDirectory.convention(verifyPluginProvider.flatMap { it.verificationReportsDirectory })
verificationReportsFormats.convention(verifyPluginProvider.flatMap { it.verificationReportsFormats })
externalPrefixes.convention(verifyPluginProvider.flatMap { it.externalPrefixes })
teamCityOutputFormat.convention(verifyPluginProvider.flatMap { it.teamCityOutputFormat })
subsystemsToCheck.convention(verifyPluginProvider.flatMap { it.subsystemsToCheck })
ignoredProblemsFile.convention(verifyPluginProvider.flatMap { it.ignoredProblemsFile })
ides.from(intellijPluginVerifierIdesConfigurations)
archiveFile.convention(buildPluginTaskProvider.flatMap { it.archiveFile })
offline.convention(project.gradle.startParameter.isOffline)
}
}
@Suppress("unused")
enum class FailureLevel(val sectionHeading: String, val message: String) {
COMPATIBILITY_WARNINGS(
"Compatibility warnings",
"Compatibility warnings detected against the specified IDE version."
),
COMPATIBILITY_PROBLEMS(
"Compatibility problems",
"Compatibility problems detected against the specified IDE version."
),
DEPRECATED_API_USAGES(
"Deprecated API usages",
"Plugin uses API marked as deprecated (@Deprecated)."
),
SCHEDULED_FOR_REMOVAL_API_USAGES(
/* # usage(s) of */"scheduled for removal API",
"Plugin uses API marked as scheduled for removal (ApiStatus.@ScheduledForRemoval)."
),
EXPERIMENTAL_API_USAGES(
"Experimental API usages",
"Plugin uses API marked as experimental (ApiStatus.@Experimental)."
),
INTERNAL_API_USAGES(
"Internal API usages",
"Plugin uses API marked as internal (ApiStatus.@Internal)."
),
OVERRIDE_ONLY_API_USAGES(
"Override-only API usages",
"Override-only API is used incorrectly (ApiStatus.@OverrideOnly)."
),
NON_EXTENDABLE_API_USAGES(
"Non-extendable API usages",
"Non-extendable API is used incorrectly (ApiStatus.@NonExtendable)."
),
PLUGIN_STRUCTURE_WARNINGS(
"Plugin structure warnings",
"The structure of the plugin is not valid."
),
MISSING_DEPENDENCIES(
"Missing dependencies",
"Plugin has some dependencies missing."
),
INVALID_PLUGIN(
"The following files specified for the verification are not valid plugins",
"Provided plugin artifact is not valid."
),
NOT_DYNAMIC(
"Plugin probably cannot be enabled or disabled without IDE restart",
"Plugin probably cannot be enabled or disabled without IDE restart."
);
companion object {
@JvmField
val ALL: EnumSet<FailureLevel> = EnumSet.allOf(FailureLevel::class.java)
@JvmField
val NONE: EnumSet<FailureLevel> = EnumSet.noneOf(FailureLevel::class.java)
}
}
@Suppress("unused")
enum class VerificationReportsFormats {
PLAIN,
HTML,
MARKDOWN;
companion object {
@JvmField
val ALL: EnumSet<VerificationReportsFormats> = EnumSet.allOf(VerificationReportsFormats::class.java)
@JvmField
val NONE: EnumSet<VerificationReportsFormats> = EnumSet.noneOf(VerificationReportsFormats::class.java)
}
override fun toString() = name.lowercase()
}
@Suppress("unused")
enum class Subsystems {
ALL,
ANDROID_ONLY,
WITHOUT_ANDROID;
override fun toString() = name.lowercase().replace('_', '-')
}
}