Skip to content

Commit

Permalink
Fix: IDEA plugin can be installed as a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelcarmena committed Jan 15, 2020
1 parent 980915e commit 6b862f1
Showing 1 changed file with 24 additions and 13 deletions.
Expand Up @@ -9,6 +9,7 @@ import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
import java.util.jar.JarFile

open class InstallIdeaPlugin: DefaultTask() {

Expand Down Expand Up @@ -52,27 +53,37 @@ internal fun inIdea(): Boolean =

internal fun pluginsDirExists(): Boolean =
when {
System.getProperty("idea.plugins.path") != null ->
File(System.getProperty("idea.plugins.path")).exists()
System.getProperty("jb.vmOptionsFile") != null ->
File(pluginsDirFromVmOptionsFile()).exists()
else ->
false
System.getProperty("idea.plugins.path") != null -> File(System.getProperty("idea.plugins.path")).exists()
System.getProperty("jb.vmOptionsFile") != null -> File(pluginsDirFromVmOptionsFile()).exists()
else -> false
}

private fun pluginsDir(): String =
when {
System.getProperty("idea.plugins.path") != null ->
System.getProperty("idea.plugins.path")
else -> {
pluginsDirFromVmOptionsFile()
}
System.getProperty("idea.plugins.path") != null -> System.getProperty("idea.plugins.path")
else -> pluginsDirFromVmOptionsFile()
}

internal fun ideaPluginExists(): Boolean =
File(pluginsDir()).listFiles().any { it.name.startsWith("idea-plugin") }
File(pluginsDir()).listFiles().any {
it.name == "Arrow Meta Intellij IDEA Plugin" || isArrowMetaPlugin(it)
}

private fun isArrowMetaPlugin(file: File): Boolean =
file.name.startsWith("idea-plugin")
&& file.extension == "jar"
&& hasArrowId(file.absolutePath)

private fun hasArrowId(path: String): Boolean {
val artifact = JarFile(path)
val pluginFile = artifact.getEntry("META-INF/plugin.xml") ?: return false
val parser = DOMParser()
parser.parse(InputSource(artifact.getInputStream(pluginFile)))
val items = parser.document.getElementsByTagName("id")
return (items.length == 1) && (items.item(0).textContent == "io.arrow-kt.arrow")
}

private fun pluginsDirFromVmOptionsFile(): String {
val configDir = File(System.getProperty("jb.vmOptionsFile")).parent
return Paths.get(configDir, "plugins").toString()
}
}

0 comments on commit 6b862f1

Please sign in to comment.