From eae1ce49d18c2978b49166ea502bf2c109a85504 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Sat, 18 Jul 2020 12:18:59 +0200 Subject: Simplify Dokka Gradle Plugin --- core/src/main/kotlin/DokkaBootstrapImpl.kt | 34 +--------------- core/src/main/kotlin/configuration.kt | 46 ++++++++++++++++------ core/src/main/kotlin/defaultConfiguration.kt | 25 +++++++----- core/src/main/kotlin/utilities/json.kt | 4 +- .../kotlin/utilities/DokkaConfigurationJsonTest.kt | 10 ++--- 5 files changed, 57 insertions(+), 62 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/DokkaBootstrapImpl.kt b/core/src/main/kotlin/DokkaBootstrapImpl.kt index a2efab41..584d8b8c 100644 --- a/core/src/main/kotlin/DokkaBootstrapImpl.kt +++ b/core/src/main/kotlin/DokkaBootstrapImpl.kt @@ -70,38 +70,8 @@ class DokkaBootstrapImpl : DokkaBootstrap { private lateinit var generator: DokkaGenerator - fun configure(logger: DokkaLogger, configuration: DokkaConfigurationImpl) = with(configuration) { - - fun defaultLinks(config: DokkaSourceSetImpl): List { - val links = mutableListOf() - if (!config.noJdkLink) { - val javadocLink = - if (config.jdkVersion < 11) "https://docs.oracle.com/javase/${config.jdkVersion}/docs/api/" - else "https://docs.oracle.com/en/java/javase/${config.jdkVersion}/docs/api/java.base/" - val packageListLink = - if (config.jdkVersion < 11) "${javadocLink}/package-list" - else "https://docs.oracle.com/en/java/javase/${config.jdkVersion}/docs/api/element-list" - links += DokkaConfiguration.ExternalDocumentationLink - .Builder(javadocLink, packageListLink) - .build() as ExternalDocumentationLinkImpl - } - - if (!config.noStdlibLink) - links += DokkaConfiguration.ExternalDocumentationLink - .Builder("https://kotlinlang.org/api/latest/jvm/stdlib/") - .build() as ExternalDocumentationLinkImpl - return links - } - - val configurationWithLinks = configuration.copy( - sourceSets = sourceSets.map { - val links: List = - it.externalDocumentationLinks + defaultLinks(it) - it.copy(externalDocumentationLinks = links) - } - ) - - generator = DokkaGenerator(configurationWithLinks, logger) + fun configure(logger: DokkaLogger, configuration: DokkaConfigurationImpl) { + generator = DokkaGenerator(configuration, logger) } override fun configure(serializedConfigurationJSON: String, logger: BiConsumer) = configure( diff --git a/core/src/main/kotlin/configuration.kt b/core/src/main/kotlin/configuration.kt index 91ee4d64..67c55861 100644 --- a/core/src/main/kotlin/configuration.kt +++ b/core/src/main/kotlin/configuration.kt @@ -2,16 +2,16 @@ package org.jetbrains.dokka -import org.jetbrains.dokka.utilities.toJsonString import org.jetbrains.dokka.utilities.parseJson +import org.jetbrains.dokka.utilities.toJsonString import java.io.File import java.io.Serializable import java.net.URL object DokkaDefaults { - const val outputDir = "./dokka" + val outputDir = File("./dokka") const val format: String = "html" - val cacheRoot: String? = null + val cacheRoot: File? = null const val offlineMode: Boolean = false const val failOnWarning: Boolean = false @@ -51,6 +51,14 @@ enum class Platform(val key: String) { } } +interface DokkaConfigurationBuilder { + fun build(): T +} + +fun Iterable>.build(): List { + return this.map { it.build() } +} + data class DokkaSourceSetID( val moduleName: String, val sourceSetName: String @@ -65,8 +73,8 @@ fun DokkaConfigurationImpl(json: String): DokkaConfigurationImpl = parseJson(jso fun DokkaConfiguration.toJsonString(): String = toJsonString(this) interface DokkaConfiguration : Serializable { - val outputDir: String - val cacheRoot: String? + val outputDir: File + val cacheRoot: File? val offlineMode: Boolean val failOnWarning: Boolean val sourceSets: List @@ -78,11 +86,11 @@ interface DokkaConfiguration : Serializable { val sourceSetID: DokkaSourceSetID val displayName: String val moduleDisplayName: String - val classpath: List + val classpath: List val sourceRoots: List val dependentSourceSets: Set - val samples: List - val includes: List + val samples: List + val includes: List val includeNonPublic: Boolean val includeRootPackage: Boolean val reportUndocumented: Boolean @@ -96,12 +104,12 @@ interface DokkaConfiguration : Serializable { val apiVersion: String? val noStdlibLink: Boolean val noJdkLink: Boolean - val suppressedFiles: List + val suppressedFiles: List val analysisPlatform: Platform } interface SourceRoot : Serializable { - val path: String + val directory: File } interface SourceLinkDefinition : Serializable { @@ -112,8 +120,8 @@ interface DokkaConfiguration : Serializable { interface DokkaModuleDescription : Serializable { val name: String - val path: String - val docFile: String + val path: File + val docFile: File } interface PackageOptions : Serializable { @@ -135,7 +143,7 @@ interface DokkaConfiguration : Serializable { constructor(root: String, packageList: String? = null) : this(URL(root), packageList?.let { URL(it) }) - fun build(): ExternalDocumentationLink = + fun build(): ExternalDocumentationLinkImpl = if (packageListUrl != null && url != null) ExternalDocumentationLinkImpl(url!!, packageListUrl!!) else if (url != null) @@ -146,4 +154,16 @@ interface DokkaConfiguration : Serializable { } } +fun ExternalDocumentationLink( + url: URL? = null, + packageListUrl: URL? = null +): ExternalDocumentationLinkImpl = + DokkaConfiguration.ExternalDocumentationLink.Builder(url = url, packageListUrl = packageListUrl).build() + + +fun ExternalDocumentationLink( + url: String, packageListUrl: String? = null +): ExternalDocumentationLinkImpl = + DokkaConfiguration.ExternalDocumentationLink.Builder(root = url, packageList = packageListUrl).build() + diff --git a/core/src/main/kotlin/defaultConfiguration.kt b/core/src/main/kotlin/defaultConfiguration.kt index 8e38e8d5..384ce392 100644 --- a/core/src/main/kotlin/defaultConfiguration.kt +++ b/core/src/main/kotlin/defaultConfiguration.kt @@ -3,10 +3,12 @@ package org.jetbrains.dokka import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet import java.io.File import java.net.URL +import kotlin.reflect.full.memberProperties +import kotlin.reflect.full.primaryConstructor data class DokkaConfigurationImpl( - override val outputDir: String = DokkaDefaults.outputDir, - override val cacheRoot: String? = DokkaDefaults.cacheRoot, + override val outputDir: File = DokkaDefaults.outputDir, + override val cacheRoot: File? = DokkaDefaults.cacheRoot, override val offlineMode: Boolean = DokkaDefaults.offlineMode, override val sourceSets: List = emptyList(), override val pluginsClasspath: List = emptyList(), @@ -15,15 +17,16 @@ data class DokkaConfigurationImpl( override val failOnWarning: Boolean = DokkaDefaults.failOnWarning ) : DokkaConfiguration + data class DokkaSourceSetImpl( override val moduleDisplayName: String, override val displayName: String = DokkaDefaults.sourceSetDisplayName, override val sourceSetID: DokkaSourceSetID, - override val classpath: List = emptyList(), + override val classpath: List = emptyList(), override val sourceRoots: List, override val dependentSourceSets: Set = emptySet(), - override val samples: List = emptyList(), - override val includes: List = emptyList(), + override val samples: List = emptyList(), + override val includes: List = emptyList(), override val includeNonPublic: Boolean = DokkaDefaults.includeNonPublic, override val includeRootPackage: Boolean = DokkaDefaults.includeRootPackage, override val reportUndocumented: Boolean = DokkaDefaults.reportUndocumented, @@ -37,19 +40,21 @@ data class DokkaSourceSetImpl( override val apiVersion: String? = null, override val noStdlibLink: Boolean = DokkaDefaults.noStdlibLink, override val noJdkLink: Boolean = DokkaDefaults.noJdkLink, - override val suppressedFiles: List = emptyList(), + override val suppressedFiles: List = emptyList(), override val analysisPlatform: Platform = DokkaDefaults.analysisPlatform ) : DokkaSourceSet data class DokkaModuleDescriptionImpl( override val name: String, - override val path: String, - override val docFile: String + override val path: File, + override val docFile: File ) : DokkaConfiguration.DokkaModuleDescription data class SourceRootImpl( - override val path: String -) : DokkaConfiguration.SourceRoot + override val directory: File +) : DokkaConfiguration.SourceRoot { + constructor(directoryPath: String) : this(File(directoryPath)) +} data class SourceLinkDefinitionImpl( override val path: String, diff --git a/core/src/main/kotlin/utilities/json.kt b/core/src/main/kotlin/utilities/json.kt index 9e4677aa..feac2b23 100644 --- a/core/src/main/kotlin/utilities/json.kt +++ b/core/src/main/kotlin/utilities/json.kt @@ -56,10 +56,10 @@ private object FileSerializer : StdScalarSerializer(File::class.java) { private object SourceRootSerializer : StdScalarSerializer(SourceRoot::class.java) { override fun serialize(value: SourceRoot, g: JsonGenerator, provider: SerializerProvider) { - g.writeString(value.path) + g.writeString(value.directory.path) } } private object SourceRootImplDeserializer : StdScalarDeserializer(SourceRootImpl::class.java) { - override fun deserialize(p: JsonParser, ctxt: DeserializationContext): SourceRootImpl = SourceRootImpl(p.text) + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): SourceRootImpl = SourceRootImpl(File(p.text)) } diff --git a/core/src/test/kotlin/utilities/DokkaConfigurationJsonTest.kt b/core/src/test/kotlin/utilities/DokkaConfigurationJsonTest.kt index e954c82c..35755f50 100644 --- a/core/src/test/kotlin/utilities/DokkaConfigurationJsonTest.kt +++ b/core/src/test/kotlin/utilities/DokkaConfigurationJsonTest.kt @@ -9,12 +9,12 @@ class DokkaConfigurationJsonTest { @Test fun `simple configuration toJsonString then parseJson`() { val configuration = DokkaConfigurationImpl( - outputDir = "customOutputDir", + outputDir = File("customOutputDir"), pluginsClasspath = listOf(File("plugins/customPlugin.jar")), sourceSets = listOf( DokkaSourceSetImpl( moduleDisplayName = "customModuleDisplayName", - sourceRoots = listOf(SourceRootImpl("customSourceRoot")), + sourceRoots = listOf(SourceRootImpl(File("customSourceRoot"))), sourceSetID = DokkaSourceSetID("customModuleName", "customSourceSetName") ) ) @@ -48,14 +48,14 @@ class DokkaConfigurationJsonTest { val parsedConfiguration = DokkaConfigurationImpl(json) assertEquals( DokkaConfigurationImpl( - outputDir = "customOutputDir", + outputDir = File("customOutputDir"), pluginsClasspath = listOf(File("plugins/customPlugin.jar")), sourceSets = listOf( DokkaSourceSetImpl( moduleDisplayName = "customModuleDisplayName", - sourceRoots = listOf(SourceRootImpl("customSourceRoot")), + sourceRoots = listOf(SourceRootImpl(File("customSourceRoot"))), sourceSetID = DokkaSourceSetID("customModuleName", "customSourceSetName"), - classpath = listOf("classpath/custom1.jar", "classpath/custom2.jar") + classpath = listOf(File("classpath/custom1.jar"), File("classpath/custom2.jar")) ) ) ), -- cgit