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 --- .../kotlin/allModulePage/MultimodulePageCreator.kt | 7 ++-- .../base/src/main/kotlin/renderers/FileWriter.kt | 14 +++---- .../ModuleAndPackageDocumentationTransformer.kt | 6 +-- .../SuppressedDocumentableFilterTransformer.kt | 2 +- .../pages/samples/SamplesTransformer.kt | 10 ++--- .../DefaultDescriptorToDocumentableTranslator.kt | 19 ++++++--- .../psi/DefaultPsiToDocumentableTranslator.kt | 47 ++++++++++++++-------- .../src/test/kotlin/expect/AbstractExpectTest.kt | 2 +- .../renderers/html/HtmlRenderingOnlyTestBase.kt | 2 +- 9 files changed, 66 insertions(+), 43 deletions(-) (limited to 'plugins/base') diff --git a/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt b/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt index de2242f2..4be84749 100644 --- a/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt +++ b/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt @@ -43,7 +43,7 @@ class MultimodulePageCreator( header(2, "All modules:") table(styles = setOf(MultimoduleTable)) { modules.mapNotNull { module -> - val paragraph = module.docFile.let(::File).readText().let { parser.parse(it).firstParagraph() } + val paragraph = module.docFile.readText().let { parser.parse(it).firstParagraph() } paragraph?.let { val dri = DRI(packageName = MULTIMODULE_PACKAGE_PLACEHOLDER, classNames = module.name) val dci = DCI(setOf(dri), ContentKind.Main) @@ -68,10 +68,9 @@ class MultimodulePageCreator( } private fun throwOnMissingModuleDocFile(module: DokkaConfiguration.DokkaModuleDescription) { - val docFile = File(module.docFile) - if (!docFile.exists() || !docFile.isFile) { + if (!module.docFile.exists() || !module.docFile.isFile) { throw DokkaException( - "Missing documentation file for module ${module.name}: ${docFile.absolutePath}" + "Missing documentation file for module ${module.name}: ${module.docFile.absolutePath}" ) } } diff --git a/plugins/base/src/main/kotlin/renderers/FileWriter.kt b/plugins/base/src/main/kotlin/renderers/FileWriter.kt index 181295c0..cd38f1b9 100644 --- a/plugins/base/src/main/kotlin/renderers/FileWriter.kt +++ b/plugins/base/src/main/kotlin/renderers/FileWriter.kt @@ -21,10 +21,10 @@ class FileWriter(val context: DokkaContext): OutputWriter { createdFiles.add(path) try { - val dir = Paths.get(root, path.dropLastWhile { it != '/' }).toFile() + val dir = Paths.get(root.absolutePath, path.dropLastWhile { it != '/' }).toFile() withContext(Dispatchers.IO) { dir.mkdirsOrFail() - Files.write(Paths.get(root, "$path$ext"), text.lines()) + Files.write(Paths.get(root.absolutePath, "$path$ext"), text.lines()) } } catch (e: Throwable) { context.logger.error("Failed to write $this. ${e.message}") @@ -41,7 +41,7 @@ class FileWriter(val context: DokkaContext): OutputWriter { private suspend fun copyFromDirectory(pathFrom: String, pathTo: String) { - val dest = Paths.get(root, pathTo).toFile() + val dest = Paths.get(root.path, pathTo).toFile() val uri = javaClass.getResource(pathFrom).toURI() withContext(Dispatchers.IO) { File(uri).copyRecursively(dest, true) @@ -51,7 +51,7 @@ class FileWriter(val context: DokkaContext): OutputWriter { private suspend fun copyFromJar(pathFrom: String, pathTo: String) { val rebase = fun(path: String) = "$pathTo/${path.removePrefix(pathFrom)}" - val dest = Paths.get(root, pathTo).toFile() + val dest = Paths.get(root.path, pathTo).toFile() dest.mkdirsOrFail() val uri = javaClass.getResource(pathFrom).toURI() val fs = getFileSystemForURI(uri) @@ -60,12 +60,12 @@ class FileWriter(val context: DokkaContext): OutputWriter { if (Files.isDirectory(file)) { val dirPath = file.toAbsolutePath().toString() withContext(Dispatchers.IO) { - Paths.get(root, rebase(dirPath)).toFile().mkdirsOrFail() + Paths.get(root.path, rebase(dirPath)).toFile().mkdirsOrFail() } } else { val filePath = file.toAbsolutePath().toString() withContext(Dispatchers.IO) { - Paths.get(root, rebase(filePath)).toFile().writeBytes( + Paths.get(root.path, rebase(filePath)).toFile().writeBytes( this@FileWriter.javaClass.getResourceAsStream(filePath).readBytes() ) } @@ -85,4 +85,4 @@ class FileWriter(val context: DokkaContext): OutputWriter { } catch (e: FileSystemAlreadyExistsException) { FileSystems.getFileSystem(uri) } -} \ No newline at end of file +} diff --git a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt index 4a98a5e0..71824922 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt @@ -22,9 +22,9 @@ internal class ModuleAndPackageDocumentationTransformer( val modulesAndPackagesDocumentation = context.configuration.sourceSets - .map { - Pair(it.moduleDisplayName, it) to - it.includes.map { Paths.get(it) } + .map { sourceSet -> + Pair(sourceSet.moduleDisplayName, sourceSet) to + sourceSet.includes.map { it.toPath() } .also { it.forEach { if (Files.notExists(it)) diff --git a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt index 79feb832..7febabbb 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt @@ -45,7 +45,7 @@ class SuppressedDocumentableFilterTransformer(val context: DokkaContext) : PreMe if (documentable !is WithExpectActual) return false val sourceFile = File(source(documentable).path).absoluteFile return sourceSet(documentable).suppressedFiles.any { suppressedFile -> - sourceFile.startsWith(File(suppressedFile).absoluteFile) + sourceFile.startsWith(suppressedFile.absoluteFile) } } } diff --git a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt index 695ef050..f13e52ab 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt @@ -43,16 +43,16 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { } } - private fun setUpAnalysis(context: DokkaContext) = context.configuration.sourceSets.map { - it to AnalysisEnvironment(DokkaMessageCollector(context.logger), it.analysisPlatform).run { + private fun setUpAnalysis(context: DokkaContext) = context.configuration.sourceSets.map { sourceSet -> + sourceSet to AnalysisEnvironment(DokkaMessageCollector(context.logger), sourceSet.analysisPlatform).run { if (analysisPlatform == Platform.jvm) { addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) } - it.classpath.forEach { addClasspath(File(it)) } + sourceSet.classpath.forEach(::addClasspath) - addSources(it.samples.map { it }) + addSources(sourceSet.samples.toList()) - loadLanguageVersionSettings(it.languageVersion, it.apiVersion) + loadLanguageVersionSettings(sourceSet.languageVersion, sourceSet.apiVersion) val environment = createCoreEnvironment() val (facade, _) = createResolutionFacade(environment) diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index c4c6483f..1ac4edf7 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -95,7 +95,7 @@ private class DokkaDescriptorVisitor( private fun Collection.filterDescriptorsInSourceSet() = filter { it.toSourceElement.containingFile.toString().let { path -> path.isNotBlank() && sourceSet.sourceRoots.any { root -> - Paths.get(path).startsWith(Paths.get(root.path)) + Paths.get(path).startsWith(root.directory.toPath()) } } } @@ -356,7 +356,8 @@ private class DokkaDescriptorVisitor( sources = actual, visibility = descriptor.visibility.toDokkaVisibility().toSourceSetDependent(), generics = descriptor.typeParameters.map { it.toTypeParameter() }, - documentation = descriptor.takeIf { it.kind != CallableMemberDescriptor.Kind.SYNTHESIZED }?.resolveDescriptorData() ?: emptyMap(), + documentation = descriptor.takeIf { it.kind != CallableMemberDescriptor.Kind.SYNTHESIZED } + ?.resolveDescriptorData() ?: emptyMap(), modifier = descriptor.modifier().toSourceSetDependent(), type = descriptor.returnType!!.toBound(), sourceSets = setOf(sourceSet), @@ -746,11 +747,19 @@ private class DokkaDescriptorVisitor( private data class InheritanceLevel(val level: Int, val superclass: DRI?, val interfaces: List) - private data class ClassInfo(val inheritance: List, val docs: SourceSetDependent){ + private data class ClassInfo( + val inheritance: List, + val docs: SourceSetDependent + ) { val supertypes: List get() = inheritance.firstOrNull { it.level == 0 }?.let { - listOfNotNull(it.superclass?.let { DriWithKind(it, KotlinClassKindTypes.CLASS) }) + it.interfaces.map { DriWithKind(it, KotlinClassKindTypes.INTERFACE) } - }.orEmpty() + listOfNotNull(it.superclass?.let { + DriWithKind( + it, + KotlinClassKindTypes.CLASS + ) + }) + it.interfaces.map { DriWithKind(it, KotlinClassKindTypes.INTERFACE) } + }.orEmpty() val allImplementedInterfaces: List get() = inheritance.flatMap { it.interfaces }.distinct() diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 9ed37c30..cd43e635 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -43,8 +43,8 @@ class DefaultPsiToDocumentableTranslator( override fun invoke(sourceSet: DokkaSourceSet, context: DokkaContext): DModule { - fun isFileInSourceRoots(file: File) : Boolean { - return sourceSet.sourceRoots.any { root -> file.path.startsWith(File(root.path).absolutePath) } + fun isFileInSourceRoots(file: File): Boolean { + return sourceSet.sourceRoots.any { root -> file.startsWith(root.directory) } } val (environment, _) = kotlinAnalysis[sourceSet] @@ -133,7 +133,7 @@ class DefaultPsiToDocumentableTranslator( val superMethods = mutableListOf>() methods.forEach { superMethodsKeys.add(it.hash) } fun parseSupertypes(superTypes: Array, level: Int = 0) { - if(superTypes.isEmpty()) return + if (superTypes.isEmpty()) return val parsedClasses = superTypes.filter { !it.shouldBeIgnored }.mapNotNull { it.resolve()?.let { when { @@ -178,7 +178,8 @@ class DefaultPsiToDocumentableTranslator( }) + it.interfaces.map { DriWithKind(dri = it, kind = JavaClassKindTypes.INTERFACE) } }.toSourceSetDependent() val modifiers = getModifier().toSourceSetDependent() - val implementedInterfacesExtra = ImplementedInterfaces(inheritanceTree.flatMap { it.interfaces }.distinct().toSourceSetDependent()) + val implementedInterfacesExtra = + ImplementedInterfaces(inheritanceTree.flatMap { it.interfaces }.distinct().toSourceSetDependent()) return when { isAnnotationType -> DAnnotation( @@ -195,8 +196,11 @@ class DefaultPsiToDocumentableTranslator( constructors.map { parseFunction(it, true) }, mapTypeParameters(dri), setOf(sourceSetData), - PropertyContainer.withAll(implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() - .toAnnotations()) + PropertyContainer.withAll( + implementedInterfacesExtra, + annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) isEnum -> DEnum( dri, @@ -211,8 +215,11 @@ class DefaultPsiToDocumentableTranslator( emptyList(), emptyList(), setOf(sourceSetData), - PropertyContainer.withAll(implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() - .toAnnotations()) + PropertyContainer.withAll( + implementedInterfacesExtra, + annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) }, documentation, @@ -226,8 +233,10 @@ class DefaultPsiToDocumentableTranslator( constructors.map { parseFunction(it, true) }, ancestors, setOf(sourceSetData), - PropertyContainer.withAll(implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() - .toAnnotations()) + PropertyContainer.withAll( + implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) isInterface -> DInterface( dri, @@ -243,8 +252,10 @@ class DefaultPsiToDocumentableTranslator( mapTypeParameters(dri), ancestors, setOf(sourceSetData), - PropertyContainer.withAll(implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() - .toAnnotations()) + PropertyContainer.withAll( + implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) else -> DClass( dri, @@ -262,8 +273,10 @@ class DefaultPsiToDocumentableTranslator( null, modifiers, setOf(sourceSetData), - PropertyContainer.withAll(implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() - .toAnnotations()) + PropertyContainer.withAll( + implementedInterfacesExtra, annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) } } @@ -305,7 +318,8 @@ class DefaultPsiToDocumentableTranslator( PropertyContainer.withAll( InheritedFunction(inheritedFrom.toSourceSetDependent()), it.toSourceSetDependent().toAdditionalModifiers(), - (psi.annotations.toList().toListOfAnnotations() + it.toListOfAnnotations()).toSourceSetDependent() + (psi.annotations.toList() + .toListOfAnnotations() + it.toListOfAnnotations()).toSourceSetDependent() .toAnnotations() ) } @@ -434,7 +448,8 @@ class DefaultPsiToDocumentableTranslator( psi.additionalExtras().let { PropertyContainer.withAll( it.toSourceSetDependent().toAdditionalModifiers(), - (psi.annotations.toList().toListOfAnnotations() + it.toListOfAnnotations()).toSourceSetDependent() + (psi.annotations.toList() + .toListOfAnnotations() + it.toListOfAnnotations()).toSourceSetDependent() .toAnnotations() ) } diff --git a/plugins/base/src/test/kotlin/expect/AbstractExpectTest.kt b/plugins/base/src/test/kotlin/expect/AbstractExpectTest.kt index 4dfdc410..57571cd3 100644 --- a/plugins/base/src/test/kotlin/expect/AbstractExpectTest.kt +++ b/plugins/base/src/test/kotlin/expect/AbstractExpectTest.kt @@ -24,7 +24,7 @@ abstract class AbstractExpectTest( var result: Path? = null testFromData(config, cleanupOutput = false) { - renderingStage = { _, context -> result = Paths.get(context.configuration.outputDir) } + renderingStage = { _, context -> result = context.configuration.outputDir.toPath() } } return result } diff --git a/plugins/base/src/test/kotlin/renderers/html/HtmlRenderingOnlyTestBase.kt b/plugins/base/src/test/kotlin/renderers/html/HtmlRenderingOnlyTestBase.kt index b6765fda..ae494929 100644 --- a/plugins/base/src/test/kotlin/renderers/html/HtmlRenderingOnlyTestBase.kt +++ b/plugins/base/src/test/kotlin/renderers/html/HtmlRenderingOnlyTestBase.kt @@ -52,7 +52,7 @@ abstract class HtmlRenderingOnlyTestBase : RenderingOnlyTestBase() { DokkaBase().externalLocationProviderFactory to { ::DokkaExternalLocationProviderFactory }, DokkaBase().tabSortingStrategy to { DefaultTabSortingStrategy() }, testConfiguration = DokkaConfigurationImpl( - "", null, false, listOf(js, jvm, native), emptyList(), emptyMap(), emptyList(), false + sourceSets = listOf(js, jvm, native) ) ) -- cgit