package org.jetbrains.dokka.gradle import groovy.lang.Closure import org.gradle.api.DefaultTask import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaPluginConvention import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskAction import org.jetbrains.dokka.DokkaGenerator import org.jetbrains.dokka.SourceLinkDefinition import java.io.File import java.util.ArrayList public open class DokkaPlugin : Plugin { override fun apply(project: Project) { project.tasks.create("dokka", DokkaTask::class.java).apply { moduleName = project.name outputDirectory = File(project.buildDir, "dokka").absolutePath } } } public open class DokkaTask : DefaultTask() { init { group = JavaBasePlugin.DOCUMENTATION_GROUP description = "Generates dokka documentation for Kotlin" } var moduleName: String = "" var outputFormat: String = "html" var outputDirectory: String = "" var processConfigurations: ArrayList = arrayListOf("compile") var includes: ArrayList = arrayListOf() var linkMappings: ArrayList = arrayListOf() var samples: ArrayList = arrayListOf() fun linkMapping(closure: Closure) { val mapping = LinkMapping() closure.delegate = mapping closure.call() if (mapping.dir.isEmpty()) { throw IllegalArgumentException("Link mapping should have dir") } if (mapping.url.isEmpty()) { throw IllegalArgumentException("Link mapping should have url") } linkMappings.add(mapping) } @TaskAction fun generate() { val project = project val javaPluginConvention = project.convention.getPlugin(JavaPluginConvention::class.java) val sourceSets = javaPluginConvention.sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) val sourceDirectories = sourceSets?.allSource?.srcDirs?.filter { it.exists() } ?: emptyList() val allConfigurations = project.configurations val classpath = processConfigurations .map { allConfigurations?.getByName(it) ?: throw IllegalArgumentException("No configuration $it found") } .flatMap { it } if (sourceDirectories.isEmpty()) { logger.warn("No source directories found: skipping dokka generation") return } DokkaGenerator( DokkaGradleLogger(logger), classpath.map { it.absolutePath }, sourceDirectories.map { it.absolutePath }, samples, includes, moduleName, outputDirectory, outputFormat, linkMappings.map { SourceLinkDefinition(project.file(it.dir).absolutePath, it.url, it.suffix) }, false ).generate() } } public open class LinkMapping { var dir: String = "" var url: String = "" var suffix: String? = null }