diff options
author | sebastian.sellmair <sebastian.sellmair@jetbrains.com> | 2020-07-09 11:04:34 +0200 |
---|---|---|
committer | Sebastian Sellmair <34319766+sellmair@users.noreply.github.com> | 2020-07-13 14:23:08 +0200 |
commit | bb942786909961443eb1ff7feee90c190040ed49 (patch) | |
tree | eb78a14b01645d614497475fba7ec33710fbffc6 /runners/gradle-plugin/src/main/kotlin | |
parent | 824e5240d2f7b1bda775be22e12a95c0f8f2b0eb (diff) | |
download | dokka-bb942786909961443eb1ff7feee90c190040ed49.tar.gz dokka-bb942786909961443eb1ff7feee90c190040ed49.tar.bz2 dokka-bb942786909961443eb1ff7feee90c190040ed49.zip |
Refactor dokka tasks classpath configuration
Diffstat (limited to 'runners/gradle-plugin/src/main/kotlin')
4 files changed, 94 insertions, 166 deletions
diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaBootstrapFactory.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaBootstrapFactory.kt new file mode 100644 index 00000000..df29c19b --- /dev/null +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/DokkaBootstrapFactory.kt @@ -0,0 +1,18 @@ +package org.jetbrains.dokka.gradle + +import org.gradle.api.artifacts.Configuration +import org.jetbrains.dokka.DokkaBootstrap +import java.net.URLClassLoader + + +fun DokkaBootstrap(configuration: Configuration, bootstrapClassFQName: String): DokkaBootstrap { + val runtimeJars = configuration.resolve() + val runtimeClassLoader = URLClassLoader( + runtimeJars.map { it.toURI().toURL() }.toTypedArray(), + ClassLoader.getSystemClassLoader().parent + ) + + val bootstrapClass = runtimeClassLoader.loadClass(bootstrapClassFQName) + val bootstrapInstance = bootstrapClass.constructors.first().newInstance() + return automagicTypedProxy(DokkaPlugin::class.java.classLoader, bootstrapInstance) +} 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 803c91c0..d04ad258 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 @@ -3,11 +3,13 @@ package org.jetbrains.dokka.gradle import com.google.gson.GsonBuilder import org.gradle.api.DefaultTask import org.gradle.api.artifacts.Configuration +import org.gradle.api.attributes.Usage import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction import org.jetbrains.dokka.DokkaBootstrap +import org.jetbrains.dokka.DokkaVersion import org.jetbrains.dokka.plugability.Configurable import java.net.URLClassLoader import java.util.function.BiConsumer @@ -23,13 +25,18 @@ open class DokkaMultimoduleTask : DefaultTask(), Configurable { @Input var outputDirectory: String = "" - @get:Classpath - lateinit var pluginsConfig: Configuration - internal set + @Classpath + val runtime = project.configurations.create("${name}Runtime").apply { + defaultDependencies { dependencies -> + dependencies.add(project.dependencies.create("org.jetbrains.dokka:dokka-core:${DokkaVersion.version}")) + } + } - @get:Classpath - lateinit var dokkaRuntime: Configuration - internal set + @Classpath + val plugins = project.configurations.create("${name}Plugin").apply { + attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime")) + isCanBeConsumed = false + } @Input override val pluginsConfiguration: Map<String, String> = mutableMapOf() @@ -40,30 +47,19 @@ open class DokkaMultimoduleTask : DefaultTask(), Configurable { System.setProperty(DokkaTask.COLORS_ENABLED_PROPERTY, "false") try { - loadCore() - val bootstrapClass = - ClassloaderContainer.coreClassLoader!!.loadClass("org.jetbrains.dokka.DokkaMultimoduleBootstrapImpl") - val bootstrapInstance = bootstrapClass.constructors.first().newInstance() - val bootstrapProxy: DokkaBootstrap = automagicTypedProxy( - javaClass.classLoader, - bootstrapInstance - ) + val bootstrap = DokkaBootstrap(runtime, "org.jetbrains.dokka.DokkaMultimoduleBootstrapImpl") val gson = GsonBuilder().setPrettyPrinting().create() val configuration = getConfiguration() - bootstrapProxy.configure( - BiConsumer { level, message -> - when (level) { - "debug" -> logger.debug(message) - "info" -> logger.info(message) - "progress" -> logger.lifecycle(message) - "warn" -> logger.warn(message) - "error" -> logger.error(message) - } - }, - gson.toJson(configuration) - ) - - bootstrapProxy.generate() + bootstrap.configure(gson.toJson(configuration)) { level, message -> + when (level) { + "debug" -> logger.debug(message) + "info" -> logger.info(message) + "progress" -> logger.lifecycle(message) + "warn" -> logger.warn(message) + "error" -> logger.error(message) + } + } + bootstrap.generate() } finally { System.setProperty(DokkaTask.COLORS_ENABLED_PROPERTY, kotlinColorsEnabledBefore) } @@ -74,30 +70,18 @@ open class DokkaMultimoduleTask : DefaultTask(), Configurable { GradleDokkaConfigurationImpl().apply { outputDir = project.file(outputDirectory).absolutePath format = outputFormat - pluginsClasspath = pluginsConfig.resolve().toList() + pluginsClasspath = plugins.resolve().toList() pluginsConfiguration = this@DokkaMultimoduleTask.pluginsConfiguration modules = project.subprojects .mapNotNull { subproject -> subproject.tasks.withType(DokkaTask::class.java).firstOrNull()?.let { dokkaTask -> GradleDokkaModuleDescription().apply { name = subproject.name - path = - subproject.projectDir.resolve(dokkaTask.outputDirectory).toRelativeString( - project.file(outputDirectory) - ) + path = subproject.projectDir.resolve(dokkaTask.outputDirectory) + .toRelativeString(project.file(outputDirectory)) docFile = subproject.projectDir.resolve(documentationFileName).absolutePath } } } } - - private fun loadCore() { - if (ClassloaderContainer.coreClassLoader == null) { - val jars = dokkaRuntime!!.resolve() - ClassloaderContainer.coreClassLoader = URLClassLoader( - jars.map { it.toURI().toURL() }.toTypedArray(), - ClassLoader.getSystemClassLoader().parent - ) - } - } } 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 d4ade2d9..9bcbe9e0 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 @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import com.google.gson.GsonBuilder import org.gradle.api.* import org.gradle.api.artifacts.Configuration +import org.gradle.api.attributes.Usage import org.gradle.api.file.FileCollection import org.gradle.api.internal.plugins.DslObject import org.gradle.api.plugins.JavaBasePlugin @@ -50,9 +51,6 @@ open class DokkaTask : DefaultTask(), Configurable { @Input var outputDirectory: String = "" - @get:Classpath - lateinit var dokkaRuntime: Configuration - internal set @Input var subProjects: List<String> = emptyList() @@ -64,17 +62,27 @@ open class DokkaTask : DefaultTask(), Configurable { @Input var cacheRoot: String? = null - @get:Classpath - lateinit var pluginsClasspathConfiguration: Configuration - internal set + @Classpath + val runtime = project.configurations.create("${name}Runtime").apply { + defaultDependencies { dependencies -> + dependencies.add(project.dependencies.create("org.jetbrains.dokka:dokka-core:${DokkaVersion.version}")) + } + } + + @Classpath + val plugins: Configuration = project.configurations.create("${name}Plugin").apply { + attributes.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime")) + isCanBeConsumed = false + } @get:Internal internal var config: GradleDokkaConfigurationImpl? = null - var dokkaSourceSets: NamedDomainObjectContainer<GradleDokkaSourceSet> - @Suppress("UNCHECKED_CAST") - @Nested get() = (DslObject(this).extensions.getByName(SOURCE_SETS_EXTENSION_NAME) as NamedDomainObjectContainer<GradleDokkaSourceSet>) - internal set(value) = DslObject(this).extensions.add(SOURCE_SETS_EXTENSION_NAME, value) + @get:Nested + val dokkaSourceSets: NamedDomainObjectContainer<GradleDokkaSourceSet> = + project.container(GradleDokkaSourceSet::class.java) { name -> GradleDokkaSourceSet(name, project) } + .also { container -> DslObject(this).extensions.add("dokkaSourceSets", container) } + private val kotlinTasks: List<Task> by lazy { extractKotlinCompileTasks( @@ -96,16 +104,6 @@ open class DokkaTask : DefaultTask(), Configurable { private var outputDiagnosticInfo: Boolean = false // Workaround for Gradle, which fires some methods (like collectConfigurations()) multiple times in its lifecycle - private fun loadCore() { - if (ClassloaderContainer.coreClassLoader == null) { - val jars = dokkaRuntime.resolve() - ClassloaderContainer.coreClassLoader = URLClassLoader( - jars.map { it.toURI().toURL() }.toTypedArray(), - ClassLoader.getSystemClassLoader().parent - ) - } - } - protected fun extractKotlinCompileTasks(collectTasks: List<Any?>?): List<Task> { val inputList = (collectTasks ?: emptyList()).filterNotNull() val (paths, other) = inputList.partition { it is String } @@ -152,29 +150,20 @@ open class DokkaTask : DefaultTask(), Configurable { val kotlinColorsEnabledBefore = System.getProperty(COLORS_ENABLED_PROPERTY) ?: "false" System.setProperty(COLORS_ENABLED_PROPERTY, "false") try { - loadCore() - - val bootstrapClass = - ClassloaderContainer.coreClassLoader!!.loadClass("org.jetbrains.dokka.DokkaBootstrapImpl") - val bootstrapInstance = bootstrapClass.constructors.first().newInstance() - val bootstrapProxy: DokkaBootstrap = - automagicTypedProxy(javaClass.classLoader, bootstrapInstance) - - bootstrapProxy.configure( - BiConsumer { level, message -> - when (level) { - "debug" -> logger.debug(message) - "info" -> logger.info(message) - "progress" -> logger.lifecycle(message) - "warn" -> logger.warn(message) - "error" -> logger.error(message) - } - }, - GsonBuilder().setPrettyPrinting().create().toJson(configuration) - ) - - bootstrapProxy.generate() + val bootstrap = DokkaBootstrap(runtime, "org.jetbrains.dokka.DokkaBootstrapImpl") + bootstrap.configure( + GsonBuilder().setPrettyPrinting().create().toJson(configuration) + ) { level, message -> + when (level) { + "debug" -> logger.debug(message) + "info" -> logger.info(message) + "progress" -> logger.lifecycle(message) + "warn" -> logger.warn(message) + "error" -> logger.error(message) + } + } + bootstrap.generate() } finally { System.setProperty(COLORS_ENABLED_PROPERTY, kotlinColorsEnabledBefore) } @@ -199,7 +188,7 @@ open class DokkaTask : DefaultTask(), Configurable { cacheRoot = this@DokkaTask.cacheRoot offlineMode = this@DokkaTask.offlineMode sourceSets = defaultModulesConfiguration - pluginsClasspath = pluginsClasspathConfiguration.resolve().toList() + pluginsClasspath = plugins.resolve().toList() pluginsConfiguration = this@DokkaTask.pluginsConfiguration failOnWarning = this@DokkaTask.failOnWarning } @@ -351,7 +340,8 @@ open class DokkaTask : DefaultTask(), Configurable { @Classpath fun getInputClasspath(): FileCollection = - project.files((configuredDokkaSourceSets.flatMap { it.classpath } as List<Any>).map { project.fileTree(File(it.toString())) }) + project.files((configuredDokkaSourceSets.flatMap { it.classpath } as List<Any>) + .map { project.fileTree(File(it.toString())) }) companion object { const val COLORS_ENABLED_PROPERTY = "kotlin.colors.enabled" 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 a92f5475..60750373 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 @@ -2,102 +2,38 @@ package org.jetbrains.dokka.gradle import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.Configuration -import org.gradle.api.attributes.Usage -import org.gradle.util.GradleVersion +import org.gradle.kotlin.dsl.register import org.jetbrains.dokka.DokkaVersion import java.io.File -internal const val SOURCE_SETS_EXTENSION_NAME = "dokkaSourceSets" internal const val DOKKA_TASK_NAME = "dokka" internal const val DOKKA_COLLECTOR_TASK_NAME = "dokkaCollector" internal const val DOKKA_MULTIMODULE_TASK_NAME = "dokkaMultimodule" open class DokkaPlugin : Plugin<Project> { override fun apply(project: Project) { - val dokkaRuntimeConfiguration = addConfiguration(project) - val pluginsConfiguration = project.configurations.create("dokkaPlugins").apply { - dependencies.add(project.dependencies.create("org.jetbrains.dokka:dokka-base:${DokkaVersion.version}")) - attributes.attribute( - Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime") - ) - isCanBeConsumed = false - } - addDokkaTasks(project, dokkaRuntimeConfiguration, pluginsConfiguration, DokkaTask::class.java) - addDokkaCollectorTasks(project, DokkaCollectorTask::class.java) - addDokkaMultimoduleTasks( - project.rootProject, - dokkaRuntimeConfiguration, - pluginsConfiguration, - DokkaMultimoduleTask::class.java - ) + addDokkaTasks(project) + addDokkaCollectorTasks(project) + addDokkaMultimoduleTasks(project.rootProject) } - - private fun addConfiguration(project: Project) = - project.configurations.create("dokkaRuntime").apply { - defaultDependencies { dependencies -> - dependencies.add(project.dependencies.create("org.jetbrains.dokka:dokka-core:${DokkaVersion.version}")) - } - } - - private fun addDokkaTasks( - project: Project, - runtimeConfiguration: Configuration, - pluginsConfiguration: Configuration, - taskClass: Class<out DokkaTask> - ) { - if (GradleVersion.current() >= GradleVersion.version("4.10")) { - project.tasks.register(DOKKA_TASK_NAME, taskClass) - } else { - project.tasks.create(DOKKA_TASK_NAME, taskClass) - } - project.tasks.withType(taskClass) { task -> - task.dokkaSourceSets = project.container(GradleDokkaSourceSet::class.java) { name -> - GradleDokkaSourceSet(name, project) - } - task.dokkaRuntime = runtimeConfiguration - task.pluginsClasspathConfiguration = pluginsConfiguration - task.outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath + private fun addDokkaTasks(project: Project) { + project.tasks.register<DokkaTask>(DOKKA_TASK_NAME) { + val dokkaBase = project.dependencies.create("org.jetbrains.dokka:dokka-base:${DokkaVersion.version}") + plugins.dependencies.add(dokkaBase) + outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath } } - private fun addDokkaCollectorTasks( - project: Project, - taskClass: Class<out DokkaCollectorTask> - ) { - if (GradleVersion.current() >= GradleVersion.version("4.10")) { - project.tasks.register(DOKKA_COLLECTOR_TASK_NAME, taskClass) - } else { - project.tasks.create(DOKKA_COLLECTOR_TASK_NAME, taskClass) - } - project.tasks.withType(taskClass) { task -> - task.outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath + private fun addDokkaCollectorTasks(project: Project) { + project.tasks.register<DokkaCollectorTask>(DOKKA_COLLECTOR_TASK_NAME) { + outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath } } - private fun addDokkaMultimoduleTasks( - project: Project, - runtimeConfiguration: Configuration, - pluginsConfiguration: Configuration, - taskClass: Class<out DokkaMultimoduleTask> - ) { - if (project.tasks.find { it.name == DOKKA_MULTIMODULE_TASK_NAME } == null) { - if (GradleVersion.current() >= GradleVersion.version("4.10")) { - project.tasks.register(DOKKA_MULTIMODULE_TASK_NAME, taskClass) - } else { - project.tasks.create(DOKKA_MULTIMODULE_TASK_NAME, taskClass) - } - project.tasks.withType(taskClass) { task -> - task.dokkaRuntime = runtimeConfiguration - task.pluginsConfig = pluginsConfiguration - task.outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath - } + private fun addDokkaMultimoduleTasks(project: Project) { + project.tasks.register<DokkaMultimoduleTask>(DOKKA_MULTIMODULE_TASK_NAME) { + outputDirectory = File(project.buildDir, DOKKA_TASK_NAME).absolutePath } } } - -internal object ClassloaderContainer { - @JvmField - var coreClassLoader: ClassLoader? = null -} |