From be5060c3977d36560f83065011cea91a4255e325 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Fri, 7 Jan 2022 12:20:58 +0100 Subject: [PATCH] Apply requested changes --- core/src/main/kotlin/utilities/json.kt | 4 +- .../dokka/it/cli/CliIntegrationTest.kt | 4 +- plugins/all-modules-page/out/index.md | 10 ---- .../src/main/kotlin/cli/JsonMapperForCLI.kt | 48 ------------------- runners/cli/src/main/kotlin/cli/main.kt | 16 ++++++- runners/cli/src/test/kotlin/cli/CliTest.kt | 5 +- 6 files changed, 20 insertions(+), 67 deletions(-) delete mode 100644 plugins/all-modules-page/out/index.md delete mode 100644 runners/cli/src/main/kotlin/cli/JsonMapperForCLI.kt diff --git a/core/src/main/kotlin/utilities/json.kt b/core/src/main/kotlin/utilities/json.kt index 211037d6c3..a0ba434207 100644 --- a/core/src/main/kotlin/utilities/json.kt +++ b/core/src/main/kotlin/utilities/json.kt @@ -31,9 +31,7 @@ internal class TypeReference private constructor( @PublishedApi internal fun toJsonString(value: Any): String = objectMapper.writeValueAsString(value) -@PublishedApi -internal inline fun parseJson(json: String): T = parseJson(json, TypeReference()) - +inline fun parseJson(json: String): T = parseJson(json, TypeReference()) @PublishedApi internal fun parseJson(json: String, typeReference: TypeReference): T = diff --git a/integration-tests/cli/src/integrationTest/kotlin/org/jetbrains/dokka/it/cli/CliIntegrationTest.kt b/integration-tests/cli/src/integrationTest/kotlin/org/jetbrains/dokka/it/cli/CliIntegrationTest.kt index 4234e9b147..b94df32a31 100644 --- a/integration-tests/cli/src/integrationTest/kotlin/org/jetbrains/dokka/it/cli/CliIntegrationTest.kt +++ b/integration-tests/cli/src/integrationTest/kotlin/org/jetbrains/dokka/it/cli/CliIntegrationTest.kt @@ -264,7 +264,7 @@ class CliIntegrationTest : AbstractCliIntegrationTest() { @Test - fun json() { + fun `should accept json as input configuration`() { val dokkaOutputDir = File(projectDir, "output") assertTrue(dokkaOutputDir.mkdirs()) val jsonPath = javaClass.getResource("/my-file.json")?.path ?: throw IllegalStateException("No JSON found!") @@ -303,7 +303,7 @@ class CliIntegrationTest : AbstractCliIntegrationTest() { * make sure that global settings apply to dokka context. */ @Test - fun jsonWithGlobals() { + fun `global settings should overwrite package options in configuration`() { val dokkaOutputDir = File(projectDir, "output") assertTrue(dokkaOutputDir.mkdirs()) val jsonPath = javaClass.getResource("/my-file.json")?.path ?: throw IllegalStateException("No JSON found!") diff --git a/plugins/all-modules-page/out/index.md b/plugins/all-modules-page/out/index.md deleted file mode 100644 index 5c9593f62f..0000000000 --- a/plugins/all-modules-page/out/index.md +++ /dev/null @@ -1,10 +0,0 @@ -/ - -# Sample project - -Sample documentation with [external link](https://www.google.pl) - -## All modules: - -| Name | -|---| diff --git a/runners/cli/src/main/kotlin/cli/JsonMapperForCLI.kt b/runners/cli/src/main/kotlin/cli/JsonMapperForCLI.kt deleted file mode 100644 index 33bfd9fc04..0000000000 --- a/runners/cli/src/main/kotlin/cli/JsonMapperForCLI.kt +++ /dev/null @@ -1,48 +0,0 @@ -package org.jetbrains.dokka - -import com.fasterxml.jackson.core.JsonGenerator -import com.fasterxml.jackson.databind.DeserializationFeature -import com.fasterxml.jackson.databind.SerializerProvider -import com.fasterxml.jackson.databind.module.SimpleModule -import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import com.fasterxml.jackson.module.kotlin.jacksonTypeRef -import java.io.File - -// THIS IS COPIED FROM BASE SINCE IT NEEDS TO BE INSTANTIATED ON THE SAME CLASS LOADER AS PLUGINS - -private val objectMapper = run { - val module = SimpleModule().apply { - addSerializer(FileSerializer) - } - jacksonObjectMapper() - .registerModule(module) - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) -} - -@PublishedApi -internal class TypeReference private constructor( - internal val jackson: com.fasterxml.jackson.core.type.TypeReference -) { - companion object { - internal inline operator fun invoke(): TypeReference = TypeReference(jacksonTypeRef()) - } -} - -inline fun parseJson(json: String): T = parseJson(json, TypeReference()) - -@PublishedApi -internal fun parseJson(json: String, typeReference: TypeReference): T = - objectMapper.readValue(json, typeReference.jackson) - -private object FileSerializer : StdScalarSerializer(File::class.java) { - override fun serialize(value: File, g: JsonGenerator, provider: SerializerProvider) { - g.writeString(value.path) - } -} - -data class GlobalDokkaConfiguration( - val perPackageOptions: List?, - val externalDocumentationLinks: List?, - val sourceLinks: List? -) diff --git a/runners/cli/src/main/kotlin/cli/main.kt b/runners/cli/src/main/kotlin/cli/main.kt index e8b7914635..b32a3dd136 100644 --- a/runners/cli/src/main/kotlin/cli/main.kt +++ b/runners/cli/src/main/kotlin/cli/main.kt @@ -404,11 +404,24 @@ fun parseLinks(links: List): List { } } + +/** + * Global options are applied to all packages and modules and overwrite package configuration. + * + * These are handy if we have multiple sourcesets sharing the same global options as it reduces the size of the boilerplate. + * Otherwise, the user would be enforced to repeat all these options per each sourceset. + */ +data class GlobalDokkaConfiguration( + val perPackageOptions: List?, + val externalDocumentationLinks: List?, + val sourceLinks: List? +) + fun initializeConfiguration(globalArguments: GlobalArguments): DokkaConfiguration = if (globalArguments.json != null) { val jsonContent = Paths.get(checkNotNull(globalArguments.json)).toFile().readText() val globals: GlobalDokkaConfiguration = parseJson(jsonContent) val dokkaConfigurationImpl = DokkaConfigurationImpl(jsonContent) - dokkaConfigurationImpl.run { + dokkaConfigurationImpl.apply { sourceSets.forEach { it.perPackageOptions.cast>().addAll(globals.perPackageOptions ?: emptyList()) } @@ -425,7 +438,6 @@ fun initializeConfiguration(globalArguments: GlobalArguments): DokkaConfiguratio it.externalDocumentationLinks.cast>().addAll(defaultLinks(it)) } } - dokkaConfigurationImpl } else { globalArguments } diff --git a/runners/cli/src/test/kotlin/cli/CliTest.kt b/runners/cli/src/test/kotlin/cli/CliTest.kt index 4ed2e6dc47..5910e93871 100644 --- a/runners/cli/src/test/kotlin/cli/CliTest.kt +++ b/runners/cli/src/test/kotlin/cli/CliTest.kt @@ -3,13 +3,14 @@ package org.jetbrains.dokka import junit.framework.Assert.assertTrue import org.junit.Test import java.lang.IllegalStateException +import java.nio.file.Paths import kotlin.test.assertEquals class CliIntegrationTest { @Test - fun testGlobalArgs() { - val jsonPath = javaClass.getResource("/my-file.json")?.path ?: throw IllegalStateException("No JSON found!") + fun `should apply global settings to all source sets`() { + val jsonPath = Paths.get(javaClass.getResource("/my-file.json")?.toURI() ?: throw IllegalStateException("No JSON found!")).toFile().toString() val globalArguments = GlobalArguments(arrayOf(jsonPath)) val configuration = initializeConfiguration(globalArguments)