aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating
diff options
context:
space:
mode:
authorMarcin Aman <marcin.aman@gmail.com>2021-01-21 00:34:43 +0100
committerGitHub <noreply@github.com>2021-01-21 00:34:43 +0100
commitfdf8a298f586d7e334c312346b70b59c64c8d037 (patch)
treed9e92e920c24f442df6339e19b27225d7d01d21b /plugins/templating
parent1f592a7ec2786e0a0b77d224d1414ef3042caae4 (diff)
downloaddokka-fdf8a298f586d7e334c312346b70b59c64c8d037.tar.gz
dokka-fdf8a298f586d7e334c312346b70b59c64c8d037.tar.bz2
dokka-fdf8a298f586d7e334c312346b70b59c64c8d037.zip
Empty modules filtering (#1699)
Diffstat (limited to 'plugins/templating')
-rw-r--r--plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt1
-rw-r--r--plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt88
-rw-r--r--plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt11
-rw-r--r--plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt2
-rw-r--r--plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt2
-rw-r--r--plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt2
-rw-r--r--plugins/templating/src/test/kotlin/templates/TemplatingDokkaTestGenerator.kt6
-rw-r--r--plugins/templating/src/test/kotlin/templates/TestTemplatingGeneration.kt12
8 files changed, 92 insertions, 32 deletions
diff --git a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
index a2d55209..5a97d216 100644
--- a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
+++ b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
@@ -37,6 +37,7 @@ abstract class BaseJsonNavigationTemplateProcessingStrategy(val context: DokkaCo
val content = toJsonString(fragments.entries.flatMap { (moduleName, navigation) ->
navigation.map { it.withResolvedLocation(moduleName) }
})
+ output.resolve(path).mkdirs()
output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
fragments.keys.forEach {
diff --git a/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt
index 8fbd76b6..c67654ec 100644
--- a/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt
+++ b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt
@@ -1,15 +1,26 @@
package org.jetbrains.dokka.templates
import kotlinx.coroutines.*
+import org.jetbrains.dokka.DokkaConfiguration
+import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.templating.Command
+import org.jetbrains.dokka.model.withDescendants
+import org.jetbrains.dokka.pages.RootPageNode
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query
+import org.jetbrains.dokka.plugability.querySingle
import org.jsoup.nodes.Element
import java.io.File
-interface TemplateProcessor {
- fun process()
+interface TemplateProcessor
+
+interface SubmoduleTemplateProcessor : TemplateProcessor {
+ fun process(modules: List<DokkaConfiguration.DokkaModuleDescription>): TemplatingResult
+}
+
+interface MultiModuleTemplateProcessor : TemplateProcessor {
+ fun process(generatedPagesTree: RootPageNode)
}
interface TemplateProcessingStrategy {
@@ -17,40 +28,69 @@ interface TemplateProcessingStrategy {
fun finish(output: File) {}
}
-class DefaultTemplateProcessor(
+class DefaultSubmoduleTemplateProcessor(
private val context: DokkaContext,
-): TemplateProcessor {
+) : SubmoduleTemplateProcessor {
- private val strategies: List<TemplateProcessingStrategy> = context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
+ private val strategies: List<TemplateProcessingStrategy> =
+ context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
- override fun process() = runBlocking(Dispatchers.Default) {
- coroutineScope {
- context.configuration.modules.forEach {
- launch {
- it.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(it.relativePathToOutputDirectory))
+ private val configuredModulesPaths =
+ context.configuration.modules.map { it.sourceOutputDirectory.absolutePath to it.name }.toMap()
+
+ override fun process(modules: List<DokkaConfiguration.DokkaModuleDescription>) =
+ runBlocking(Dispatchers.Default) {
+ coroutineScope {
+ modules.fold(TemplatingResult()) { acc, module ->
+ acc + module.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(module.relativePathToOutputDirectory))
}
}
}
- strategies.map { it.finish(context.configuration.outputDir) }
- Unit
- }
- private suspend fun File.visit(target: File): Unit = coroutineScope {
- val source = this@visit
- if (source.isDirectory) {
- target.mkdir()
- source.list()?.forEach {
- launch { source.resolve(it).visit(target.resolve(it)) }
- }
- } else {
- strategies.first { it.process(source, target) }
+ private suspend fun File.visit(target: File, acc: TemplatingResult = TemplatingResult()): TemplatingResult =
+ coroutineScope {
+ val source = this@visit
+ if (source.isDirectory) {
+ target.mkdir()
+ val files = source.list().orEmpty()
+ val accWithSelf = configuredModulesPaths[source.absolutePath]
+ ?.takeIf { files.firstOrNull { !it.startsWith(".") } != null }
+ ?.let { acc.copy(modules = acc.modules + it) }
+ ?: acc
+
+ files.fold(accWithSelf) { acc, path ->
+ source.resolve(path).visit(target.resolve(path), acc)
+ }
+ } else {
+ strategies.first { it.process(source, target) }
+ acc
+ }
}
+}
+
+class DefaultMultiModuleTemplateProcessor(
+ context: DokkaContext,
+) : MultiModuleTemplateProcessor {
+ private val strategies: List<TemplateProcessingStrategy> =
+ context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
+
+ private val locationProviderFactory = context.plugin<DokkaBase>().querySingle { locationProviderFactory }
+
+ override fun process(generatedPagesTree: RootPageNode) {
+ val locationProvider = locationProviderFactory.getLocationProvider(generatedPagesTree)
+ generatedPagesTree.withDescendants().mapNotNull { locationProvider.resolve(it)?.let { File(it) } }
+ .forEach { location -> strategies.first { it.process(location, location) } }
}
+
}
-data class TemplatingContext<out T: Command>(
+data class TemplatingContext<out T : Command>(
val input: File,
val output: File,
val element: Element,
val command: T,
-) \ No newline at end of file
+)
+
+data class TemplatingResult(val modules: List<String> = emptyList()) {
+ operator fun plus(rhs: TemplatingResult): TemplatingResult = TemplatingResult((modules + rhs.modules).distinct())
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt
index 29ca4904..546a0443 100644
--- a/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt
+++ b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt
@@ -6,14 +6,19 @@ import org.jetbrains.dokka.plugability.DokkaPlugin
class TemplatingPlugin : DokkaPlugin() {
- val templateProcessor by extensionPoint<TemplateProcessor>()
+ val submoduleTemplateProcessor by extensionPoint<SubmoduleTemplateProcessor>()
+ val multimoduleTemplateProcessor by extensionPoint<MultiModuleTemplateProcessor>()
val templateProcessingStrategy by extensionPoint<TemplateProcessingStrategy>()
val directiveBasedCommandHandlers by extensionPoint<CommandHandler>()
val substitutor by extensionPoint<Substitutor>()
- val defaultTemplateProcessor by extending {
- templateProcessor providing ::DefaultTemplateProcessor
+ val defaultSubmoduleTemplateProcessor by extending {
+ submoduleTemplateProcessor providing ::DefaultSubmoduleTemplateProcessor
+ }
+
+ val defaultMultiModuleTemplateProcessor by extending {
+ multimoduleTemplateProcessor providing ::DefaultMultiModuleTemplateProcessor
}
val directiveBasedHtmlTemplateProcessingStrategy by extending {
diff --git a/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
index 19c940c2..a1e11dfe 100644
--- a/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
@@ -127,7 +127,7 @@ class AddToNavigationCommandResolutionTest : TemplatingAbstractTest() {
module2Navigation.writeText(inputForModule("module2"))
testFromData(configuration, preserveOutputLocation = true) {
- submoduleProcessingStage = { ctx ->
+ finishProcessingSubmodules = { ctx ->
test(ctx)
}
}
diff --git a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
index a962d524..049cef96 100644
--- a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
@@ -56,7 +56,7 @@ class AddToSearchCommandResolutionTest : TemplatingAbstractTest() {
}
testFromData(configuration, preserveOutputLocation = true) {
- submoduleProcessingStage = { _ ->
+ finishProcessingSubmodules = { _ ->
val expected = elements.map { it.copy(location = "module1/${it.location}") } +
elements.map { it.copy(location = "module2/${it.location}") }
diff --git a/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
index d7143f11..9d211876 100644
--- a/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
@@ -52,7 +52,7 @@ class SubstitutionCommandResolutionTest : TemplatingAbstractTest() {
}
testFromData(configuration, preserveOutputLocation = true){
- submoduleProcessingStage = {
+ finishProcessingSubmodules = {
assertHtmlEqualsIgnoringWhitespace(expected, testedFile.readText())
}
}
diff --git a/plugins/templating/src/test/kotlin/templates/TemplatingDokkaTestGenerator.kt b/plugins/templating/src/test/kotlin/templates/TemplatingDokkaTestGenerator.kt
index 906fb1ea..04420662 100644
--- a/plugins/templating/src/test/kotlin/templates/TemplatingDokkaTestGenerator.kt
+++ b/plugins/templating/src/test/kotlin/templates/TemplatingDokkaTestGenerator.kt
@@ -36,6 +36,9 @@ class TemplatingDokkaTestGenerator(
generation.processSubmodules()
submoduleProcessingStage(context)
+
+ generation.finishProcessing()
+ finishProcessingSubmodules(context)
}
}
@@ -43,15 +46,18 @@ class TemplatingDokkaTestGenerator(
open class TemplatingTestMethods(
open val pluginsSetupStage: (DokkaContext) -> Unit,
open val submoduleProcessingStage: (DokkaContext) -> Unit,
+ open val finishProcessingSubmodules: (DokkaContext) -> Unit,
) : TestMethods
class TemplatingTestBuilder : TestBuilder<TemplatingTestMethods>() {
var pluginsSetupStage: (DokkaContext) -> Unit = {}
var submoduleProcessingStage: (DokkaContext) -> Unit = {}
+ var finishProcessingSubmodules: (DokkaContext) -> Unit = {}
override fun build() = TemplatingTestMethods(
pluginsSetupStage,
submoduleProcessingStage,
+ finishProcessingSubmodules,
)
}
diff --git a/plugins/templating/src/test/kotlin/templates/TestTemplatingGeneration.kt b/plugins/templating/src/test/kotlin/templates/TestTemplatingGeneration.kt
index 0a5bae4b..5dbe2bbe 100644
--- a/plugins/templating/src/test/kotlin/templates/TestTemplatingGeneration.kt
+++ b/plugins/templating/src/test/kotlin/templates/TestTemplatingGeneration.kt
@@ -4,19 +4,27 @@ import org.jetbrains.dokka.Timer
import org.jetbrains.dokka.generation.Generation
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.query
import org.jetbrains.dokka.plugability.querySingle
-class TestTemplatingGeneration(context: DokkaContext): Generation {
+class TestTemplatingGeneration(private val context: DokkaContext) : Generation {
val templatingPlugin by lazy { context.plugin<TemplatingPlugin>() }
override fun Timer.generate() {
report("Processing submodules")
processSubmodules()
+
+ report("Finishing processing")
+ finishProcessing()
}
fun processSubmodules() =
- templatingPlugin.querySingle { templateProcessor }.process()
+ templatingPlugin.querySingle { submoduleTemplateProcessor }.process(context.configuration.modules)
+
+ fun finishProcessing() =
+ templatingPlugin.query { templateProcessingStrategy }.forEach { it.finish(context.configuration.outputDir) }
+
override val generationName = "test template generation"
} \ No newline at end of file