Skip to content

Commit

Permalink
Script: Remove @file:DependsOn ,use @file:JvmName instead
Browse files Browse the repository at this point in the history
(see MythicMobs_Support.superitem.kts in src/main/resources for example)
(because DependsOn need other dependency)
  • Loading branch information
way-zer committed Jul 17, 2019
1 parent 6cca85a commit 1a91325
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/cf/wayzer/SuperItem/ItemManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object ItemManager {
val res = result.resultOrNull()!!.returnValue
if(res is ResultValue.Value && res.value is Item) {
item = (res.value as Item)
result.reports.forEachIndexed { index, rep ->
result.reports.filterNot { it.severity==ScriptDiagnostic.Severity.DEBUG }.forEachIndexed { index, rep ->
logger.log(Level.WARNING,"##$index##"+rep.message,rep.exception)
}
return@onSuccess ResultWithDiagnostics.Success(ResultValue.Unit)
Expand Down
44 changes: 8 additions & 36 deletions src/main/kotlin/cf/wayzer/SuperItem/ScriptSupporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,24 @@ package cf.wayzer.SuperItem
import cf.wayzer.SuperItem.scripts.SuperItemScript
import cf.wayzer.libraryManager.Dependency
import cf.wayzer.libraryManager.LibraryManager
import org.bukkit.Bukkit
import org.jetbrains.kotlin.script.util.DependsOn
import java.io.File
import java.nio.file.Paths
import kotlin.reflect.KClass
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.host.ScriptingHostConfiguration
import kotlin.script.experimental.host.configurationDependencies
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvm.util.classpathFromClass
import kotlin.script.experimental.jvm.JvmDependency
import kotlin.script.experimental.jvm.baseClassLoader
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.util.classpathFromClassloader
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

object ScriptSupporter {
private fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClass(classLoader: ClassLoader,vararg classes:KClass<out Any>) {
classes.flatMap {c->
classpathFromClass(classLoader,c)?:let {
val clp = "${c.java.canonicalName.replace('.', '/')}.class"
val url = classLoader.getResource(clp)
url?.toURI()?.schemeSpecificPart?.let { listOf(File(it.removePrefix("file:").split("!")[0])) }
}?: emptyList()
}.let(this::updateClasspath)
}

object Configuration:ScriptCompilationConfiguration({
jvm {
dependenciesFromClass(ScriptSupporter::class.java.classLoader,Bukkit::class,Item::class)
}
defaultImports(Item::class, DependsOn::class)
defaultImports.append("cf.wayzer.SuperItem.features.*")
refineConfiguration{
onAnnotations(DependsOn::class){ context->
val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.filter { it.annotationClass== DependsOn::class }?: listOf()
val classes = annotations.map { Class.forName((it as DependsOn).value).kotlin}
val diagnostics = classes.map { ScriptDiagnostic("[Info]PluginDependency: ${it.java.name}") }
ScriptCompilationConfiguration(context.compilationConfiguration) {
jvm{
dependenciesFromClass(ScriptSupporter::class.java.classLoader, *classes.toTypedArray())
}
}.asSuccess(diagnostics)
}
}
})

fun loadFile(f: File):ResultWithDiagnostics<EvaluationResult>{
LibraryManager(Paths.get("./libs/")).apply {
addMavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cf.wayzer.SuperItem.scripts

import cf.wayzer.SuperItem.Item
import cf.wayzer.SuperItem.ScriptSupporter
import org.bukkit.Bukkit
import java.io.File
import kotlin.reflect.KClass
import kotlin.script.experimental.api.*
import kotlin.script.experimental.jvm.JvmScriptCompilationConfigurationBuilder
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.updateClasspath
import kotlin.script.experimental.jvm.util.classpathFromClass

object CompilationConfiguration: ScriptCompilationConfiguration({
jvm {
dependenciesFromClass(ScriptSupporter::class.java.classLoader, Bukkit::class, Item::class)
}
defaultImports(Item::class, ImportClass::class)
defaultImports.append("cf.wayzer.SuperItem.features.*")
refineConfiguration{
onAnnotations(JvmName::class){ context->
val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.filter { it.annotationClass== JvmName::class }?: listOf()
val classes = annotations.map { Class.forName((it as JvmName).name).kotlin}
val diagnostics = classes.map { ScriptDiagnostic("[Info]PluginDependency: ${it.java.name}",ScriptDiagnostic.Severity.INFO) }
ScriptCompilationConfiguration(context.compilationConfiguration) {
jvm {
dependenciesFromClass(ScriptSupporter::class.java.classLoader, *classes.toTypedArray())
}
}.asSuccess(diagnostics)
}
}
})
private fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClass(classLoader: ClassLoader, vararg classes: KClass<out Any>) {
classes.flatMap {c->
classpathFromClass(classLoader,c) ?:let {
val clp = "${c.java.canonicalName.replace('.', '/')}.class"
val url = classLoader.getResource(clp)
url?.toURI()?.schemeSpecificPart?.let { listOf(File(it.removePrefix("file:").split("!")[0])) }
}?: emptyList()
}.let(this::updateClasspath)
}
6 changes: 6 additions & 0 deletions src/main/kotlin/cf/wayzer/SuperItem/scripts/ImportClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cf.wayzer.SuperItem.scripts

@Target(AnnotationTarget.FILE)
annotation class ImportClass(
val value:String
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cf.wayzer.SuperItem.scripts

import cf.wayzer.SuperItem.Item
import cf.wayzer.SuperItem.ScriptSupporter
import kotlin.script.experimental.annotations.KotlinScript

@KotlinScript(
displayName = "SuperItem Kotlin Script",
fileExtension = "superitem.kts",
compilationConfiguration = ScriptSupporter.Configuration::class
compilationConfiguration = CompilationConfiguration::class
)
abstract class SuperItemScript(name:String): Item.Builder("scripts",name)
open class SuperItemScript(name:String): Item.Builder("scripts",name)
2 changes: 1 addition & 1 deletion src/main/resources/Mythicmobs_Support.superitem.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:DependsOn("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent")
@file:JvmName("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent")

import cf.wayzer.SuperItem.ItemManager
import io.lumine.xikage.mythicmobs.adapters.bukkit.BukkitItemStack
Expand Down

0 comments on commit 1a91325

Please sign in to comment.