From 1722a7dd82c5916d2d48d32a1547347ff8392408 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 30 Jun 2019 10:34:15 +0300 Subject: [PATCH] Slightly modified plugin lookup --- build.gradle.kts | 2 +- .../hep/dataforge/context/PluginManager.kt | 22 +++++++++++-------- .../hep/dataforge/output/OutputManager.kt | 7 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 3b494f3d..4fda7bec 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt index bcce8eb6..7c6175b3 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/PluginManager.kt @@ -51,7 +51,10 @@ class PluginManager(override val context: Context) : ContextAware, Iterable get(type: KClass, recursive: Boolean = true): T? = - get(recursive) { type.isInstance(it) } as T? + operator fun get(type: KClass, tag: PluginTag? = null, recursive: Boolean = true): T? = + get(recursive) { type.isInstance(it) && (tag == null || tag.matches(it.tag)) } as T? - inline fun get(recursive: Boolean = true): T? = get(T::class, recursive) + inline fun 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 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) @@ -115,13 +119,13 @@ class PluginManager(override val context: Context) : ContextAware, Iterable load(PluginRepository.fetch(tag,meta)) + loaded == null -> load(PluginRepository.fetch(tag, meta)) 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.") } } - fun load(factory: PluginFactory<*>, meta: Meta = EmptyMeta): Plugin{ + fun load(factory: PluginFactory<*>, meta: Meta = EmptyMeta): Plugin { val loaded = get(factory.tag, false) return when { loaded == null -> load(factory(meta)) @@ -135,7 +139,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable load(type: KClass, 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 getOrLoad(noinline metaBuilder: MetaBuilder.() -> Unit = {}): T { - return get(true) ?: load(metaBuilder) + return get(recursive = true) ?: load(metaBuilder) } } \ No newline at end of file diff --git a/dataforge-output/src/commonMain/kotlin/hep/dataforge/output/OutputManager.kt b/dataforge-output/src/commonMain/kotlin/hep/dataforge/output/OutputManager.kt index 47d9f59c..448b3adb 100644 --- a/dataforge-output/src/commonMain/kotlin/hep/dataforge/output/OutputManager.kt +++ b/dataforge-output/src/commonMain/kotlin/hep/dataforge/output/OutputManager.kt @@ -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.