Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multimodule sourcesets dependencies #1766

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/base/src/main/kotlin/DokkaBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class DokkaBase : DokkaPlugin() {
}

val sourcesetDependencyAppender by extending {
htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) } applyIf { !delayTemplateSubstitution }
htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) }
}

val resolveLinkConsumer by extending {
Expand Down
18 changes: 10 additions & 8 deletions plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.DokkaBaseConfiguration
import org.jetbrains.dokka.base.renderers.sourceSets
import org.jetbrains.dokka.base.templating.AddToSearch
import org.jetbrains.dokka.base.templating.AddToSourcesetDependencies
import org.jetbrains.dokka.base.templating.toJsonString
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.pages.*
Expand Down Expand Up @@ -169,15 +171,15 @@ class SourcesetDependencyAppender(val context: DokkaContext) : PageTransformer {
it.sourceSetID to it.dependentSourceSets
}.toMap()

fun createDependenciesJson(): String = "sourceset_dependencies = '{${
dependenciesMap.entries.joinToString(", ") {
"\"${it.key}\": [${
it.value.joinToString(",") {
"\"$it\""
fun createDependenciesJson(): String =
dependenciesMap.map { (key, values) -> key.toString() to values.map { it.toString() } }.toMap()
.let { content ->
if (context.configuration.delayTemplateSubstitution) {
toJsonString(AddToSourcesetDependencies(context.configuration.moduleName, content))
} else {
"sourceset_dependencies='${toJsonString(content)}'"
}
}]"
}
}}'"
}

val deps = RendererSpecificResourcePage(
name = name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.jetbrains.dokka.base.templating

data class AddToSourcesetDependencies(val moduleName: String, val content: Map<String, List<String>>) : Command
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package templates

import org.jetbrains.dokka.base.templating.AddToSourcesetDependencies
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

private typealias Entry = Map<String, List<String>>

class SourcesetDependencyProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy {
private val fileName = "sourceset_dependencies.js"
private val fragments = ConcurrentHashMap<String, Entry>()

override fun finish(output: File) {
if (fragments.isNotEmpty()) {
val content = fragments.values.fold(emptyMap<String, List<String>>()) { acc, e -> acc + e }
.let { "sourceset_dependencies = '${toJsonString(it)}'" }
output.resolve("scripts").mkdirs()
output.resolve("scripts/$fileName").writeText(content)
}
}

override fun process(input: File, output: File): Boolean =
input.takeIf { it.name == fileName }
?.runCatching { parseJson<AddToSourcesetDependencies>(input.readText()) }
?.getOrNull()
?.also { (moduleName, content) ->
fragments += (moduleName to content)
} != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.dokka.templates
import org.jetbrains.dokka.allModulesPage.templates.NavigationSearchTemplateStrategy
import org.jetbrains.dokka.allModulesPage.templates.PagesSearchTemplateStrategy
import org.jetbrains.dokka.plugability.DokkaPlugin
import templates.SourcesetDependencyProcessingStrategy

class TemplatingPlugin : DokkaPlugin() {

Expand Down Expand Up @@ -32,6 +33,12 @@ class TemplatingPlugin : DokkaPlugin() {
}
}

val sourcesetDependencyProcessingStrategy by extending {
templateProcessingStrategy providing ::SourcesetDependencyProcessingStrategy order {
before(fallbackProcessingStrategy)
}
}

val pagesSearchTemplateStrategy by extending {
templateProcessingStrategy providing ::PagesSearchTemplateStrategy order {
before(fallbackProcessingStrategy)
Expand Down