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 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 bintrayRepo by extra("dataforge")
val vcs by extra("https://github.com/mipt-npm/dataforge-core") 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 tag
* @param type * @param type
@ -59,10 +62,11 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
* @return * @return
*/ */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
operator fun <T : Plugin> get(type: KClass<T>, recursive: Boolean = true): T? = operator fun <T : Any> get(type: KClass<T>, tag: PluginTag? = null, recursive: Boolean = true): T? =
get(recursive) { type.isInstance(it) } as 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 { fun <T : Plugin> load(plugin: T): T {
if (context.isActive) error("Can't load plugin into active context") 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}") throw RuntimeException("Plugin of type ${plugin::class} already exists in ${context.name}")
} else { } else {
loadDependencies(plugin) loadDependencies(plugin)
@ -115,13 +119,13 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
fun load(tag: PluginTag, meta: Meta = EmptyMeta): Plugin { fun load(tag: PluginTag, meta: Meta = EmptyMeta): Plugin {
val loaded = get(tag, false) val loaded = get(tag, false)
return when { return when {
loaded == null -> load(PluginRepository.fetch(tag,meta)) loaded == null -> load(PluginRepository.fetch(tag, meta))
loaded.meta == meta -> loaded // if meta is the same, return existing plugin loaded.meta == meta -> loaded // if meta is the same, return existing plugin
else -> throw RuntimeException("Can't load plugin with tag $tag. Plugin with this tag and different configuration already exists in context.") else -> throw RuntimeException("Can't load plugin with tag $tag. Plugin with this tag and different configuration already exists in context.")
} }
} }
fun load(factory: PluginFactory<*>, meta: Meta = EmptyMeta): Plugin{ fun load(factory: PluginFactory<*>, meta: Meta = EmptyMeta): Plugin {
val loaded = get(factory.tag, false) val loaded = get(factory.tag, false)
return when { return when {
loaded == null -> load(factory(meta)) loaded == null -> load(factory(meta))
@ -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 * 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 { fun <T : Plugin> load(type: KClass<T>, meta: Meta = EmptyMeta): T {
val loaded = get(type, false) val loaded = get(type, recursive = false)
return when { return when {
loaded == null -> { loaded == null -> {
val plugin = PluginRepository.list().first { it.type == type }.invoke(meta) 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. * 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 { 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 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.context.PluginTag.Companion.DATAFORGE_GROUP
import hep.dataforge.meta.EmptyMeta import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
@ -13,7 +16,7 @@ import kotlin.reflect.KClass
/** /**
* A manager for outputs * A manager for outputs
*/ */
interface OutputManager : Plugin { interface OutputManager {
/** /**
* Get an output specialized for given type, name and stage. * Get an output specialized for given type, name and stage.