Slightly modified plugin lookup

This commit is contained in:
Alexander Nozik 2019-06-30 10:34:15 +03:00
parent b13d980c02
commit 1722a7dd82
3 changed files with 19 additions and 12 deletions

View File

@ -3,7 +3,7 @@ plugins {
id("scientifik.publish") apply false
}
val dataforgeVersion by extra("0.1.3-dev-7")
val dataforgeVersion by extra("0.1.3-dev-8")
val bintrayRepo by extra("dataforge")
val vcs by extra("https://github.com/mipt-npm/dataforge-core")

View File

@ -51,7 +51,10 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
/**
* Find a loaded plugin via its class
* Find a loaded plugin via its class. This method does not check if the result is unique and just returns first
* plugin matching the class condition.
* For safe search provide a tag since tags are checked on load and plugins with the same tag are not allowed
* in the same context.
*
* @param tag
* @param type
@ -59,10 +62,11 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
* @return
*/
@Suppress("UNCHECKED_CAST")
operator fun <T : Plugin> get(type: KClass<T>, recursive: Boolean = true): T? =
get(recursive) { type.isInstance(it) } as T?
operator fun <T : Any> get(type: KClass<T>, tag: PluginTag? = null, recursive: Boolean = true): T? =
get(recursive) { type.isInstance(it) && (tag == null || tag.matches(it.tag)) } as T?
inline fun <reified T : Plugin> get(recursive: Boolean = true): T? = get(T::class, recursive)
inline fun <reified T : Any> get(tag: PluginTag? = null, recursive: Boolean = true): T? =
get(T::class, tag, recursive)
/**
@ -75,7 +79,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
fun <T : Plugin> load(plugin: T): T {
if (context.isActive) error("Can't load plugin into active context")
if (get(plugin::class, false) != null) {
if (get(plugin::class, recursive = false) != null) {
throw RuntimeException("Plugin of type ${plugin::class} already exists in ${context.name}")
} else {
loadDependencies(plugin)
@ -135,7 +139,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
* Throw an exception if there exists plugin with the same type, but different meta
*/
fun <T : Plugin> load(type: KClass<T>, meta: Meta = EmptyMeta): T {
val loaded = get(type, false)
val loaded = get(type, recursive = false)
return when {
loaded == null -> {
val plugin = PluginRepository.list().first { it.type == type }.invoke(meta)
@ -165,7 +169,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
* Get a plugin if it exists or load it with given meta if it is not.
*/
inline fun <reified T : Plugin> getOrLoad(noinline metaBuilder: MetaBuilder.() -> Unit = {}): T {
return get(true) ?: load(metaBuilder)
return get(recursive = true) ?: load(metaBuilder)
}
}

View File

@ -1,6 +1,9 @@
package hep.dataforge.output
import hep.dataforge.context.*
import hep.dataforge.context.AbstractPlugin
import hep.dataforge.context.Context
import hep.dataforge.context.PluginFactory
import hep.dataforge.context.PluginTag
import hep.dataforge.context.PluginTag.Companion.DATAFORGE_GROUP
import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta
@ -13,7 +16,7 @@ import kotlin.reflect.KClass
/**
* A manager for outputs
*/
interface OutputManager : Plugin {
interface OutputManager {
/**
* Get an output specialized for given type, name and stage.