Skip to content

Instantly share code, notes, and snippets.

@halirutan
Created March 9, 2020 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halirutan/6346c4b3d52fd36ce4cf41f2ddc61e15 to your computer and use it in GitHub Desktop.
Save halirutan/6346c4b3d52fd36ce4cf41f2ddc61e15 to your computer and use it in GitHub Desktop.
Gradle task for locally verifying an IntelliJ Plugin
import org.gradle.api.internal.ConventionTask
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("de.undercouch:gradle-download-task:4.0.2")
}
}
open class PluginVerifierRunner : ConventionTask() {
private val pluginVerifierDir = "pluginVerifier"
private val pluginVerifierName = "verifier-all.jar"
private val verifierVersion = "1.222"
@Input
val pluginFileName: Property<String> = objects.property()
@Input
private var ides: ListProperty<String> = objects.listProperty()
@org.gradle.api.tasks.TaskAction
fun runTaskAction() {
if (!project.file("${project.buildDir}/distributions/${pluginFileName}.zip").exists())
throw IllegalStateException("Plugin file $pluginFileName does not exist.")
if (!ides.isPresent) {
throw IllegalArgumentException("Need IDEs to test against")
}
val ideList = ides.get()
downloadVerifier()
runVerifier(ideList.map { ide -> resolveIde(ide) })
}
private fun downloadVerifier() {
val url =
"https://dl.bintray.com/jetbrains/intellij-plugin-service/org/jetbrains/intellij/" +
"plugins/verifier-cli/$verifierVersion/verifier-cli-$verifierVersion-all.jar"
with(de.undercouch.gradle.tasks.download.DownloadAction(project)) {
src(url)
dest("$project.buildDir/$pluginVerifierDir/$pluginVerifierName")
overwrite(false)
execute()
}
}
private fun resolveIde(identifier: String): File {
logger.lifecycle("Resolving $identifier")
val dependency = project.dependencies.create(identifierToDependency(identifier))
val configuration = project.configurations.detachedConfiguration(dependency)
val archive = configuration.singleFile.absolutePath
val extractionTarget = File(archive.substring(0, archive.length - ".zip".length))
if (!extractionTarget.exists()) {
logger.lifecycle("Extracting $identifier")
project.copy {
from(project.zipTree(archive))
into(extractionTarget)
}
}
return extractionTarget
}
private fun runVerifier(ides: List<File>) {
project.javaexec {
val idePaths = ides.map { it.absolutePath }
classpath = project.files("$project.buildDir/$pluginVerifierDir/$pluginVerifierName")
main = "com.jetbrains.pluginverifier.PluginVerifierMain"
args = listOf<String>(
"-verification-reports-dir", "build/$pluginVerifierDir/reports",
"check-plugin",
"${project.buildDir}/distributions/${pluginFileName}.zip",
*idePaths.toTypedArray()
)
}
}
private fun identifierToDependency(identifier: String): String {
val ide = identifier.split("-")
if (ide.size != 2) {
throw IllegalArgumentException("Unknown IDE")
}
val type = ide[0]
val version = ide[1]
val dependencyGroup: String
val dependencyName: String
when (type) {
"IC", "IU" -> {
dependencyGroup = "com.jetbrains.intellij.idea"
dependencyName = "idea$type"
}
"CL" -> {
dependencyGroup = "com.jetbrains.intellij.clion"
dependencyName = "clion"
}
"PC", "PY" -> {
dependencyGroup = "com.jetbrains.intellij.pycharm"
dependencyName = "pycharm$type"
}
"RD" -> {
dependencyGroup = "com.jetbrains.intellij.rider"
dependencyName = "riderRD"
}
else -> throw IllegalArgumentException("Unknown IDE type `$type`.")
}
return "$dependencyGroup:$dependencyName:$version"
}
}
project.tasks.register<PluginVerifierRunner>("runPluginVerifier") {
dependsOn(project.tasks.findByName("buildPlugin"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment