diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2019-11-26 13:44:27 +0100 |
---|---|---|
committer | Paweł Marks <pmarks@virtuslab.com> | 2019-11-26 21:02:20 +0100 |
commit | 49439594f86217d8a25e8df2580b8ef29d836230 (patch) | |
tree | 50dc1b0793c4804b382c6b75fd7a3a0d27cb869a /core/src/main/kotlin/DokkaGenerator.kt | |
parent | c185e420971950747535e98f96a12e480159dd83 (diff) | |
download | dokka-49439594f86217d8a25e8df2580b8ef29d836230.tar.gz dokka-49439594f86217d8a25e8df2580b8ef29d836230.tar.bz2 dokka-49439594f86217d8a25e8df2580b8ef29d836230.zip |
Introduction of all important extension points and restructuring of DokkaGenerator
Diffstat (limited to 'core/src/main/kotlin/DokkaGenerator.kt')
-rw-r--r-- | core/src/main/kotlin/DokkaGenerator.kt | 183 |
1 files changed, 134 insertions, 49 deletions
diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index 05eb3054..b6bf7a73 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -1,20 +1,24 @@ package org.jetbrains.dokka import org.jetbrains.dokka.Model.Module -import org.jetbrains.dokka.Model.transformers.DocumentationNodesMerger import org.jetbrains.dokka.Utilities.pretty import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.pages.MarkdownToContentConverter +import org.jetbrains.dokka.pages.DefaultMarkdownToContentConverter import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.single import org.jetbrains.dokka.renderers.FileWriter import org.jetbrains.dokka.renderers.HtmlRenderer import org.jetbrains.dokka.resolvers.DefaultLocationProvider -import org.jetbrains.dokka.transformers.DefaultDocumentationToPageTransformer +import org.jetbrains.dokka.resolvers.LocationProvider +import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationToPageTranslator +import org.jetbrains.dokka.transformers.descriptors.DokkaDescriptorVisitor +import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationNodeMerger import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageRenderer +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.utils.PathUtil import java.io.File @@ -22,56 +26,131 @@ class DokkaGenerator( private val configuration: DokkaConfiguration, private val logger: DokkaLogger ) { - fun generate(): Unit { + + fun generate() { + logger.debug("Setting up analysis environments") + val platforms: Map<PlatformData, EnvironmentAndFacade> = configuration.passesConfigurations.map { + PlatformData(it.analysisPlatform, it.targets) to createEnvironmentAndFacade(it) + }.toMap() logger.debug("Initializing plugins") - val context = DokkaContext.create(configuration.pluginsClasspath, logger) - - configuration.passesConfigurations.map { pass -> - AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { - if (analysisPlatform == Platform.jvm) { - addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) - } - for (element in pass.classpath) { - addClasspath(File(element)) - } - - addSources(pass.sourceRoots.map { it.path }) - - loadLanguageVersionSettings(pass.languageVersion, pass.apiVersion) - - val environment = createCoreEnvironment() - val (facade, _) = createResolutionFacade(environment) - - environment.getSourceFiles().asSequence() - .map { it.packageFqName } - .distinct() - .mapNotNull { facade.resolveSession.getPackageFragment(it) } - .map { - DokkaDescriptorVisitor( - PlatformData(pass.analysisPlatform, pass.targets), - facade - ).visitPackageFragmentDescriptor( - it, - DRI.topLevel - ) - } - .toList() - .let { Module(it) } - .let { DocumentationNodesMerger(it) } - .also { println("${pass.analysisPlatform}:\n${it.pretty()}\n\n") } + val context = DokkaContext.create(configuration.pluginsClasspath, logger, platforms) + + logger.debug("Creating documentation models") + val modulesFromPlatforms = platforms.map { (pdata, _) -> translateDescriptors(pdata, context) } + + logger.debug("Merging documentation models") + val documentationModel = context.single(CoreExtensions.documentationMerger) + .invoke(modulesFromPlatforms, context) + + logger.debug("Transforming documentation model") + val transformedDocumentation = context[CoreExtensions.documentationTransformer] + .fold(documentationModel) { acc, t -> t(acc, context) } + + logger.debug("Creating pages") + val pages = context.single(CoreExtensions.documentationToPageTranslator) + .invoke(transformedDocumentation, context) + + logger.debug("Transforming pages") + val transformedPages = context[CoreExtensions.pageTransformer] + .fold(pages) { acc, t -> t(acc, context) } + + logger.debug("Rendering") + val fileWriter = FileWriter(configuration.outputDir, "") + val locationProvider = context.single(CoreExtensions.locationProvider) + .invoke(transformedPages, configuration, context) + val renderer = context.single(CoreExtensions.renderer) + .invoke(fileWriter, locationProvider, context) + + renderer.render(transformedPages) + } + +// fun generate(int: Int) { +// +// logger.debug("Initializing plugins") +// val context = DokkaContext.create(configuration.pluginsClasspath, logger, platforms) +// +// configuration.passesConfigurations.map { pass -> +// AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { +// if (analysisPlatform == Platform.jvm) { +// addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) +// } +// for (element in pass.classpath) { +// addClasspath(File(element)) +// } +// +// addSources(pass.sourceRoots.map { it.path }) +// +// loadLanguageVersionSettings(pass.languageVersion, pass.apiVersion) +// +// val environment = createCoreEnvironment() +// val (facade, _) = createResolutionFacade(environment) +// +// environment.getSourceFiles().asSequence() +// .map { it.packageFqName } +// .distinct() +// .mapNotNull { facade.resolveSession.getPackageFragment(it) } +// .map { +// DokkaDescriptorVisitor( +// PlatformData( +// pass.analysisPlatform, +// pass.targets +// ), facade +// ) +// .visitPackageFragmentDescriptor(it, DRI.topLevel) +// } +// .toList() +// .let { Module(it) } +// .let { DefaultDocumentationNodeMerger(it) } +// .also { println("${pass.analysisPlatform}:\n${it.pretty()}\n\n") } +// } +// }.let { +// val markdownConverter = DefaultMarkdownToContentConverter(logger) +// DefaultDocumentationToPageTranslator( +// markdownConverter, +// logger +// ).transform( +// DefaultDocumentationNodeMerger( +// it +// ) +// ) +// }.let { +// context[CoreExtensions.pageTransformer].fold(it) { pn, t -> t(pn, context) } +// }.also { +// HtmlRenderer( +// FileWriter(configuration.outputDir, ""), +// DefaultLocationProvider(it, configuration, ".${configuration.format}") +// ).render(it) +// } +// } + + private fun createEnvironmentAndFacade(pass: DokkaConfiguration.PassConfiguration): EnvironmentAndFacade = + AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { + if (analysisPlatform == Platform.jvm) { + addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) } - }.let { - val markdownConverter = MarkdownToContentConverter(logger) - DefaultDocumentationToPageTransformer(markdownConverter, logger).transform(DocumentationNodesMerger(it)) - }.let { - context[CoreExtensions.pageTransformer].fold(it) { pn, t -> t.action.invoke(pn, context) } - }.also { - HtmlRenderer( - FileWriter(configuration.outputDir, ""), - DefaultLocationProvider(it, configuration, ".${configuration.format}") - ).render(it) + pass.classpath.forEach { addClasspath(File(it)) } + + addSources(pass.sourceRoots.map { it.path }) + + loadLanguageVersionSettings(pass.languageVersion, pass.apiVersion) + + val environment = createCoreEnvironment() + val (facade, _) = createResolutionFacade(environment) + EnvironmentAndFacade(environment, facade) } + + private fun translateDescriptors(platformData: PlatformData, context: DokkaContext): Module { + val (environment, facade) = context.platforms.getValue(platformData) + + val packageFragments = environment.getSourceFiles().asSequence() + .map { it.packageFqName } + .distinct() + .mapNotNull { facade.resolveSession.getPackageFragment(it) } + .toList() + + return context.single(CoreExtensions.descriptorToDocumentationTranslator) + .invoke(packageFragments, platformData, context) } private class DokkaMessageCollector(private val logger: DokkaLogger) : MessageCollector { @@ -90,4 +169,10 @@ class DokkaGenerator( override fun hasErrors() = seenErrors } +} + +// It is not data class due to ill-defined equals +class EnvironmentAndFacade(val environment: KotlinCoreEnvironment, val facade: DokkaResolutionFacade) { + operator fun component1() = environment + operator fun component2() = facade }
\ No newline at end of file |