Removed configuration from plugins.

This commit is contained in:
Alexander Nozik 2019-05-01 16:46:09 +03:00
parent a56fa74ab6
commit f284d15ee4
8 changed files with 24 additions and 32 deletions

View File

@ -1,4 +1,4 @@
val dataforgeVersion by extra("0.1.2-dev-6")
val dataforgeVersion by extra("0.1.2-dev-7")
allprojects {
repositories {

View File

@ -1,16 +1,15 @@
package hep.dataforge.context
import hep.dataforge.meta.Config
import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta
import hep.dataforge.names.Name
abstract class AbstractPlugin : Plugin {
abstract class AbstractPlugin(override val meta: Meta = EmptyMeta) : Plugin {
private var _context: Context? = null
override val context: Context
get() = _context ?: error("Plugin $tag is not attached")
override val config = Config()
override fun attach(context: Context) {
this._context = context
}
@ -19,8 +18,6 @@ abstract class AbstractPlugin : Plugin {
this._context = null
}
//TODO make configuration activation-safe
override fun provideTop(target: String, name: Name): Any? = null
override fun listTop(target: String): Sequence<Name> = emptySequence()

View File

@ -1,8 +1,7 @@
package hep.dataforge.context
import hep.dataforge.meta.Config
import hep.dataforge.meta.MetaBuilder
import hep.dataforge.meta.configure
import hep.dataforge.meta.buildMeta
/**
* A convenience builder for context
@ -19,11 +18,11 @@ class ContextBuilder(var name: String = "@anonimous", val parent: Context = Glob
plugins.add(plugin)
}
fun plugin(tag: PluginTag, action: Config.() -> Unit) {
plugins.add(PluginRepository.fetch(tag).configure(action))
fun plugin(tag: PluginTag, action: MetaBuilder.() -> Unit) {
plugins.add(PluginRepository.fetch(tag, buildMeta(action)))
}
fun plugin(name: String, group: String = "", version: String = "", action: Config.() -> Unit) {
fun plugin(name: String, group: String = "", version: String = "", action: MetaBuilder.() -> Unit) {
plugin(PluginTag(name, group, version), action)
}

View File

@ -1,6 +1,5 @@
package hep.dataforge.context
import hep.dataforge.meta.Configurable
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaRepr
import hep.dataforge.meta.buildMeta
@ -22,7 +21,7 @@ import hep.dataforge.provider.Provider
*
* @author Alexander Nozik
*/
interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable {
interface Plugin : Named, ContextAware, Provider, MetaRepr {
/**
* Get tag for this plugin
@ -31,13 +30,14 @@ interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable {
*/
val tag: PluginTag
val meta: Meta
/**
* The name of this plugin ignoring version and group
*
* @return
*/
override val name: String
get() = tag.name
override val name: String get() = tag.name
/**
* Plugin dependencies which are required to attach this plugin. Plugin
@ -67,7 +67,7 @@ interface Plugin : Named, ContextAware, Provider, MetaRepr, Configurable {
"context" to context.name
"type" to this::class.simpleName
"tag" to tag
"meta" to config
"meta" to meta
}
companion object {

View File

@ -116,7 +116,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
val loaded = get(tag, false)
return when {
loaded == null -> load(PluginRepository.fetch(tag,meta))
loaded.config == 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.")
}
}
@ -125,7 +125,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
val loaded = get(factory.tag, false)
return when {
loaded == null -> load(factory(meta))
loaded.config == 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 ${factory.tag}. Plugin with this tag and different configuration already exists in context.")
}
}
@ -146,7 +146,7 @@ class PluginManager(override val context: Context) : ContextAware, Iterable<Plug
error("Corrupt type information in plugin repository")
}
}
loaded.config == 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 type $type. Plugin with this type and different configuration already exists in context.")
}
}

View File

@ -2,18 +2,14 @@ package hep.dataforge.context
import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta
import hep.dataforge.meta.configure
import kotlin.reflect.KClass
interface PluginFactory<T : Plugin> {
val tag: PluginTag
val type: KClass<out T>
operator fun invoke(): T
operator fun invoke(meta: Meta = EmptyMeta): T
}
operator fun PluginFactory<*>.invoke(meta: Meta) = invoke().configure(meta)
expect object PluginRepository {
fun register(factory: PluginFactory<*>)
@ -29,26 +25,25 @@ expect object PluginRepository {
* Fetch specific plugin and instantiate it with given meta
*/
fun PluginRepository.fetch(tag: PluginTag, meta: Meta = EmptyMeta): Plugin =
PluginRepository.list().find { it.tag.matches(tag) }?.invoke(meta)
?: error("Plugin with tag $tag not found in the repository")
list().find { it.tag.matches(tag) }?.invoke(meta) ?: error("Plugin with tag $tag not found in the repository")
fun <T : Plugin> PluginRepository.register(
tag: PluginTag,
type: KClass<out T>,
constructor: () -> T
constructor: (Meta) -> T
): PluginFactory<T> {
val factory = object : PluginFactory<T> {
override val tag: PluginTag = tag
override val type: KClass<out T> = type
override fun invoke(): T = constructor()
override fun invoke(meta: Meta): T = constructor(meta)
}
PluginRepository.register(factory)
register(factory)
return factory
}
inline fun <reified T : Plugin> PluginRepository.register(tag: PluginTag, noinline constructor: () -> T) =
inline fun <reified T : Plugin> PluginRepository.register(tag: PluginTag, noinline constructor: (Meta) -> T) =
register(tag, T::class, constructor)
fun PluginRepository.register(plugin: Plugin) = register(plugin.tag, plugin::class) { plugin }

View File

@ -61,6 +61,7 @@ data class NameToken(val body: String, val index: String = "") {
}
fun String.toName(): Name {
if (isBlank()) return EmptyName
val tokens = sequence {
var bodyBuilder = StringBuilder()
var queryBuilder = StringBuilder()

View File

@ -67,7 +67,7 @@ class ConsoleOutputManager : AbstractPlugin(), OutputManager {
override val type = ConsoleOutputManager::class
override fun invoke() = ConsoleOutputManager()
override fun invoke(meta:Meta) = ConsoleOutputManager()
}
}