diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2019-12-13 14:27:25 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-01-16 11:55:53 +0100 |
commit | 0917459a2eafabc563e79579a0322bb7832cae1f (patch) | |
tree | a5ac67c7595232a28af27716711bee644d3a12f3 | |
parent | 9e69d2db272b2ebe9317b88ce222473c4df355af (diff) | |
download | dokka-0917459a2eafabc563e79579a0322bb7832cae1f.tar.gz dokka-0917459a2eafabc563e79579a0322bb7832cae1f.tar.bz2 dokka-0917459a2eafabc563e79579a0322bb7832cae1f.zip |
Refactor of DokkaGenerator pipeline, removed ExtensionPoints dependencies other than context
-rw-r--r-- | core/src/main/kotlin/CoreExtensions.kt | 13 | ||||
-rw-r--r-- | core/src/main/kotlin/DokkaGenerator.kt | 16 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/DefaultExtensions.kt | 18 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/DokkaContext.kt | 17 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/LazyEvaluated.kt | 16 | ||||
-rw-r--r-- | core/src/main/kotlin/plugability/extensions.kt | 18 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/DefaultRenderer.kt | 14 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/FileWriter.kt | 4 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/HtmlRenderer.kt | 9 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/OutputWriter.kt | 6 | ||||
-rw-r--r-- | core/src/main/kotlin/resolvers/DefaultLocationProvider.kt | 5 | ||||
-rw-r--r-- | core/src/main/kotlin/resolvers/LocationProviderFactory.kt | 13 | ||||
-rw-r--r-- | core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt | 2 | ||||
-rw-r--r-- | plugins/xml/src/main/kotlin/XmlPlugin.kt | 2 |
14 files changed, 96 insertions, 57 deletions
diff --git a/core/src/main/kotlin/CoreExtensions.kt b/core/src/main/kotlin/CoreExtensions.kt index 191a6c59..c1871118 100644 --- a/core/src/main/kotlin/CoreExtensions.kt +++ b/core/src/main/kotlin/CoreExtensions.kt @@ -1,12 +1,10 @@ package org.jetbrains.dokka import org.jetbrains.dokka.pages.CommentsToContentConverter -import org.jetbrains.dokka.pages.PageNode -import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.ExtensionPoint import org.jetbrains.dokka.renderers.Renderer -import org.jetbrains.dokka.renderers.Writer -import org.jetbrains.dokka.resolvers.LocationProvider +import org.jetbrains.dokka.renderers.OutputWriter +import org.jetbrains.dokka.resolvers.LocationProviderFactory import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentationTranslator import org.jetbrains.dokka.transformers.documentation.DocumentationNodeMerger import org.jetbrains.dokka.transformers.documentation.DocumentationNodeTransformer @@ -23,11 +21,12 @@ object CoreExtensions { val descriptorToDocumentationTranslator by coreExtension<DescriptorToDocumentationTranslator>() val documentationMerger by coreExtension<DocumentationNodeMerger>() val documentationTransformer by coreExtension<DocumentationNodeTransformer>() - val commentsToContentConverterFactory by coreExtension<(DokkaContext) -> CommentsToContentConverter>() + val commentsToContentConverter by coreExtension<CommentsToContentConverter>() val documentationToPageTranslator by coreExtension<DocumentationToPageTranslator>() val pageTransformer by coreExtension<PageNodeTransformer>() - val rendererFactory by coreExtension<(Writer, LocationProvider, DokkaContext) -> Renderer>() - val locationProviderFactory by coreExtension<(root: PageNode, DokkaConfiguration, DokkaContext) -> LocationProvider>() + val locationProviderFactory by coreExtension<LocationProviderFactory>() + val outputWriter by coreExtension<OutputWriter>() + val renderer by coreExtension<Renderer>() val fileExtension by coreExtension<String>() private fun <T: Any> coreExtension() = object { diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index 0812dd5b..06de6c27 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -30,7 +30,7 @@ class DokkaGenerator( val platforms: Map<PlatformData, EnvironmentAndFacade> = setUpAnalysis(configuration) logger.progress("Initializing plugins") - val context = initializePlugins(configuration.pluginsClasspath, logger, platforms) + val context = initializePlugins(configuration, logger, platforms) logger.progress("Creating documentation models") val modulesFromPlatforms = createDocumentationModels(platforms, context) @@ -48,7 +48,7 @@ class DokkaGenerator( val transformedPages = transformPages(pages, context) logger.progress("Rendering") - render(transformedPages, configuration, context) + render(transformedPages, context) } internal fun setUpAnalysis(configuration: DokkaConfiguration): Map<PlatformData, EnvironmentAndFacade> = @@ -57,10 +57,10 @@ class DokkaGenerator( }.toMap() internal fun initializePlugins( - pluginsClasspath: List<File>, + configuration: DokkaConfiguration, logger: DokkaLogger, platforms: Map<PlatformData, EnvironmentAndFacade> - ) = DokkaContext.create(pluginsClasspath, logger, platforms) + ) = DokkaContext.create(configuration, logger, platforms) internal fun createDocumentationModels( platforms: Map<PlatformData, EnvironmentAndFacade>, @@ -89,15 +89,9 @@ class DokkaGenerator( internal fun render( transformedPages: ModulePageNode, - configuration: DokkaConfiguration, context: DokkaContext ) { - val fileWriter = FileWriter(configuration.outputDir, "") - val locationProvider = context.single(CoreExtensions.locationProviderFactory) - .invoke(transformedPages, configuration, context) - val renderer = context.single(CoreExtensions.rendererFactory) - .invoke(fileWriter, locationProvider, context) - + val renderer = context.single(CoreExtensions.renderer) renderer.render(transformedPages) } diff --git a/core/src/main/kotlin/plugability/DefaultExtensions.kt b/core/src/main/kotlin/plugability/DefaultExtensions.kt index 86ca5ca8..58b686f3 100644 --- a/core/src/main/kotlin/plugability/DefaultExtensions.kt +++ b/core/src/main/kotlin/plugability/DefaultExtensions.kt @@ -2,23 +2,25 @@ package org.jetbrains.dokka.plugability import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.pages.DocTagToContentConverter +import org.jetbrains.dokka.renderers.FileWriter import org.jetbrains.dokka.renderers.HtmlRenderer -import org.jetbrains.dokka.resolvers.DefaultLocationProvider +import org.jetbrains.dokka.resolvers.DefaultLocationProviderFactory import org.jetbrains.dokka.transformers.descriptors.DefaultDescriptorToDocumentationTranslator import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationNodeMerger import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationToPageTranslator -object DefaultExtensions : DokkaExtensionHandler { +internal object DefaultExtensions { @Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST") - override fun <T : Any, E : ExtensionPoint<T>> get(point: E, askDefault: AskDefault): List<T> = + internal fun <T : Any, E : ExtensionPoint<T>> get(point: E, fullContext: DokkaContext): List<T> = when (point) { - CoreExtensions.descriptorToDocumentationTranslator -> DefaultDescriptorToDocumentationTranslator + CoreExtensions.descriptorToDocumentationTranslator -> DefaultDescriptorToDocumentationTranslator CoreExtensions.documentationMerger -> DefaultDocumentationNodeMerger - CoreExtensions.commentsToContentConverterFactory -> ::DocTagToContentConverter + CoreExtensions.commentsToContentConverter -> DocTagToContentConverter(fullContext) CoreExtensions.documentationToPageTranslator -> DefaultDocumentationToPageTranslator - CoreExtensions.rendererFactory -> ::HtmlRenderer - CoreExtensions.locationProviderFactory -> ::DefaultLocationProvider + CoreExtensions.renderer -> HtmlRenderer(fullContext.single(CoreExtensions.outputWriter), fullContext) + CoreExtensions.locationProviderFactory -> DefaultLocationProviderFactory + CoreExtensions.outputWriter -> FileWriter(fullContext.configuration.outputDir, "") CoreExtensions.fileExtension -> ".html" else -> null - }.let { listOfNotNull(it) as List<T> } + }.let { listOfNotNull( it ) as List<T> } }
\ No newline at end of file diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index 8709b74f..f2c99d95 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -1,5 +1,6 @@ package org.jetbrains.dokka.plugability +import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.dokka.EnvironmentAndFacade import org.jetbrains.dokka.pages.PlatformData @@ -20,17 +21,17 @@ interface DokkaContext : DokkaExtensionHandler { fun <T : DokkaPlugin> plugin(kclass: KClass<T>): T? val logger: DokkaLogger - + val configuration: DokkaConfiguration val platforms: Map<PlatformData, EnvironmentAndFacade> companion object { fun create( - pluginsClasspath: Iterable<File>, + configuration: DokkaConfiguration, logger: DokkaLogger, platforms: Map<PlatformData, EnvironmentAndFacade> ): DokkaContext = - DokkaContextConfigurationImpl(logger, DefaultExtensions, platforms).apply { - pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() } + DokkaContextConfigurationImpl(logger, configuration, platforms).apply { + configuration.pluginsClasspath.map { it.relativeTo(File(".").absoluteFile).toURI().toURL() } .toTypedArray() .let { URLClassLoader(it, this.javaClass.classLoader) } .also { checkClasspath(it) } @@ -60,7 +61,7 @@ interface DokkaContextConfiguration { private class DokkaContextConfigurationImpl( override val logger: DokkaLogger, - private val defaultHandler: DokkaExtensionHandler?, + override val configuration: DokkaConfiguration, override val platforms: Map<PlatformData, EnvironmentAndFacade> ) : DokkaContext, DokkaContextConfiguration { private val plugins = mutableMapOf<KClass<*>, DokkaPlugin>() @@ -105,12 +106,12 @@ private class DokkaContextConfigurationImpl( override operator fun <T, E> get(point: E, askDefault: AskDefault) where T : Any, E : ExtensionPoint<T> = when (askDefault) { AskDefault.Never -> actions(point).orEmpty() - AskDefault.Always -> actions(point).orEmpty() + defaultHandler?.get(point, askDefault).orEmpty() + AskDefault.Always -> actions(point).orEmpty() + DefaultExtensions.get(point, this) AskDefault.WhenEmpty -> - actions(point)?.takeIf { it.isNotEmpty() } ?: defaultHandler?.get(point, askDefault).orEmpty() + actions(point)?.takeIf { it.isNotEmpty() } ?: DefaultExtensions?.get(point, this) } as List<T> - private fun <E : ExtensionPoint<*>> actions(point: E) = extensions[point]?.map { it.action } + private fun <E : ExtensionPoint<*>> actions(point: E) = extensions[point]?.map { it.action.get(this) } @Suppress("UNCHECKED_CAST") override fun <T : DokkaPlugin> plugin(kclass: KClass<T>) = (plugins[kclass] ?: pluginStubFor(kclass)) as T diff --git a/core/src/main/kotlin/plugability/LazyEvaluated.kt b/core/src/main/kotlin/plugability/LazyEvaluated.kt new file mode 100644 index 00000000..79ac0968 --- /dev/null +++ b/core/src/main/kotlin/plugability/LazyEvaluated.kt @@ -0,0 +1,16 @@ +package org.jetbrains.dokka.plugability + +internal class LazyEvaluated<T : Any> private constructor(private val recipe: ((DokkaContext) -> T)? = null, private var value: T? = null) { + + internal fun get(context: DokkaContext): T { + if(value != null) { + value = recipe?.invoke(context) + } + return value ?: throw AssertionError("Incorrect initialized LazyEvaluated instance") + } + + companion object { + fun <T : Any> fromInstance(value: T) = LazyEvaluated(value = value) + fun <T : Any> fromRecipe(recipe: (DokkaContext) -> T) = LazyEvaluated(recipe = recipe) + } +}
\ 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 8d1a7ba0..42bf3900 100644 --- a/core/src/main/kotlin/plugability/extensions.kt +++ b/core/src/main/kotlin/plugability/extensions.kt @@ -11,7 +11,7 @@ abstract class Extension<T : Any> internal constructor( internal val extensionPoint: ExtensionPoint<T>, internal val pluginClass: String, internal val extensionName: String, - val action: T, + internal val action: LazyEvaluated<T>, internal val ordering: (OrderDsl.() -> Unit)? = null ) { override fun toString() = "Extension: $pluginClass/$extensionName" @@ -23,11 +23,11 @@ abstract class Extension<T : Any> internal constructor( override fun hashCode() = listOf(pluginClass, extensionName).hashCode() } -class ExtensionOrdered<T : Any>( +class ExtensionOrdered<T : Any> internal constructor( extensionPoint: ExtensionPoint<T>, pluginClass: String, extensionName: String, - action: T, + action: LazyEvaluated<T>, ordering: (OrderDsl.() -> Unit) ) : Extension<T>( extensionPoint, @@ -37,11 +37,11 @@ class ExtensionOrdered<T : Any>( ordering ) -class ExtensionUnordered<T : Any>( +class ExtensionUnordered<T : Any> internal constructor( extensionPoint: ExtensionPoint<T>, pluginClass: String, extensionName: String, - action: T + action: LazyEvaluated<T> ) : Extension<T>( extensionPoint, pluginClass, @@ -57,8 +57,12 @@ annotation class ExtensionsDsl @ExtensionsDsl class ExtendingDSL(private val pluginClass: String, private val extensionName: String) { - infix fun <T: Any> ExtensionPoint<T>.with(action: T) = - ExtensionUnordered(this, this@ExtendingDSL.pluginClass, extensionName, action) + + infix fun <T : Any> ExtensionPoint<T>.with(action: T) = + ExtensionUnordered(this, this@ExtendingDSL.pluginClass, extensionName, LazyEvaluated.fromInstance(action)) + + 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) = ExtensionOrdered(extensionPoint, pluginClass, extensionName, action, block) diff --git a/core/src/main/kotlin/renderers/DefaultRenderer.kt b/core/src/main/kotlin/renderers/DefaultRenderer.kt index 8f6c6635..5f982251 100644 --- a/core/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/core/src/main/kotlin/renderers/DefaultRenderer.kt @@ -1,15 +1,20 @@ package org.jetbrains.dokka.renderers +import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.single import org.jetbrains.dokka.resolvers.LocationProvider + abstract class DefaultRenderer<T>( - protected val writer: Writer, - protected val locationProvider: LocationProvider, + protected val outputWriter: OutputWriter, protected val context: DokkaContext ) : Renderer { + protected lateinit var locationProvider: LocationProvider + private set + protected abstract fun T.buildHeader(level: Int, content: T.() -> Unit) protected abstract fun T.buildLink(address: String, content: T.() -> Unit) protected abstract fun T.buildList(node: ContentList, pageContext: PageNode) @@ -64,7 +69,7 @@ abstract class DefaultRenderer<T>( } protected open fun renderPage(page: PageNode) = - writer.write(locationProvider.resolve(page), buildPage(page, ::buildPageContent), "") + outputWriter.write(locationProvider.resolve(page), buildPage(page, ::buildPageContent), "") protected open fun renderPages(root: PageNode) { renderPage(root) @@ -77,7 +82,7 @@ abstract class DefaultRenderer<T>( getPackageNamesAndPlatforms(root) .keys .joinToString("\n") - .also { writer.write("${root.name}/package-list", it, "") } + .also { outputWriter.write("${root.name}/package-list", it, "") } protected open fun getPackageNamesAndPlatforms(root: PageNode): Map<String, List<PlatformData>> = root.children @@ -90,6 +95,7 @@ abstract class DefaultRenderer<T>( } override fun render(root: PageNode) { + locationProvider = context.single(CoreExtensions.locationProviderFactory).getLocationProvider(root, context) renderPackageList(root) buildSupportFiles() renderPages(root) diff --git a/core/src/main/kotlin/renderers/FileWriter.kt b/core/src/main/kotlin/renderers/FileWriter.kt index c9785dfd..5439db17 100644 --- a/core/src/main/kotlin/renderers/FileWriter.kt +++ b/core/src/main/kotlin/renderers/FileWriter.kt @@ -4,10 +4,10 @@ import java.io.File import java.io.IOException import java.nio.file.Paths -class FileWriter(override val root: String, override val extension: String) : Writer { +class FileWriter(val root: String, override val extension: String): OutputWriter { private val createdFiles: MutableSet<String> = mutableSetOf() - override fun write(path: String, text: String, ext: String){ + override fun write(path: String, text: String, ext: String) { if (createdFiles.contains(path)) { println("ERROR. An attempt to write $root/$path several times!") return diff --git a/core/src/main/kotlin/renderers/HtmlRenderer.kt b/core/src/main/kotlin/renderers/HtmlRenderer.kt index 2bb07666..68d6d276 100644 --- a/core/src/main/kotlin/renderers/HtmlRenderer.kt +++ b/core/src/main/kotlin/renderers/HtmlRenderer.kt @@ -4,15 +4,13 @@ import kotlinx.html.* import kotlinx.html.stream.appendHTML import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.resolvers.LocationProvider import java.io.File import java.net.URL open class HtmlRenderer( - writer: Writer, - locationProvider: LocationProvider, + outputWriter: OutputWriter, context: DokkaContext -) : DefaultRenderer<FlowContent>(writer, locationProvider, context) { +) : DefaultRenderer<FlowContent>(outputWriter, context) { override fun FlowContent.buildList(node: ContentList, pageContext: PageNode) = if (node.ordered) ol { @@ -120,8 +118,9 @@ open class HtmlRenderer( text(textNode.text) } + override fun buildSupportFiles() { // TODO copy file instead of reading - writer.write( + outputWriter.write( "style.css", javaClass.getResourceAsStream("/dokka/styles/style.css").reader().readText() ) diff --git a/core/src/main/kotlin/renderers/OutputWriter.kt b/core/src/main/kotlin/renderers/OutputWriter.kt new file mode 100644 index 00000000..84cc124d --- /dev/null +++ b/core/src/main/kotlin/renderers/OutputWriter.kt @@ -0,0 +1,6 @@ +package org.jetbrains.dokka.renderers + +interface OutputWriter{ + val extension: String + fun write(path: String, text: String, ext: String = extension) +}
\ No newline at end of file diff --git a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt index 7442f449..7eae26ca 100644 --- a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt +++ b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt @@ -10,8 +10,7 @@ import org.jetbrains.dokka.plugability.single open class DefaultLocationProvider( private val pageGraphRoot: ModulePageNode, - private val configuration: DokkaConfiguration, - context: DokkaContext + private val context: DokkaContext ) : LocationProvider { // TODO: cache private val extension = context.single(CoreExtensions.fileExtension) @@ -21,7 +20,7 @@ open class DefaultLocationProvider( findInPageGraph(dri, platforms)?.let { resolve(it, context) } ?: // Not found in PageGraph, that means it's an external link ExternalLocationProvider.getLocation(dri, - configuration.passesConfigurations + this@DefaultLocationProvider.context.configuration.passesConfigurations .filter { passConfig -> platforms.toSet().contains(PlatformData(passConfig.moduleName, passConfig.analysisPlatform, passConfig.targets)) } // TODO: change targets to something better? diff --git a/core/src/main/kotlin/resolvers/LocationProviderFactory.kt b/core/src/main/kotlin/resolvers/LocationProviderFactory.kt new file mode 100644 index 00000000..1622045b --- /dev/null +++ b/core/src/main/kotlin/resolvers/LocationProviderFactory.kt @@ -0,0 +1,13 @@ +package org.jetbrains.dokka.resolvers + +import org.jetbrains.dokka.pages.ModulePageNode +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.plugability.DokkaContext + +interface LocationProviderFactory { + fun getLocationProvider(pageNode: PageNode, context: DokkaContext): LocationProvider +} + +object DefaultLocationProviderFactory : LocationProviderFactory { + override fun getLocationProvider(pageNode: PageNode, context: DokkaContext) = DefaultLocationProvider(pageNode as ModulePageNode, context) +}
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt index 32053aa0..20180ba7 100644 --- a/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt +++ b/core/src/main/kotlin/transformers/documentation/DefaultDocumentationToPageTranslator.kt @@ -16,7 +16,7 @@ object DefaultDocumentationToPageTranslator : DocumentationToPageTranslator { node.dri, node.platformData, kind, - context.single(CoreExtensions.commentsToContentConverterFactory).invoke(context), + context.single(CoreExtensions.commentsToContentConverter), context.logger, operation ) diff --git a/plugins/xml/src/main/kotlin/XmlPlugin.kt b/plugins/xml/src/main/kotlin/XmlPlugin.kt index 1f496b33..839929e6 100644 --- a/plugins/xml/src/main/kotlin/XmlPlugin.kt +++ b/plugins/xml/src/main/kotlin/XmlPlugin.kt @@ -38,7 +38,7 @@ object XmlTransformer : PageNodeTransformer { node.dri, platformData, XMLKind.XmlList, - dokkaContext.single(CoreExtensions.commentsToContentConverterFactory).invoke(dokkaContext), + dokkaContext.single(CoreExtensions.commentsToContentConverter), dokkaContext.logger ) { block("XML Attributes", 2, XMLKind.XmlList, elementsToAdd, platformData) { element -> |