diff options
Diffstat (limited to 'core/src/main/kotlin/plugability')
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaContext.kt | 32 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaPlugin.kt | 11 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/extensions.kt | 28 |
3 files changed, 48 insertions, 23 deletions
diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index 6a8ac4a3..b4be5862 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -17,6 +17,8 @@ interface DokkaContext { operator fun <T, E> get(point: E, askDefault: AskDefault = AskDefault.WhenEmpty): List<T> where T : Any, E : ExtensionPoint<T> + fun <T, E> single(point: E): T where T : Any, E : ExtensionPoint<T> + val logger: DokkaLogger val configuration: DokkaConfiguration val platforms: Map<PlatformData, EnvironmentAndFacade> @@ -41,19 +43,6 @@ interface DokkaContext { } } -fun <T, E> DokkaContext.single(point: E): T where T : Any, E : ExtensionPoint<T> { - fun throwBadArity(substitution: String): Nothing = throw IllegalStateException( - "$point was expected to have exactly one extension registered, but $substitution found." - ) - - val extensions = get(point, AskDefault.WhenEmpty) - return when (extensions.size) { - 0 -> throwBadArity("none was") - 1 -> extensions.first() - else -> throwBadArity("multiple were") - } -} - interface DokkaContextConfiguration { fun addExtensionDependencies(extension: Extension<*>) } @@ -110,6 +99,23 @@ private class DokkaContextConfigurationImpl( actions(point)?.takeIf { it.isNotEmpty() } ?: DefaultExtensions.get(point, this) } as List<T> + @Suppress("UNCHECKED_CAST") + override fun <T, E> single(point: E): T where T : Any, E : ExtensionPoint<T> { + fun throwBadArity(substitution: String): Nothing = throw IllegalStateException( + "$point was expected to have exactly one extension registered, but $substitution found." + ) + + val extensions = extensions[point].orEmpty() as List<Extension<T>> + return when (extensions.size) { + 0 -> DefaultExtensions.get(point, this).single() ?: throwBadArity("none was") + 1 -> extensions.single().action.get(this) + else -> { + val notFallbacks = extensions.filterNot { it.isFallback } + if (notFallbacks.size == 1) notFallbacks.single().action.get(this) else throwBadArity("many were") + } + } + } + private fun <E : ExtensionPoint<*>> actions(point: E) = extensions[point]?.map { it.action.get(this) } @Suppress("UNCHECKED_CAST") diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index 1fcbd934..cdc92ca5 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -14,7 +14,7 @@ abstract class DokkaPlugin { internal var context: DokkaContext? = null protected inline fun <reified T : DokkaPlugin> plugin(): T = context?.plugin(T::class) - ?: throw IllegalStateException("Querying about plugins is only possible with dokka context initialised") + ?: throw IllegalStateException("Querying about plugins is only possible with dokka context initialised") protected fun <T : Any> extensionPoint() = object : ReadOnlyProperty<DokkaPlugin, ExtensionPoint<T>> { @@ -24,7 +24,12 @@ abstract class DokkaPlugin { ) } - protected fun <T : Any> extending(definition: ExtendingDSL.() -> Extension<T>) = ExtensionProvider(definition) + protected fun <T : Any> extending(isFallback: Boolean = false, definition: ExtendingDSL.() -> Extension<T>) = + if (isFallback) { + ExtensionProvider { definition().markedAsFallback() } + } else { + ExtensionProvider(definition) + } protected class ExtensionProvider<T : Any> internal constructor( private val definition: ExtendingDSL.() -> Extension<T> @@ -41,6 +46,6 @@ abstract class DokkaPlugin { extensionDelegates.asSequence() .filterIsInstance<KProperty1<DokkaPlugin, Extension<*>>>() // should be always true .map { it.get(this) } - .forEach { if(it.condition.invoke(configuration)) ctx.addExtensionDependencies(it) } + .forEach { if (it.condition.invoke(configuration)) ctx.addExtensionDependencies(it) } } }
\ No newline at end of file diff --git a/core/src/main/kotlin/plugability/extensions.kt b/core/src/main/kotlin/plugability/extensions.kt index 4cbb61eb..46c356df 100644 --- a/core/src/main/kotlin/plugability/extensions.kt +++ b/core/src/main/kotlin/plugability/extensions.kt @@ -15,7 +15,8 @@ abstract class Extension<T : Any> internal constructor( internal val extensionName: String, internal val action: LazyEvaluated<T>, internal val ordering: (OrderDsl.() -> Unit)? = null, - internal val condition: DokkaConfiguration.() -> Boolean = { true } + internal val condition: DokkaConfiguration.() -> Boolean = { true }, + internal val isFallback: Boolean ) { override fun toString() = "Extension: $pluginClass/$extensionName" @@ -26,6 +27,8 @@ abstract class Extension<T : Any> internal constructor( override fun hashCode() = listOf(pluginClass, extensionName).hashCode() abstract fun setCondition(condition: (DokkaConfiguration.() -> Boolean)): Extension<T> + + abstract fun markedAsFallback(): Extension<T> } class ExtensionOrdered<T : Any> internal constructor( @@ -34,17 +37,22 @@ class ExtensionOrdered<T : Any> internal constructor( extensionName: String, action: LazyEvaluated<T>, ordering: (OrderDsl.() -> Unit), - condition: DokkaConfiguration.() -> Boolean = { true } + condition: DokkaConfiguration.() -> Boolean = { true }, + isFallback: Boolean = false ) : Extension<T>( extensionPoint, pluginClass, extensionName, action, ordering, - condition + condition, + isFallback ) { override fun setCondition(condition: DokkaConfiguration.() -> Boolean) = ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, ordering!!, condition) + + override fun markedAsFallback() = + ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, ordering!!, condition, true) } class ExtensionUnordered<T : Any> internal constructor( @@ -52,17 +60,22 @@ class ExtensionUnordered<T : Any> internal constructor( pluginClass: String, extensionName: String, action: LazyEvaluated<T>, - condition: DokkaConfiguration.() -> Boolean = { true } + condition: DokkaConfiguration.() -> Boolean = { true }, + isFallback: Boolean = false ) : Extension<T>( extensionPoint, pluginClass, extensionName, action, null, - condition + condition, + isFallback ) { override fun setCondition(condition: DokkaConfiguration.() -> Boolean) = ExtensionUnordered(extensionPoint, pluginClass, extensionName, action, condition) + + override fun markedAsFallback() = + ExtensionUnordered(extensionPoint, pluginClass, extensionName, action, condition, true) } internal data class Ordering(val previous: Set<Extension<*>>, val following: Set<Extension<*>>) @@ -79,10 +92,11 @@ class ExtendingDSL(private val pluginClass: String, private val extensionName: S infix fun <T : Any> ExtensionPoint<T>.providing(action: (DokkaContext) -> T) = ExtensionUnordered(this, this@ExtendingDSL.pluginClass, extensionName, LazyEvaluated.fromRecipe(action)) - infix fun <T: Any> ExtensionUnordered<T>.order(block: OrderDsl.() -> Unit) = + infix fun <T : Any> ExtensionUnordered<T>.order(block: OrderDsl.() -> Unit) = ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, block) - infix fun <T : Any> Extension<T>.applyIf(condition: DokkaConfiguration.() -> Boolean): Extension<T> = this.setCondition(condition) + infix fun <T : Any> Extension<T>.applyIf(condition: DokkaConfiguration.() -> Boolean): Extension<T> = + this.setCondition(condition) } @ExtensionsDsl |