Task builder for WorkspacePlugin

This commit is contained in:
Alexander Nozik 2019-09-24 17:35:06 +03:00
parent 71536051aa
commit 337401f6e0
4 changed files with 46 additions and 12 deletions

View File

@ -65,6 +65,11 @@ fun Meta.builder(): MetaBuilder {
} }
} }
/**
* Create a deep copy of this meta and apply builder to it
*/
fun Meta.edit(builder: MetaBuilder.() -> Unit): MetaBuilder = builder().apply(builder)
/** /**
* Build a [MetaBuilder] using given transformation * Build a [MetaBuilder] using given transformation
*/ */

View File

@ -242,11 +242,3 @@ class TaskBuilder<R : Any>(val name: Name, val type: KClass<out R>) {
} }
} }
fun <T : Any> Workspace.Companion.task(
name: String,
type: KClass<out T>,
builder: TaskBuilder<T>.() -> Unit
): GenericTask<T> = TaskBuilder(name.toName(), type).apply(builder).build()
//TODO add delegates to build gradle-like tasks

View File

@ -3,17 +3,40 @@ package hep.dataforge.workspace
import hep.dataforge.context.AbstractPlugin import hep.dataforge.context.AbstractPlugin
import hep.dataforge.context.toMap import hep.dataforge.context.toMap
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName
import kotlin.reflect.KClass
/** /**
* An abstract plugin with some additional boilerplate to effectively work with workspace context * An abstract plugin with some additional boilerplate to effectively work with workspace context
*/ */
abstract class WorkspacePlugin : AbstractPlugin() { abstract class WorkspacePlugin : AbstractPlugin() {
abstract val tasks: Collection<Task<*>> private val _tasks = HashSet<Task<*>>()
val tasks: Collection<Task<*>> get() = _tasks
override fun provideTop(target: String): Map<Name, Any> { override fun provideTop(target: String): Map<Name, Any> {
return when(target){ return when (target) {
Task.TYPE -> tasks.toMap() Task.TYPE -> tasks.toMap()
else -> emptyMap() else -> emptyMap()
} }
} }
fun task(task: Task<*>){
_tasks.add(task)
}
fun <T : Any> task(
name: String,
type: KClass<out T>,
builder: TaskBuilder<T>.() -> Unit
): GenericTask<T> = TaskBuilder(name.toName(), type).apply(builder).build().also {
_tasks.add(it)
}
inline fun <reified T : Any> task(
name: String,
noinline builder: TaskBuilder<T>.() -> Unit
) = task(name, T::class, builder)
//
////TODO add delegates to build gradle-like tasks
} }

View File

@ -3,7 +3,10 @@ package hep.dataforge.workspace
import hep.dataforge.context.PluginTag import hep.dataforge.context.PluginTag
import hep.dataforge.data.* import hep.dataforge.data.*
import hep.dataforge.meta.boolean import hep.dataforge.meta.boolean
import hep.dataforge.meta.builder
import hep.dataforge.meta.get import hep.dataforge.meta.get
import hep.dataforge.meta.int
import hep.dataforge.names.plus
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -13,12 +16,11 @@ class SimpleWorkspaceTest {
val testPlugin = object : WorkspacePlugin() { val testPlugin = object : WorkspacePlugin() {
override val tag: PluginTag = PluginTag("test") override val tag: PluginTag = PluginTag("test")
val contextTask = Workspace.task("test", Any::class) { val contextTask = task("test", Any::class) {
pipe<Any> { pipe<Any> {
context.logger.info { "Test: $it" } context.logger.info { "Test: $it" }
} }
} }
override val tasks: Collection<Task<*>> = listOf(contextTask)
} }
val workspace = Workspace { val workspace = Workspace {
@ -108,6 +110,18 @@ class SimpleWorkspaceTest {
} }
} }
val customPipeTask = task<Int>("custom") {
customPipe<Int> {
meta = meta.builder().apply {
"newValue" to 22
}
name += "new"
result {
meta["value"].int ?: 0 + it
}
}
}
target("empty") {} target("empty") {}
} }