diff options
author | Błażej Kardyś <bkardys@virtuslab.com> | 2021-01-05 17:59:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-05 17:59:38 +0100 |
commit | 1618e552c136e25d86bf0708e0d760841c77c139 (patch) | |
tree | c608ce91b020bb0ecbfe75d0f456a0eae84b4c51 /plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt | |
parent | ef98e4b6505c3fdd192b1d5057c718079d27b972 (diff) | |
download | dokka-1618e552c136e25d86bf0708e0d760841c77c139.tar.gz dokka-1618e552c136e25d86bf0708e0d760841c77c139.tar.bz2 dokka-1618e552c136e25d86bf0708e0d760841c77c139.zip |
Versioning (#1654)
* Adding versioning mechanism for multimodule
* Versioning improvement
* Refactor configuration, add ordering
* Fix integration tests
* Change packages, unignore test
Co-authored-by: Marcin Aman <marcin.aman@gmail.com>
Diffstat (limited to 'plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt')
-rw-r--r-- | plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt new file mode 100644 index 00000000..a2d55209 --- /dev/null +++ b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt @@ -0,0 +1,68 @@ +package org.jetbrains.dokka.allModulesPage.templates + +import org.jetbrains.dokka.base.renderers.html.SearchRecord +import org.jetbrains.dokka.base.templating.AddToSearch +import org.jetbrains.dokka.base.templating.parseJson +import org.jetbrains.dokka.base.templating.toJsonString +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.templates.TemplateProcessingStrategy +import java.io.File +import java.util.concurrent.ConcurrentHashMap + +abstract class BaseJsonNavigationTemplateProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy { + abstract val navigationFileNameWithoutExtension: String + abstract val path: String + + private val fragments = ConcurrentHashMap<String, List<SearchRecord>>() + + open fun canProcess(file: File): Boolean = + file.extension == "json" && file.nameWithoutExtension == navigationFileNameWithoutExtension + + override fun process(input: File, output: File): Boolean { + val canProcess = canProcess(input) + if (canProcess) { + runCatching { parseJson<AddToSearch>(input.readText()) }.getOrNull()?.let { command -> + context.configuration.modules.find { it.name == command.moduleName }?.relativePathToOutputDirectory + ?.relativeToOrSelf(context.configuration.outputDir) + ?.let { key -> + fragments[key.toString()] = command.elements + } + } ?: fallbackToCopy(input, output) + } + return canProcess + } + + override fun finish(output: File) { + if (fragments.isNotEmpty()) { + val content = toJsonString(fragments.entries.flatMap { (moduleName, navigation) -> + navigation.map { it.withResolvedLocation(moduleName) } + }) + output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content) + + fragments.keys.forEach { + output.resolve(it).resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content) + } + } + } + + private fun fallbackToCopy(input: File, output: File) { + context.logger.warn("Falling back to just copying file for ${input.name} even thought it should process it") + input.copyTo(output) + } + + private fun SearchRecord.withResolvedLocation(moduleName: String): SearchRecord = + copy(location = "$moduleName/$location") + +} + +class NavigationSearchTemplateStrategy(val dokkaContext: DokkaContext) : + BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) { + override val navigationFileNameWithoutExtension: String = "navigation-pane" + override val path: String = "scripts" +} + +class PagesSearchTemplateStrategy(val dokkaContext: DokkaContext) : + BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) { + override val navigationFileNameWithoutExtension: String = "pages" + override val path: String = "scripts" +}
\ No newline at end of file |