diff options
Diffstat (limited to 'core/src/main')
-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) } } |