You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.5 KiB
Kotlin
64 lines
2.5 KiB
Kotlin
4 years ago
|
package me.eater.hefbrug.executor
|
||
|
|
||
|
import me.eater.hefbrug.dsl.Location
|
||
|
import me.eater.hefbrug.dsl.context.RootContext
|
||
|
import me.eater.hefbrug.dsl.context.SourceContext
|
||
|
import me.eater.hefbrug.dsl.context.extension_util.Register
|
||
|
import me.eater.hefbrug.dsl.scope.SelectorScope
|
||
|
import me.eater.hefbrug.logging.Logging
|
||
|
import org.apache.logging.log4j.Level
|
||
|
import org.apache.logging.log4j.MarkerManager
|
||
|
import java.io.File
|
||
|
import java.nio.file.Paths
|
||
|
import kotlin.script.experimental.api.ScriptDiagnostic
|
||
|
import kotlin.script.experimental.api.constructorArgs
|
||
|
import kotlin.script.experimental.api.implicitReceivers
|
||
|
import kotlin.script.experimental.host.toScriptSource
|
||
|
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
|
||
|
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
|
||
|
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate
|
||
|
|
||
|
class Executor(val context: RootContext = RootContext()) : Logging {
|
||
|
private val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<HefbrugScript>()
|
||
|
|
||
|
fun run(name: String) {
|
||
|
val location = Location.File(Paths.get(name).toRealPath().toString())
|
||
|
val evaluationConfiguration = createJvmEvaluationConfigurationFromTemplate<HefbrugScript> {
|
||
|
constructorArgs(context, location)
|
||
|
implicitReceivers(SourceContext(context, location, this@Executor))
|
||
|
}
|
||
|
|
||
|
val diags =
|
||
|
BasicJvmScriptingHost().eval(
|
||
|
File(location.path).toScriptSource(),
|
||
|
compilationConfiguration,
|
||
|
evaluationConfiguration
|
||
|
)
|
||
|
|
||
|
for (d in diags.reports) {
|
||
|
|
||
|
log(
|
||
|
when (d.severity) {
|
||
|
ScriptDiagnostic.Severity.DEBUG -> Level.forName("SCRIPT", 700)
|
||
|
ScriptDiagnostic.Severity.INFO -> Level.DEBUG
|
||
|
ScriptDiagnostic.Severity.WARNING -> Level.INFO
|
||
|
ScriptDiagnostic.Severity.ERROR -> Level.ERROR
|
||
|
ScriptDiagnostic.Severity.FATAL -> Level.FATAL
|
||
|
},
|
||
|
MarkerManager.getMarker("script"),
|
||
|
"[@|blue ${location.path}|@] ${d.message} ${d.location ?: ""}"
|
||
|
)
|
||
|
|
||
|
if (d.exception != null) {
|
||
|
d.exception!!.printStackTrace()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fun getScope(): SelectorScope =
|
||
|
Register[context.contextUUID].selectorScope
|
||
|
|
||
|
init {
|
||
|
Register.register(Register(context.contextUUID))
|
||
|
}
|
||
|
}
|