Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Kotlin script extension #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
*.iml
target/
.jython_cache/
run/
.DS_Store
!run/plugins/Poly/scripts/**/*
151 changes: 151 additions & 0 deletions bukkit-ext-kts/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>okaeri-poly</artifactId>
<groupId>eu.okaeri</groupId>
<version>1.2.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>okaeri-poly-bukkit-kts-ext</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${library.kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-poly-core</artifactId>
<version>1.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-poly-bukkit</artifactId>
<version>1.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-dependencies</artifactId>
<version>${library.kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-dependencies-maven-all</artifactId>
<version>${library.kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-main-kts</artifactId>
<version>${library.kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-runtime</artifactId>
<version>1.7.20</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eu.okaeri.poly.bukkit.ext;

import eu.okaeri.poly.api.Poly
import eu.okaeri.poly.api.script.ScriptHelper
import eu.okaeri.poly.bukkit.provider.BukkitScriptHelperImpl

class BukkitKotlinScriptScriptHelper(polyKts: PolyKotlinScriptExtension, name: String) : BukkitScriptHelperImpl(polyKts, name)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eu.okaeri.poly.bukkit.ext

import ScriptWithMavenDeps
import eu.okaeri.poly.api.Poly
import eu.okaeri.poly.api.script.ScriptHelper
import eu.okaeri.poly.core.script.ScriptServiceImpl
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

class BukkitKotlinScriptServiceImpl(poly: Poly, private val ext: PolyKotlinScriptExtension) : ScriptServiceImpl(poly) {
override fun exec(name: String, source: String): ScriptHelper {
val helper = BukkitKotlinScriptScriptHelper(ext, name)
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<ScriptWithMavenDeps>() {
providedProperties(getDefaultBindings(helper).map { it.key to KotlinType(it.value.javaClass.kotlin) }.toMap())
}
val result = BasicJvmScriptingHost().eval(source.toScriptSource(name), compilationConfiguration, ScriptEvaluationConfiguration {
providedProperties(getDefaultBindings(helper))
constructorArgs(getDefaultBindings(helper))
})
result.reports.forEach { println(it) }
return helper
}

override fun eval(source: String, context: MutableMap<String, Any>): Any {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.okaeri.poly.bukkit.ext

import eu.okaeri.poly.api.Poly
import eu.okaeri.poly.bukkit.PolyPlugin
import org.bukkit.plugin.java.JavaPlugin

class PolyKotlinScriptExtension : JavaPlugin() {
override fun onLoad() {
val poly = this.server.pluginManager.getPlugin("Poly") as PolyPlugin
poly.scriptManager.register("kts", BukkitKotlinScriptServiceImpl(poly, this))
}
}
66 changes: 66 additions & 0 deletions bukkit-ext-kts/src/main/kotlin/scriptDef.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import eu.okaeri.poly.bukkit.PolyPlugin
import eu.okaeri.poly.bukkit.ext.BukkitKotlinScriptScriptHelper
import eu.okaeri.poly.core.script.ScriptLoggerWrapper
import kotlinx.coroutines.runBlocking
import org.bukkit.Server
import org.jetbrains.kotlin.mainKts.*
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.*
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvmhost.jsr223.importAllBindings
import kotlin.script.experimental.jvmhost.jsr223.jsr223

@KotlinScript(
fileExtension = "poly.kts",
compilationConfiguration = ScriptWithMavenDepsConfiguration::class
)
abstract class ScriptWithMavenDeps(val bindings: Map<String, Any?>) {
@JvmField
val server = bindings["server"] as Server
@JvmField
var script = bindings["script"] as BukkitKotlinScriptScriptHelper
@JvmField
var logger = bindings["logger"] as ScriptLoggerWrapper
@JvmField
var plugin = bindings["plugin"] as PolyPlugin
}

object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration(
{
defaultImports(
DependsOn::class,
Repository::class,
Import::class,
CompilerOptions::class,
ScriptFileLocation::class
)
jvm {
dependenciesFromCurrentContext(wholeClasspath = true)
}
refineConfiguration {
onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations)
}
ide {
acceptedLocations(ScriptAcceptedLocation.Everywhere)
}
jsr223 {
importAllBindings(true)
}
}
)

private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())

fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> {
val annotations = context.collectedData?.get(ScriptCollectedData.collectedAnnotations)?.takeIf { it.isNotEmpty() }
?: return context.compilationConfiguration.asSuccess() // If no action is performed, the original configuration should be returned
return runBlocking {
resolver.resolveFromScriptSourceAnnotations(annotations)
}.onSuccess {
context.compilationConfiguration.with {
dependencies.append(JvmDependency(it))
}.asSuccess()
}
}
7 changes: 7 additions & 0 deletions bukkit-ext-kts/src/main/resources/example/greeter.poly.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.bukkit.Bukkit
import org.bukkit.event.player.PlayerJoinEvent

script.listen(PlayerJoinEvent::class.java) {
Bukkit.broadcastMessage("Hello, ${it.player.name}!")

}
8 changes: 8 additions & 0 deletions bukkit-ext-kts/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: PolyKts
version: ${project.version}
main: eu.okaeri.poly.bukkit.ext.PolyKotlinScriptExtension
description: Kotlin Script extension for Poly
website: https://github.com/OkaeriPoland/okaeri-poly
author: 'Bartosz Stefańczyk <[email protected]>'
api-version: 1.13
depend: [Poly]
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module>core-api</module>
<module>bukkit</module>
<module>bukkit-api</module>
<module>bukkit-ext-kts</module>
</modules>

<properties>
Expand All @@ -25,6 +26,7 @@
<library.graalvm.version>22.1.0.1</library.graalvm.version>
<library.groovy.version>3.0.11</library.groovy.version>
<library.jython.version>2.7.2</library.jython.version>
<library.kotlin.version>1.7.20</library.kotlin.version>
</properties>

<build>
Expand Down