How to use the Kts ScriptEngine in an IntelliJ IDEA plugin?

Answered

Hello, I would like to parse and execute Kotlin kts scripts in my plugin but I am not sure how to get the scriptengine to work. 

I have already tried to bundle the kotlin-scripting-jsr223 into my plugin which unfortunately did not get resolved in this question.

Is the scriptengine already bundled with IntelliJ? With the following minimalistic plugin, I still get no results:

plugin.xml

<idea-plugin>
<id>com.mydemo</id>
<name>MyKtsDemoPlugin</name>
<vendor>Me</vendor>
<depends>org.jetbrains.kotlin</depends>
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="MyStartup"/>
</extensions>
</idea-plugin>

build.gradle.kts

plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.21"
id("org.jetbrains.intellij") version "1.0"
}

group = "com.me"
version = "0.0.1"

java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-stdlib")
}

apply(plugin = "org.jetbrains.intellij")

intellij {
version.set("2021.2.3")
plugins.add("Kotlin")
}

MyStartup.kt

class MyStartup  : StartupActivity {
override fun runActivity(project: Project) {
val scriptEngine = ScriptEngineManager().getEngineByExtension("kts")
println("ScriptEngine is " + scriptEngine)
}
}

Output:

ScriptEngine is null
0
2 comments

Try with:

IdeScriptEngineManager.getInstance().getEngineByName("kotlin", null)

This will return KotlinJsr223JvmScriptEngine4Idea engine.

1

That's what I was looking for! Thank you :) 

My question is resolved.

 

0

Please sign in to leave a comment.