diff options
author | Krzysztof Romanwoski <kromanowski@virtuslab.com> | 2020-09-11 16:29:55 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-09-28 10:16:38 +0200 |
commit | db215639cef1a8ea4b0ac9761b9c23f7bac4e32d (patch) | |
tree | 136a0f825d6a7504bac232790db233c71b91b080 /core/src/main/kotlin | |
parent | aa745c95adde72a66ad559223c2055a15842736b (diff) | |
download | dokka-db215639cef1a8ea4b0ac9761b9c23f7bac4e32d.tar.gz dokka-db215639cef1a8ea4b0ac9761b9c23f7bac4e32d.tar.bz2 dokka-db215639cef1a8ea4b0ac9761b9c23f7bac4e32d.zip |
Create java-like api for plugins
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaJavaPlugin.kt | 68 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaPlugin.kt | 11 |
2 files changed, 76 insertions, 3 deletions
diff --git a/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt b/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt new file mode 100644 index 00000000..7c527abc --- /dev/null +++ b/core/src/main/kotlin/plugability/DokkaJavaPlugin.kt @@ -0,0 +1,68 @@ +package org.jetbrains.dokka.plugability + +import org.jetbrains.dokka.DokkaConfiguration + +class ExtensionBuilderStart internal constructor(){ + fun <T: Any> extensionPoint(ext: ExtensionPoint<T>): ProvidedExtension<T> = ProvidedExtension(ext) +} + +class ProvidedExtension<T: Any> internal constructor(val ext: ExtensionPoint<T>){ + fun fromInstance(inst: T): ExtensionBuilder<T> = createBuilder( + LazyEvaluated.fromInstance( + inst + ) + ) + fun fromRecipe(recipe: (DokkaContext) -> T): ExtensionBuilder<T> = createBuilder( + LazyEvaluated.fromRecipe(recipe) + ) + + private val defaultName = "${ext.pointName}/in/${javaClass.simpleName}" + + private fun createBuilder(action: LazyEvaluated<T>) = + ExtensionBuilder(defaultName, ext, action, + OrderingKind.None, + OverrideKind.None, emptyList()) +} + +data class ExtensionBuilder<T: Any> internal constructor( + private val name: String, + private val ext: ExtensionPoint<T>, + private val action: LazyEvaluated<T>, + private val ordering: OrderingKind = OrderingKind.None, + private val override: OverrideKind = OverrideKind.None, + private val conditions: List<(DokkaConfiguration) -> Boolean> +){ + fun build(): Extension<T, *, *> = Extension( + ext, + javaClass.name, + name, + action, + ordering, + override, + conditions + ) + + fun overrideExtension(extension: Extension<T, *, *>) = copy(override = OverrideKind.Present(extension)) + + fun newOrdering(before: Array<Extension<*, *, *>>, after: Array<Extension<*, *, *>>) { + copy(ordering = OrderingKind.ByDsl { + before(*before) + after(*after) + }) + } + + fun addCondition(c: (DokkaConfiguration) -> Boolean) = copy(conditions = conditions + c) + + fun name(name: String) = copy(name = name) +} + +abstract class DokkaJavaPlugin: DokkaPlugin() { + + fun <T: DokkaPlugin> plugin(clazz: Class<T>): T? = + context?.plugin(clazz.kotlin) ?: throwIllegalQuery() + + + fun <T: Any> extend(func: (ExtensionBuilderStart) -> ExtensionBuilder<T>): Lazy<Extension<T, *, *>> = + lazy { func(ExtensionBuilderStart()).build() }.also { unsafeInstall(it) } + +}
\ No newline at end of file diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index a62327d2..13f59f6e 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -10,6 +10,7 @@ import kotlin.reflect.full.createInstance abstract class DokkaPlugin { private val extensionDelegates = mutableListOf<KProperty<*>>() + private val unsafePlugins = mutableListOf<Lazy<Extension<*, *, *>>>() @PublishedApi internal var context: DokkaContext? = null @@ -37,10 +38,14 @@ abstract class DokkaPlugin { } internal fun internalInstall(ctx: DokkaContextConfiguration, configuration: DokkaConfiguration) { - extensionDelegates.asSequence() + val extensionsToInstall = extensionDelegates.asSequence() .filterIsInstance<KProperty1<DokkaPlugin, Extension<*, *, *>>>() // should be always true - .map { it.get(this) } - .forEach { if (configuration.(it.condition)()) ctx.installExtension(it) } + .map { it.get(this) } + unsafePlugins.map{ it.value } + extensionsToInstall.forEach { if (configuration.(it.condition)()) ctx.installExtension(it) } + } + + protected fun <T: Any> unsafeInstall(ext: Lazy<Extension<T, *, *>>){ + unsafePlugins.add(ext) } } |