From 7c56f29fcf550faaf22b81bed4afed8e2f803be1 Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Wed, 10 Apr 2019 16:04:02 +0200 Subject: Rework Gradle plugin, autoconfiguration not working yet --- runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runners/android-gradle-plugin/src/main') diff --git a/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt index b1996da0..2fbeaae7 100644 --- a/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt +++ b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.Input +import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink.Builder import java.io.File @@ -23,7 +24,7 @@ open class DokkaAndroidTask : DokkaTask() { @Input var noAndroidSdkLink: Boolean = false - override fun collectSuppressedFiles(sourceRoots: List): List { + override fun collectSuppressedFiles(sourceRoots: List): List { val generatedRoot = project.buildDir.resolve("generated").absoluteFile return sourceRoots .map { File(it.path) } -- cgit From d5864c672135bd2f5c7ce4fea5aeccd861796a47 Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Mon, 15 Apr 2019 13:26:26 +0200 Subject: Light refactoring --- .../src/main/kotlin/mainAndroid.kt | 4 +- .../kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt | 275 +++++++++++++++++++++ .../org/jetbrains/dokka/gradle/ProxyUtils.kt | 4 +- .../dokka/gradle/configurationImplementations.kt | 35 +-- .../kotlin/org/jetbrains/dokka/gradle/dokkaTask.kt | 265 -------------------- .../main/kotlin/org/jetbrains/dokka/gradle/main.kt | 5 +- 6 files changed, 300 insertions(+), 288 deletions(-) create mode 100644 runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt delete mode 100644 runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/dokkaTask.kt (limited to 'runners/android-gradle-plugin/src/main') diff --git a/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt index 2fbeaae7..367b9257 100644 --- a/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt +++ b/runners/android-gradle-plugin/src/main/kotlin/mainAndroid.kt @@ -3,8 +3,8 @@ package org.jetbrains.dokka.gradle import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.Input -import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink.Builder +import org.jetbrains.dokka.DokkaConfiguration.SourceRoot import java.io.File open class DokkaAndroidPlugin : Plugin { @@ -24,7 +24,7 @@ open class DokkaAndroidTask : DokkaTask() { @Input var noAndroidSdkLink: Boolean = false - override fun collectSuppressedFiles(sourceRoots: List): List { + override fun collectSuppressedFiles(sourceRoots: List): List { val generatedRoot = project.buildDir.resolve("generated").absoluteFile return sourceRoots .map { File(it.path) } 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 new file mode 100644 index 00000000..e8067e4c --- /dev/null +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaTask.kt @@ -0,0 +1,275 @@ +package org.jetbrains.dokka.gradle + +import com.google.gson.GsonBuilder +import groovy.lang.Closure +import org.gradle.api.DefaultTask +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.artifacts.Configuration +import org.gradle.api.file.FileCollection +import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.plugins.JavaPluginConvention +import org.gradle.api.tasks.* +import org.gradle.api.tasks.compile.AbstractCompile +import org.jetbrains.dokka.DokkaBootstrap +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.DokkaConfiguration.SourceRoot +import org.jetbrains.dokka.ReflectDsl +import org.jetbrains.dokka.ReflectDsl.isNotInstance +import java.io.File +import java.io.Serializable +import java.net.URLClassLoader +import java.util.concurrent.Callable +import java.util.function.BiConsumer + +open class DokkaTask : DefaultTask() { + + fun defaultKotlinTasks() = with(ReflectDsl) { + val abstractKotlinCompileClz = try { + project.buildscript.classLoader.loadClass(ABSTRACT_KOTLIN_COMPILE) + } catch (cnfe: ClassNotFoundException) { + logger.warn("$ABSTRACT_KOTLIN_COMPILE class not found, default kotlin tasks ignored") + return@with emptyList() + } + + return@with project.tasks.filter { it isInstance abstractKotlinCompileClz }.filter { "Test" !in it.name } + } + + init { + group = JavaBasePlugin.DOCUMENTATION_GROUP + description = "Generates dokka documentation for Kotlin" + + @Suppress("LeakingThis") + dependsOn(Callable { kotlinTasks.map { it.taskDependencies } }) + } + + @Input + var moduleName: String = "" + + @Input + var outputFormat: String = "html" + + @OutputDirectory + var outputDirectory: String = "" + + var dokkaRuntime: Configuration? = null + + @InputFiles + var classpath: Iterable = arrayListOf() + + @Input + var sourceDirs: Iterable = emptyList() + + @Input + var sourceRoots: MutableList = arrayListOf() + + @Input + var dokkaFatJar: Any = "org.jetbrains.dokka:dokka-fatjar:${DokkaVersion.version}" + + @Input + var impliedPlatforms: MutableList = arrayListOf() + + @Optional + @Input + var cacheRoot: String? = null + + @Input + var collectInheritedExtensionsFromLibraries: Boolean = false + + @get:Internal + internal val kotlinCompileBasedClasspathAndSourceRoots: ClasspathAndSourceRoots by lazy { extractClasspathAndSourceRootsFromKotlinTasks() } + + protected var externalDocumentationLinks: MutableList = mutableListOf() + + private var kotlinTasksConfigurator: () -> List? = { defaultKotlinTasks() } + private val kotlinTasks: List by lazy { extractKotlinCompileTasks() } + + fun kotlinTasks(taskSupplier: Callable>) { + kotlinTasksConfigurator = { taskSupplier.call() } + } + + fun kotlinTasks(closure: Closure) { + kotlinTasksConfigurator = { closure.call() as? List } + } + + + fun tryResolveFatJar(project: Project): Set { + return try { + dokkaRuntime!!.resolve() + } catch (e: Exception) { + project.parent?.let { tryResolveFatJar(it) } ?: throw e + } + } + + fun loadFatJar() { + if (ClassloaderContainer.fatJarClassLoader == null) { + val jars = if (dokkaFatJar is File) + setOf(dokkaFatJar as File) + else + tryResolveFatJar(project) + ClassloaderContainer.fatJarClassLoader = URLClassLoader(jars.map { it.toURI().toURL() }.toTypedArray(), ClassLoader.getSystemClassLoader().parent) + } + } + + internal data class ClasspathAndSourceRoots(val classpathFileCollection: FileCollection, val sourceRoots: List) : + Serializable + + private fun extractKotlinCompileTasks(): List { + val inputList = (kotlinTasksConfigurator.invoke() ?: emptyList()).filterNotNull() + val (paths, other) = inputList.partition { it is String } + + val taskContainer = project.tasks + + val tasksByPath = paths.map { taskContainer.findByPath(it as String) ?: throw IllegalArgumentException("Task with path '$it' not found") } + + other + .filter { it !is Task || it isNotInstance getAbstractKotlinCompileFor(it) } + .forEach { throw IllegalArgumentException("Illegal entry in kotlinTasks, must be subtype of $ABSTRACT_KOTLIN_COMPILE or String, but was $it") } + + tasksByPath + .filter { it == null || it isNotInstance getAbstractKotlinCompileFor(it) } + .forEach { throw IllegalArgumentException("Illegal task path in kotlinTasks, must be subtype of $ABSTRACT_KOTLIN_COMPILE, but was $it") } + + + return (tasksByPath + other) as List + } + + private fun extractClasspathAndSourceRootsFromKotlinTasks(): ClasspathAndSourceRoots { + + val allTasks = kotlinTasks + + val allClasspath = mutableSetOf() + var allClasspathFileCollection: FileCollection = project.files() + val allSourceRoots = mutableSetOf() + + allTasks.forEach { + logger.debug("Dokka found AbstractKotlinCompile task: $it") + with(ReflectDsl) { + val taskSourceRoots: List = it["sourceRootsContainer"]["sourceRoots"].v() + + val abstractKotlinCompileClz = getAbstractKotlinCompileFor(it)!! + + val taskClasspath: Iterable = + (it["getClasspath", AbstractCompile::class].takeIfIsFunc()?.invoke() + ?: it["compileClasspath", abstractKotlinCompileClz].takeIfIsProp()?.v() + ?: it["getClasspath", abstractKotlinCompileClz]()) + + if (taskClasspath is FileCollection) { + allClasspathFileCollection += taskClasspath + } else { + allClasspath += taskClasspath + } + allSourceRoots += taskSourceRoots.filter { it.exists() } + } + } + + return ClasspathAndSourceRoots(allClasspathFileCollection + project.files(allClasspath), allSourceRoots.toList()) + } + + private fun Iterable.toSourceRoots(): List = this.filter { it.exists() }.map { GradleSourceRootImpl().apply { path = it.path } } + + protected open fun collectSuppressedFiles(sourceRoots: List): List = emptyList() + + @TaskAction + fun generate() { + if (dokkaRuntime == null){ + dokkaRuntime = project.configurations.getByName("dokkaRuntime") + } + + dokkaRuntime?.defaultDependencies{ dependencies -> dependencies.add(project.dependencies.create(dokkaFatJar)) } + val kotlinColorsEnabledBefore = System.getProperty(COLORS_ENABLED_PROPERTY) ?: "false" + System.setProperty(COLORS_ENABLED_PROPERTY, "false") + try { + loadFatJar() + // TODO: implement extracting source roots from kotlin tasks + val (_, tasksSourceRoots) = kotlinCompileBasedClasspathAndSourceRoots + + val sourceRoots = collectSourceRoots() + tasksSourceRoots.toSourceRoots() + + val bootstrapClass = ClassloaderContainer.fatJarClassLoader!!.loadClass("org.jetbrains.dokka.DokkaBootstrapImpl") + val bootstrapInstance = bootstrapClass.constructors.first().newInstance() + val bootstrapProxy: DokkaBootstrap = + automagicTypedProxy(javaClass.classLoader, bootstrapInstance) + + val gson = GsonBuilder().setPrettyPrinting().create() + + val passConfigurationExtension: GradlePassConfigurationImpl? = extensions.findByName( + CONFIGURATION_EXTENSION_NAME) as GradlePassConfigurationImpl? + val passConfigurationsContainer by lazy { + (extensions.getByName(MULTIPLATFORM_EXTENSION_NAME) as Iterable).toList() + } + passConfigurationExtension?.sourceRoots?.addAll(sourceRoots) + + val passConfigurationList = + (passConfigurationExtension?.let {passConfigurationsContainer + it } ?: passConfigurationsContainer) + .map { defaultPassConfiguration(it) } + + val configuration = GradleDokkaConfigurationImpl() + configuration.outputDir = outputDirectory + configuration.format = outputFormat + configuration.generateIndexPages = true + configuration.cacheRoot = cacheRoot + configuration.impliedPlatforms = impliedPlatforms + configuration.passesConfigurations = passConfigurationList + + bootstrapProxy.configure( + BiConsumer { level, message -> + when (level) { + "info" -> logger.info(message) + "warn" -> logger.warn(message) + "error" -> logger.error(message) + } + }, + gson.toJson(configuration) + ) + + bootstrapProxy.generate() + + } finally { + System.setProperty(COLORS_ENABLED_PROPERTY, kotlinColorsEnabledBefore) + } + } + + private fun defaultPassConfiguration(passConfig: GradlePassConfigurationImpl): GradlePassConfigurationImpl{ + val (tasksClasspath, _) = kotlinCompileBasedClasspathAndSourceRoots + + val fullClasspath = tasksClasspath + classpath + passConfig.moduleName = moduleName + passConfig.classpath = fullClasspath.map { it.absolutePath } + passConfig.samples = passConfig.samples.map { project.file(it).absolutePath } + passConfig.includes = passConfig.includes.map { project.file(it).absolutePath } + passConfig.collectInheritedExtensionsFromLibraries = collectInheritedExtensionsFromLibraries + passConfig.suppressedFiles = collectSuppressedFiles(passConfig.sourceRoots) + passConfig.externalDocumentationLinks.addAll(externalDocumentationLinks) + + return passConfig + } + + private fun collectSourceRoots(): List { + val sourceDirs = when { + sourceDirs.any() -> { + logger.info("Dokka: Taking source directories provided by the user") + sourceDirs.toSet() + } + kotlinTasks.isEmpty() -> project.convention.findPlugin(JavaPluginConvention::class.java)?.let { javaPluginConvention -> + logger.info("Dokka: Taking source directories from default java plugin") + val sourceSets = javaPluginConvention.sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME) + sourceSets?.allSource?.srcDirs + } + else -> emptySet() + } + + return sourceRoots + (sourceDirs?.toSourceRoots() ?: emptyList()) + } + + companion object { + const val COLORS_ENABLED_PROPERTY = "kotlin.colors.enabled" + const val ABSTRACT_KOTLIN_COMPILE = "org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile" + + private fun getAbstractKotlinCompileFor(task: Task) = try { + task.project.buildscript.classLoader.loadClass(ABSTRACT_KOTLIN_COMPILE) + } catch (e: ClassNotFoundException) { + null + } + } +} diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ProxyUtils.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ProxyUtils.kt index 61c9e773..f8965993 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ProxyUtils.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/ProxyUtils.kt @@ -26,8 +26,8 @@ inline fun automagicTypedProxy(targetClassLoader: ClassLoader, */ fun automagicProxy(targetClassLoader: ClassLoader, targetType: Class<*>, delegate: Any): Any = Proxy.newProxyInstance( - targetClassLoader, - arrayOf(targetType), + targetClassLoader, + arrayOf(targetType), DelegatedInvocationHandler(delegate) ) 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 bab36f29..56bfe262 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 @@ -4,12 +4,13 @@ import groovy.lang.Closure import org.gradle.api.Action import org.gradle.util.ConfigureUtil import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.DokkaConfiguration.* import org.jetbrains.dokka.Platform import java.io.File import java.io.Serializable import java.net.URL -class GradleSourceRootImpl: DokkaConfiguration.SourceRoot, Serializable{ +class GradleSourceRootImpl: SourceRoot, Serializable { override var path: String = "" set(value) { field = File(value).absolutePath @@ -18,10 +19,10 @@ class GradleSourceRootImpl: DokkaConfiguration.SourceRoot, Serializable{ override fun toString(): String = path } -open class GradlePassConfigurationImpl(@Transient val name: String = ""): DokkaConfiguration.PassConfiguration { +open class GradlePassConfigurationImpl(@Transient val name: String = ""): PassConfiguration { override var classpath: List = emptyList() override var moduleName: String = "" - override var sourceRoots: MutableList = mutableListOf() + override var sourceRoots: MutableList = mutableListOf() override var samples: List = emptyList() override var includes: List = emptyList() override var includeNonPublic: Boolean = false @@ -30,9 +31,9 @@ open class GradlePassConfigurationImpl(@Transient val name: String = ""): DokkaC override var skipEmptyPackages: Boolean = false override var skipDeprecated: Boolean = false override var jdkVersion: Int = 6 - override var sourceLinks: MutableList = mutableListOf() - override var perPackageOptions: MutableList = mutableListOf() - override var externalDocumentationLinks: MutableList = mutableListOf() + override var sourceLinks: MutableList = mutableListOf() + override var perPackageOptions: MutableList = mutableListOf() + override var externalDocumentationLinks: MutableList = mutableListOf() override var languageVersion: String? = null override var apiVersion: String? = null override var noStdlibLink: Boolean = false @@ -43,58 +44,58 @@ open class GradlePassConfigurationImpl(@Transient val name: String = ""): DokkaC override var targets: List = listOf("JVM") override var sinceKotlin: String = "1.0" - fun sourceRoot(c: Closure){ + fun sourceRoot(c: Closure) { val configured = ConfigureUtil.configure(c, GradleSourceRootImpl()) sourceRoots.add(configured) } - fun sourceRoot(action: Action){ + fun sourceRoot(action: Action) { val sourceRoot = GradleSourceRootImpl() action.execute(sourceRoot) sourceRoots.add(sourceRoot) } - fun sourceLink(c: Closure){ + fun sourceLink(c: Closure) { val configured = ConfigureUtil.configure(c, GradleSourceLinkDefinitionImpl()) sourceLinks.add(configured) } - fun sourceLink(action: Action){ + fun sourceLink(action: Action) { val sourceLink = GradleSourceLinkDefinitionImpl() action.execute(sourceLink) sourceLinks.add(sourceLink) } - fun perPackageOption(c: Closure){ + fun perPackageOption(c: Closure) { val configured = ConfigureUtil.configure(c, GradlePackageOptionsImpl()) perPackageOptions.add(configured) } - fun perPackageOption(action: Action){ + fun perPackageOption(action: Action) { val option = GradlePackageOptionsImpl() action.execute(option) perPackageOptions.add(option) } - fun externalDocumentationLink(c: Closure){ + fun externalDocumentationLink(c: Closure) { val link = ConfigureUtil.configure(c, GradleExternalDocumentationLinkImpl()) externalDocumentationLinks.add(link) } - fun externalDocumentationLink(action: Action){ + fun externalDocumentationLink(action: Action) { val link = GradleExternalDocumentationLinkImpl() action.execute(link) externalDocumentationLinks.add(link) } } -class GradleSourceLinkDefinitionImpl : DokkaConfiguration.SourceLinkDefinition { +class GradleSourceLinkDefinitionImpl : SourceLinkDefinition { override var path: String = "" override var url: String = "" override var lineSuffix: String? = null } -class GradleExternalDocumentationLinkImpl : DokkaConfiguration.ExternalDocumentationLink { +class GradleExternalDocumentationLinkImpl : ExternalDocumentationLink { override var url: URL = URL("") override var packageListUrl: URL = URL("") } @@ -108,7 +109,7 @@ class GradleDokkaConfigurationImpl: DokkaConfiguration { override var passesConfigurations: List = emptyList() } -class GradlePackageOptionsImpl: DokkaConfiguration.PackageOptions { +class GradlePackageOptionsImpl: PackageOptions { override var prefix: String = "" override val includeNonPublic: Boolean = false override val reportUndocumented: Boolean = true 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 deleted file mode 100644 index 0d78461c..00000000 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/dokkaTask.kt +++ /dev/null @@ -1,265 +0,0 @@ -package org.jetbrains.dokka.gradle - -import com.google.gson.GsonBuilder -import groovy.lang.Closure -import org.gradle.api.DefaultTask -import org.gradle.api.Project -import org.gradle.api.Task -import org.gradle.api.artifacts.Configuration -import org.gradle.api.file.FileCollection -import org.gradle.api.plugins.JavaBasePlugin -import org.gradle.api.plugins.JavaPluginConvention -import org.gradle.api.tasks.* -import org.gradle.api.tasks.compile.AbstractCompile -import org.jetbrains.dokka.DokkaBootstrap -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.ReflectDsl -import org.jetbrains.dokka.ReflectDsl.isNotInstance -import java.io.File -import java.io.Serializable -import java.net.URLClassLoader -import java.util.concurrent.Callable -import java.util.function.BiConsumer - -open class DokkaTask : DefaultTask() { - - fun defaultKotlinTasks() = with(ReflectDsl) { - val abstractKotlinCompileClz = try { - project.buildscript.classLoader.loadClass(ABSTRACT_KOTLIN_COMPILE) - } catch (cnfe: ClassNotFoundException) { - logger.warn("$ABSTRACT_KOTLIN_COMPILE class not found, default kotlin tasks ignored") - return@with emptyList() - } - - return@with project.tasks.filter { it isInstance abstractKotlinCompileClz }.filter { "Test" !in it.name } - } - - init { - group = JavaBasePlugin.DOCUMENTATION_GROUP - description = "Generates dokka documentation for Kotlin" - - @Suppress("LeakingThis") - dependsOn(Callable { kotlinTasks.map { it.taskDependencies } }) - } - - @Input - var moduleName: String = "" - @Input - var outputFormat: String = "html" - @OutputDirectory - var outputDirectory: String = "" - var dokkaRuntime: Configuration? = null - - @InputFiles - var classpath: Iterable = arrayListOf() - - @Input - var sourceDirs: Iterable = emptyList() - @Input - var sourceRoots: MutableList = arrayListOf() - @Input - var dokkaFatJar: Any = "org.jetbrains.dokka:dokka-fatjar:${DokkaVersion.version}" - @Input - var impliedPlatforms: MutableList = arrayListOf() - @Optional - @Input - var cacheRoot: String? = null - @Input - var collectInheritedExtensionsFromLibraries: Boolean = false - @get:Internal - internal val kotlinCompileBasedClasspathAndSourceRoots: ClasspathAndSourceRoots by lazy { extractClasspathAndSourceRootsFromKotlinTasks() } - - protected var externalDocumentationLinks: MutableList = mutableListOf() - - private var kotlinTasksConfigurator: () -> List? = { defaultKotlinTasks() } - private val kotlinTasks: List by lazy { extractKotlinCompileTasks() } - - fun kotlinTasks(taskSupplier: Callable>) { - kotlinTasksConfigurator = { taskSupplier.call() } - } - - fun kotlinTasks(closure: Closure) { - kotlinTasksConfigurator = { closure.call() as? List } - } - - - fun tryResolveFatJar(project: Project): Set { - return try { - dokkaRuntime!!.resolve() - } catch (e: Exception) { - project.parent?.let { tryResolveFatJar(it) } ?: throw e - } - } - - fun loadFatJar() { - if (ClassloaderContainer.fatJarClassLoader == null) { - val jars = if (dokkaFatJar is File) - setOf(dokkaFatJar as File) - else - tryResolveFatJar(project) - ClassloaderContainer.fatJarClassLoader = URLClassLoader(jars.map { it.toURI().toURL() }.toTypedArray(), ClassLoader.getSystemClassLoader().parent) - } - } - - internal data class ClasspathAndSourceRoots(val classpathFileCollection: FileCollection, val sourceRoots: List) : - Serializable - - private fun extractKotlinCompileTasks(): List { - val inputList = (kotlinTasksConfigurator.invoke() ?: emptyList()).filterNotNull() - val (paths, other) = inputList.partition { it is String } - - val taskContainer = project.tasks - - val tasksByPath = paths.map { taskContainer.findByPath(it as String) ?: throw IllegalArgumentException("Task with path '$it' not found") } - - other - .filter { it !is Task || it isNotInstance getAbstractKotlinCompileFor(it) } - .forEach { throw IllegalArgumentException("Illegal entry in kotlinTasks, must be subtype of $ABSTRACT_KOTLIN_COMPILE or String, but was $it") } - - tasksByPath - .filter { it == null || it isNotInstance getAbstractKotlinCompileFor(it) } - .forEach { throw IllegalArgumentException("Illegal task path in kotlinTasks, must be subtype of $ABSTRACT_KOTLIN_COMPILE, but was $it") } - - - return (tasksByPath + other) as List - } - - private fun extractClasspathAndSourceRootsFromKotlinTasks(): ClasspathAndSourceRoots { - - val allTasks = kotlinTasks - - val allClasspath = mutableSetOf() - var allClasspathFileCollection: FileCollection = project.files() - val allSourceRoots = mutableSetOf() - - allTasks.forEach { - logger.debug("Dokka found AbstractKotlinCompile task: $it") - with(ReflectDsl) { - val taskSourceRoots: List = it["sourceRootsContainer"]["sourceRoots"].v() - - val abstractKotlinCompileClz = getAbstractKotlinCompileFor(it)!! - - val taskClasspath: Iterable = - (it["getClasspath", AbstractCompile::class].takeIfIsFunc()?.invoke() - ?: it["compileClasspath", abstractKotlinCompileClz].takeIfIsProp()?.v() - ?: it["getClasspath", abstractKotlinCompileClz]()) - - if (taskClasspath is FileCollection) { - allClasspathFileCollection += taskClasspath - } else { - allClasspath += taskClasspath - } - allSourceRoots += taskSourceRoots.filter { it.exists() } - } - } - - return ClasspathAndSourceRoots(allClasspathFileCollection + project.files(allClasspath), allSourceRoots.toList()) - } - - private fun Iterable.toSourceRoots(): List = this.filter { it.exists() }.map { GradleSourceRootImpl().apply { path = it.path } } - - protected open fun collectSuppressedFiles(sourceRoots: List): List = emptyList() - - @TaskAction - fun generate() { - if (dokkaRuntime == null){ - dokkaRuntime = project.configurations.getByName("dokkaRuntime") - } - - dokkaRuntime?.defaultDependencies{ dependencies -> dependencies.add(project.dependencies.create(dokkaFatJar)) } - val kotlinColorsEnabledBefore = System.getProperty(COLORS_ENABLED_PROPERTY) ?: "false" - System.setProperty(COLORS_ENABLED_PROPERTY, "false") - try { - loadFatJar() - // TODO: implement extracting source roots from kotlin tasks - val (_, tasksSourceRoots) = kotlinCompileBasedClasspathAndSourceRoots - - val sourceRoots = collectSourceRoots(sourceDirs, sourceRoots) + tasksSourceRoots.toSourceRoots() - - val bootstrapClass = ClassloaderContainer.fatJarClassLoader!!.loadClass("org.jetbrains.dokka.DokkaBootstrapImpl") - val bootstrapInstance = bootstrapClass.constructors.first().newInstance() - val bootstrapProxy: DokkaBootstrap = - automagicTypedProxy(javaClass.classLoader, bootstrapInstance) - - val gson = GsonBuilder().setPrettyPrinting().create() - - val passConfigurationExtension: GradlePassConfigurationImpl? = this.extensions.findByName( - CONFIGURATION_EXTENSION_NAME) as GradlePassConfigurationImpl? - val passConfigurationsContainer by lazy { - (this.extensions.getByName(MULTIPLATFORM_EXTENSION_NAME) as Iterable).toList() - } - passConfigurationExtension?.sourceRoots?.addAll(sourceRoots) - - val passConfigurationList = - (passConfigurationExtension?.let {passConfigurationsContainer + it } ?: passConfigurationsContainer) - .map { defaultPassConfiguration(it) } - - val configuration = GradleDokkaConfigurationImpl() - configuration.outputDir = outputDirectory - configuration.format = outputFormat - configuration.generateIndexPages = true - configuration.cacheRoot = cacheRoot - configuration.impliedPlatforms = impliedPlatforms - configuration.passesConfigurations = passConfigurationList - - bootstrapProxy.configure( - BiConsumer { level, message -> - when (level) { - "info" -> logger.info(message) - "warn" -> logger.warn(message) - "error" -> logger.error(message) - } - }, - gson.toJson(configuration) - ) - - bootstrapProxy.generate() - - } finally { - System.setProperty(COLORS_ENABLED_PROPERTY, kotlinColorsEnabledBefore) - } - } - - private fun defaultPassConfiguration(passConfig: GradlePassConfigurationImpl): GradlePassConfigurationImpl{ - val (tasksClasspath, tasksSourceRoots) = kotlinCompileBasedClasspathAndSourceRoots - - val fullClasspath = tasksClasspath + classpath - passConfig.moduleName = moduleName - passConfig.classpath = fullClasspath.map { it.absolutePath } - passConfig.samples = passConfig.samples.map { project.file(it).absolutePath } - passConfig.includes = passConfig.includes.map { project.file(it).absolutePath } - passConfig.collectInheritedExtensionsFromLibraries = collectInheritedExtensionsFromLibraries - passConfig.suppressedFiles = collectSuppressedFiles(passConfig.sourceRoots) - passConfig.externalDocumentationLinks.addAll(externalDocumentationLinks) - - return passConfig - } - - private fun collectSourceRoots(sourceDirs: Iterable, sourceRoots: List): List { - val sourceDirs = when { - sourceDirs.any() -> { - logger.info("Dokka: Taking source directories provided by the user") - sourceDirs.toSet() - } - kotlinTasks.isEmpty() -> project.convention.findPlugin(JavaPluginConvention::class.java)?.let { javaPluginConvention -> - logger.info("Dokka: Taking source directories from default java plugin") - val sourceSets = javaPluginConvention.sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME) - sourceSets?.allSource?.srcDirs - } - else -> emptySet() - } - - return sourceRoots + (sourceDirs?.toSourceRoots() ?: emptyList()) - } - - companion object { - const val COLORS_ENABLED_PROPERTY = "kotlin.colors.enabled" - const val ABSTRACT_KOTLIN_COMPILE = "org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile" - - private fun getAbstractKotlinCompileFor(task: Task) = try { - task.project.buildscript.classLoader.loadClass(ABSTRACT_KOTLIN_COMPILE) - } catch (e: ClassNotFoundException) { - null - } - } -} diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt index defddd31..d04ed5f0 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt @@ -7,9 +7,10 @@ import java.io.InputStream import java.util.* /* -* Those are extension names, which are used in a build.gradle file as closure names: +* Extension names, which are used in a build.gradle file as closure names: * dokka { -* configuration { <- extension name +* configuration { // extension name +* * } * } * */ -- cgit