From 13edb873db122e2088207ca10b94382e30875636 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Thu, 16 Jul 2020 10:24:02 +0200 Subject: Use jackson for json serialization and parsing --- runners/gradle-plugin/build.gradle.kts | 1 + .../jetbrains/dokka/gradle/AbstractDokkaTask.kt | 2 +- .../jetbrains/dokka/gradle/DokkaMultimoduleTask.kt | 6 +- .../kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt | 6 +- .../dokka/gradle/configurationImplementations.kt | 6 +- .../gradle/GradleDokkaConfigurationJsonTest.kt | 94 ++++++++++++++++++++++ 6 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/GradleDokkaConfigurationJsonTest.kt (limited to 'runners/gradle-plugin') diff --git a/runners/gradle-plugin/build.gradle.kts b/runners/gradle-plugin/build.gradle.kts index 0222f5e0..4ad1d144 100644 --- a/runners/gradle-plugin/build.gradle.kts +++ b/runners/gradle-plugin/build.gradle.kts @@ -11,6 +11,7 @@ repositories { dependencies { implementation(project(":core")) + compileOnly("com.fasterxml.jackson.core:jackson-annotations:2.11.1") compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin") compileOnly("com.android.tools.build:gradle:3.0.0") compileOnly("com.android.tools.build:gradle-core:3.0.0") diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaTask.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaTask.kt index 846f021c..1269b305 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaTask.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaTask.kt @@ -18,7 +18,7 @@ abstract class AbstractDokkaTask : DefaultTask(), Configurable { var outputDirectory: String = defaultDokkaOutputDirectory().absolutePath @Input - override val pluginsConfiguration: Map = mutableMapOf() + override val pluginsConfiguration: MutableMap = mutableMapOf() @Classpath val plugins: Configuration = project.maybeCreateDokkaPluginConfiguration(name) diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaMultimoduleTask.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaMultimoduleTask.kt index 6fd58afe..986b883a 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaMultimoduleTask.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaMultimoduleTask.kt @@ -1,11 +1,10 @@ package org.jetbrains.dokka.gradle -import com.google.gson.GsonBuilder -import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaBasePlugin.DOCUMENTATION_GROUP import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal import org.jetbrains.dokka.plugability.Configurable +import org.jetbrains.dokka.toJsonString open class DokkaMultimoduleTask : AbstractDokkaTask(), Configurable { @@ -23,9 +22,8 @@ open class DokkaMultimoduleTask : AbstractDokkaTask(), Configurable { override fun generate() { val bootstrap = DokkaBootstrap("org.jetbrains.dokka.DokkaMultimoduleBootstrapImpl") - val gson = GsonBuilder().setPrettyPrinting().create() val configuration = getConfiguration() - bootstrap.configure(gson.toJson(configuration)) { level, message -> + bootstrap.configure(configuration.toJsonString()) { level, message -> when (level) { "debug" -> logger.debug(message) "info" -> logger.info(message) diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt index 0d7e74a3..2253a975 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt @@ -1,6 +1,5 @@ package org.jetbrains.dokka.gradle -import com.google.gson.GsonBuilder import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.Task @@ -15,6 +14,7 @@ import org.jetbrains.dokka.Platform import org.jetbrains.dokka.ReflectDsl import org.jetbrains.dokka.ReflectDsl.isNotInstance import org.jetbrains.dokka.gradle.ConfigurationExtractor.PlatformData +import org.jetbrains.dokka.toJsonString import java.io.File import java.util.concurrent.Callable @@ -123,9 +123,7 @@ open class DokkaTask : AbstractDokkaTask() { outputDiagnosticInfo = true val bootstrap = DokkaBootstrap("org.jetbrains.dokka.DokkaBootstrapImpl") - bootstrap.configure( - GsonBuilder().setPrettyPrinting().create().toJson(configuration) - ) { level, message -> + bootstrap.configure(configuration.toJsonString()) { level, message -> when (level) { "debug" -> logger.debug(message) "info" -> logger.info(message) diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/configurationImplementations.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/configurationImplementations.kt index b6b8399c..84f35cb4 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/configurationImplementations.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/configurationImplementations.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import com.android.build.gradle.api.AndroidSourceSet +import com.fasterxml.jackson.annotation.JsonIgnore import groovy.lang.Closure import org.gradle.api.Action import org.gradle.api.Project @@ -34,8 +35,8 @@ class GradleSourceRootImpl : SourceRoot, Serializable { } open class GradleDokkaSourceSet constructor( - @Transient @get:Input val name: String, - @Transient @get:Internal internal val project: Project + @get:JsonIgnore @Transient @get:Input val name: String, + @get:JsonIgnore @Transient @get:Internal internal val project: Project ) : DokkaSourceSet { @Input @@ -117,6 +118,7 @@ open class GradleDokkaSourceSet constructor( @Optional var platform: String? = null + @JsonIgnore @Internal @Transient var collectKotlinTasks: (() -> List?)? = null diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/GradleDokkaConfigurationJsonTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/GradleDokkaConfigurationJsonTest.kt new file mode 100644 index 00000000..b6cf2119 --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/GradleDokkaConfigurationJsonTest.kt @@ -0,0 +1,94 @@ +package org.jetbrains.dokka.gradle + +import org.gradle.api.artifacts.FileCollectionDependency +import org.gradle.kotlin.dsl.withType +import org.gradle.testfixtures.ProjectBuilder +import org.jetbrains.dokka.* +import java.net.URL +import kotlin.test.Test +import kotlin.test.assertEquals + +class GradleDokkaConfigurationJsonTest { + + @Test + fun `DokkaTask configuration toJsonString then parseJson`() { + val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val dokkaTask = project.tasks.withType().first() + dokkaTask.plugins.withDependencies { dependencies -> + dependencies.clear() + } + dokkaTask.apply { + this.failOnWarning = true + this.offlineMode = true + this.outputDirectory = "customOutputDir" + this.cacheRoot = "customCacheRoot" + this.pluginsConfiguration["0"] = "a" + this.pluginsConfiguration["1"] = "b" + this.dokkaSourceSets.create("main") { sourceSet -> + sourceSet.moduleDisplayName = "moduleDisplayName" + sourceSet.displayName = "customSourceSetDisplayName" + sourceSet.reportUndocumented = true + + sourceSet.externalDocumentationLink { link -> + link.packageListUrl = URL("http://some.url") + link.url = URL("http://some.other.url") + } + sourceSet.collectKotlinTasks = { + println(this@GradleDokkaConfigurationJsonTest) + println("This lambda is capturing the entire test") + emptyList() + } + + sourceSet.perPackageOption { packageOption -> + packageOption.includeNonPublic = true + packageOption.reportUndocumented = true + packageOption.skipDeprecated = true + } + } + } + + val sourceConfiguration = dokkaTask.getConfigurationOrThrow() + val configurationJson = sourceConfiguration.toJsonString() + val parsedConfiguration = DokkaConfigurationImpl(configurationJson) + + assertEquals( + DokkaConfigurationImpl( + failOnWarning = sourceConfiguration.failOnWarning, + offlineMode = sourceConfiguration.offlineMode, + outputDir = sourceConfiguration.outputDir, + cacheRoot = sourceConfiguration.cacheRoot, + pluginsClasspath = emptyList(), + pluginsConfiguration = sourceConfiguration.pluginsConfiguration.toMap(), + sourceSets = listOf( + DokkaSourceSetImpl( + moduleDisplayName = sourceConfiguration.sourceSets.single().moduleDisplayName, + displayName = sourceConfiguration.sourceSets.single().displayName, + reportUndocumented = sourceConfiguration.sourceSets.single().reportUndocumented, + externalDocumentationLinks = sourceConfiguration.sourceSets.single().externalDocumentationLinks + .map { link -> + ExternalDocumentationLinkImpl( + url = link.url, + packageListUrl = link.packageListUrl + ) + }, + perPackageOptions = sourceConfiguration.sourceSets.single().perPackageOptions.map { option -> + PackageOptionsImpl( + prefix = option.prefix, + includeNonPublic = option.includeNonPublic, + reportUndocumented = option.reportUndocumented, + skipDeprecated = option.skipDeprecated, + suppress = option.suppress + ) + }, + sourceSetID = sourceConfiguration.sourceSets.single().sourceSetID, + sourceRoots = sourceConfiguration.sourceSets.single().sourceRoots.map { sourceRoot -> + SourceRootImpl(sourceRoot.path) + } + ) + ) + ), parsedConfiguration + ) + println(parsedConfiguration) + } +} -- cgit