Created
May 1, 2023 16:26
-
-
Save mikehearn/fdcb2c3ef30c6a2970d089fd8f860216 to your computer and use it in GitHub Desktop.
IntelliJ plugin to register a custom Kotlin scripting host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hydraulic.hshell.intellij | |
import com.intellij.notification.NotificationGroupManager | |
import com.intellij.notification.NotificationType | |
import com.intellij.openapi.diagnostic.logger | |
import java.io.File | |
import java.io.IOException | |
import kotlin.script.experimental.intellij.ScriptDefinitionsProvider | |
private val LOG = logger<HShellDefinitionProvider>() | |
class HShellDefinitionProvider : ScriptDefinitionsProvider { | |
override val id: String | |
get() = ".hshell.kts script" | |
override fun getDefinitionClasses(): Iterable<String> = if (jars.isNotEmpty()) listOf("hydraulic.hshell.HShellScript") else emptyList() | |
private val jars: List<String> by lazy { | |
try { | |
// Find the list of JARs by asking the CLI version of the scripting engine to tell us where it's installed. | |
// This code assumes it's on the path i.e. the user installed it correctly. | |
val cmd = ProcessBuilder().command("/bin/sh", "-l", "-c", "hshell print-classpath").start() | |
val cp: List<String> = cmd.inputStream.bufferedReader().readLines().dropWhile { "cds,dynamic" in it } | |
if (cmd.waitFor() != 0) { | |
val errors = cmd.errorStream.bufferedReader().readLines() | |
throw Exception("hshell invocation failed:\n" + errors.joinToString("\n")) | |
} | |
LOG.info("hshell was found successfully, classpath is:\n" + cp.joinToString("\n") { "• $it" }) | |
cp | |
} catch (e: IOException) { | |
LOG.info(e) | |
NotificationGroupManager.getInstance().getNotificationGroup("Hydraulic Shell") | |
.createNotification("HShell", "HShell was not found. Please check you have it installed and available on your system path.", NotificationType.ERROR) | |
.notify(null) | |
emptyList() | |
} | |
} | |
override fun getDefinitionsClassPath(): Iterable<File> { | |
return jars.map { File(it) } | |
} | |
override fun useDiscovery(): Boolean = false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment