Merge pull request #13 from ricardomatias/olive
Add watching to resources folder
This commit is contained in:
@@ -18,7 +18,7 @@ private fun <T> Event<T>.restoreListeners(store: Map<Event<*>, List<(Any) -> Uni
|
||||
listeners.retainAll(store[this] ?: emptyList<T>())
|
||||
}
|
||||
|
||||
class Olive<P : Program> : Extension {
|
||||
class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
||||
override var enabled: Boolean = true
|
||||
var session: Session? = null
|
||||
|
||||
@@ -110,5 +110,26 @@ class Olive<P : Program> : Extension {
|
||||
}
|
||||
setupScript(script)
|
||||
scriptChange = ::setupScript
|
||||
|
||||
if (resources != null) {
|
||||
val srcPath = "src/main/resources"
|
||||
var src = File(srcPath)
|
||||
|
||||
resources.watch(src) { file ->
|
||||
val dest = "build/resources/main"
|
||||
val filePath = file.path.split(Regex(srcPath), 2).getOrNull(1)
|
||||
|
||||
val destFile = File("$dest/${filePath}").absoluteFile
|
||||
|
||||
program.watchFile(file) {
|
||||
if (resources[file]!! && filePath != null) {
|
||||
file.copyTo(destFile, overwrite = true)
|
||||
reload()
|
||||
} else {
|
||||
resources[file] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
29
orx-olive/src/main/kotlin/Resources.kt
Normal file
29
orx-olive/src/main/kotlin/Resources.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
package org.openrndr.extra.olive
|
||||
|
||||
import java.io.File
|
||||
|
||||
class Resources(val filterOutExtensions: List<String> = listOf()) {
|
||||
private val watchedResources = mutableMapOf<File, Boolean>()
|
||||
|
||||
fun watch(src: File, watchFn: (file: File) -> Unit) {
|
||||
src.listFiles()!!.forEach {file ->
|
||||
if (file.isFile && !filterOutExtensions.contains(file.extension)) {
|
||||
watchedResources[file] = false
|
||||
|
||||
watchFn(file)
|
||||
} else if (file.isDirectory) {
|
||||
watch(file, watchFn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
operator fun get(file: File): Boolean? {
|
||||
return watchedResources[file]
|
||||
}
|
||||
|
||||
operator fun set(file: File, value: Boolean) {
|
||||
if (watchedResources.containsKey(file)) {
|
||||
watchedResources[file] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user