Upgrade to OPENRNDR 0.4 snapshot

This commit is contained in:
Edwin Jakobs
2021-06-22 11:08:07 +02:00
parent 579ddf9bb5
commit 9435907ef9
339 changed files with 460 additions and 497 deletions

View File

@@ -0,0 +1,44 @@
package org.openrndr.extra.olive
import org.openrndr.ApplicationBuilder
import org.openrndr.Program
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.streams.toList
open class OliveProgram(private val sourceLocation: String, private val scriptHost: OliveScriptHost, resources: Resources?) : Program() {
val olive = extend(Olive<OliveProgram>(scriptMode = ScriptMode.OLIVE_PROGRAM, resources = resources)) {
script = sourceLocation
scriptHost = this@OliveProgram.scriptHost
}
}
fun stackRootClassName(thread: Thread = Thread.currentThread(), sanitize: Boolean = true): String {
val root = Thread.currentThread().stackTrace.last()
val rootClass = root.className
return if (sanitize) rootClass.replace(Regex("Kt$"), "") else rootClass
}
fun ApplicationBuilder.oliveProgram(scriptHost: OliveScriptHost = OliveScriptHost.JSR223_REUSE, resources: Resources? = null, init: OliveProgram.() -> Unit): OliveProgram {
val rootClassName = stackRootClassName(sanitize = true).split(".").last()
var sourceLocation = "src/main/kotlin/$rootClassName.kt"
val candidateFile = File(sourceLocation)
if (!candidateFile.exists()) {
val otherCandidates = Files.walk(Paths.get("."))
.filter { Files.isRegularFile(it) && it.toString().endsWith("$rootClassName.kt") }.toList()
if (otherCandidates.size == 1) {
sourceLocation = otherCandidates.first().toString()
} else {
error("multiple source candidates found: $otherCandidates")
}
}
program = object : OliveProgram(sourceLocation, scriptHost, resources) {
override fun setup() {
super.setup()
init()
}
}
return program as OliveProgram
}