Skip to content

Commit

Permalink
Add flag to disable dumping Dokka config file
Browse files Browse the repository at this point in the history
Dumping the Dokka config files is useful for debugging, but can interfere with task outputs.

Here we add a flag that disables the dumping the config file by default, but we enable it for our own tests.

Fix #3958
  • Loading branch information
adam-enko committed Dec 18, 2024
1 parent fc574c8 commit 8df567d
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.jetbrains.dokka.gradle.engine.parameters.DokkaSourceSetSpec
import org.jetbrains.dokka.gradle.engine.parameters.KotlinPlatform
import org.jetbrains.dokka.gradle.engine.parameters.VisibilityModifier
import org.jetbrains.dokka.gradle.internal.*
import org.jetbrains.dokka.gradle.internal.PluginFeaturesService.Companion.pluginFeaturesService
import org.jetbrains.dokka.gradle.tasks.*
import java.io.File
import javax.inject.Inject
Expand Down Expand Up @@ -69,7 +70,7 @@ constructor(

configureDokkaPublicationsDefaults(dokkaExtension)

initDokkaTasks(target, dokkaExtension)
initDokkaTasks(target, dokkaExtension, target.pluginFeaturesService)
}

private fun createExtension(project: Project): DokkaExtension {
Expand Down Expand Up @@ -257,6 +258,7 @@ constructor(
private fun initDokkaTasks(
target: Project,
dokkaExtension: DokkaExtension,
pluginFeaturesService: PluginFeaturesService,
) {
target.tasks.register<DokkaBaseTask>(taskNames.generate) {
description = "Generates Dokka publications for all formats"
Expand All @@ -266,6 +268,7 @@ constructor(
target.tasks.withType<DokkaGenerateTask>().configureEach {
cacheDirectory.convention(dokkaExtension.dokkaCacheDirectory)
workerLogFile.convention(temporaryDir.resolve("dokka-worker.log"))
dumpDokkaConfigurationDebugFile.convention(pluginFeaturesService.dumpDokkaConfigurationDebugFile)
dokkaConfigurationJsonFile.convention(temporaryDir.resolve("dokka-configuration.json"))
workerIsolation.convention(dokkaExtension.dokkaGeneratorIsolation)
publicationEnabled.convention(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ constructor(
/** If `true`, suppress [k2AnalysisEnabled] messages. */
val k2AnalysisNoWarn: Property<Boolean>

/** @see PluginFeaturesService.dumpDokkaConfigurationDebugFile */
val dumpDokkaConfigurationDebugFile: Property<Boolean>

/** [Project.getDisplayName] - only used for log messages. */
val projectDisplayName: Property<String>

Expand Down Expand Up @@ -231,6 +234,19 @@ constructor(
}
}

/**
* Enable saving the [org.jetbrains.dokka.DokkaConfiguration] used to run
* [org.jetbrains.dokka.DokkaGenerator] to a file.
*
* The configuration file is only useful for debugging.
* (For example, checking the generated configuration on CI, which might use Linux/Windows/macOS).
*
* @see org.jetbrains.dokka.gradle.tasks.DokkaGenerateTask.dokkaConfigurationJsonFile
*/
internal val dumpDokkaConfigurationDebugFile: Boolean by lazy {
parameters.dumpDokkaConfigurationDebugFile.getOrElse(false)
}

/** Values for [pluginMode]. */
private enum class PluginMode {
V1Enabled,
Expand Down Expand Up @@ -267,7 +283,7 @@ constructor(
* ORG_GRADLE_PROJECT_org.jetbrains.dokka.gradle.enabledLogHtmlPublicationLink=false
* ```
*/
val enableLogHtmlPublicationLink: Provider<Boolean> =
val enableLogHtmlPublicationLink: Provider<Boolean> =
providers.gradleProperty("org.jetbrains.dokka.gradle.enableLogHtmlPublicationLink")
.toBoolean()
.orElse(true)
Expand Down Expand Up @@ -296,6 +312,10 @@ constructor(
private const val K2_ANALYSIS_NO_WARN_FLAG_PRETTY =
"$K2_ANALYSIS_ENABLED_FLAG.noWarn"

/** @see PluginFeaturesService.dumpDokkaConfigurationDebugFile */
private const val DUMP_DOKKA_CONFIG_DEBUG_FILE =
"org.jetbrains.dokka.internal.dumpDokkaConfigurationDebugFile"

@Suppress("ObjectPrivatePropertyName")
private val `To learn about migrating read the migration guide` = /* language=text */ """
|To learn about migrating read the migration guide https://kotl.in/dokka-gradle-migration
Expand Down Expand Up @@ -381,6 +401,8 @@ constructor(
.toBoolean()
)

dumpDokkaConfigurationDebugFile.set(getFlag(DUMP_DOKKA_CONFIG_DEBUG_FILE).toBoolean())

configureParamsDuringAccessorsGeneration(project)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,17 @@ constructor(
abstract val workerLogFile: RegularFileProperty

/**
* The [DokkaConfiguration] by Dokka Generator can be saved to a file for debugging purposes.
* To disable this behaviour set this property to `null`.
* Enable saving the [DokkaConfiguration] used to config Dokka Generator to a file, for debugging purposes.
*
* @see dokkaConfigurationJsonFile
*/
@InternalDokkaGradlePluginApi
@get:Optional
@get:Input
abstract val dumpDokkaConfigurationDebugFile: Property<Boolean>

/**
* @see dumpDokkaConfigurationDebugFile
*/
@InternalDokkaGradlePluginApi
@get:Optional
Expand Down Expand Up @@ -205,6 +214,9 @@ constructor(
private fun dumpDokkaConfigurationJson(
dokkaConfiguration: DokkaConfiguration,
) {
if (dumpDokkaConfigurationDebugFile.orNull != true) {
return
}
val destFile = dokkaConfigurationJsonFile.asFile.orNull ?: return
destFile.parentFile.mkdirs()
destFile.createNewFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ data class GradlePropertiesBuilder(
var k2Analysis: Boolean? = null,
var k2AnalysisNoWarn: Boolean? = null,
var enableLogHtmlPublicationLink: Boolean? = false,
/** @see org.jetbrains.dokka.gradle.internal.PluginFeaturesService.dumpDokkaConfigurationDebugFile */
var dumpDokkaConfigurationDebugFile: Boolean? = true,
)

/** Gradle Daemon JVM args. */
Expand Down Expand Up @@ -107,6 +109,7 @@ data class GradlePropertiesBuilder(
putNotNull("org.jetbrains.dokka.experimental.tryK2", k2Analysis)
putNotNull("org.jetbrains.dokka.experimental.tryK2.noWarn", k2AnalysisNoWarn)
putNotNull("org.jetbrains.dokka.gradle.enableLogHtmlPublicationLink", enableLogHtmlPublicationLink)
putNotNull("org.jetbrains.dokka.internal.dumpDokkaConfigurationDebugFile", dumpDokkaConfigurationDebugFile)
}

with(kotlin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fun gradleKtsProjectTest(

return gradleProjectTest(
testProjectName = rootProjectNameValue,
baseDir = baseDir,
baseDir = baseDir.resolve(projectLocation),
) {

settingsGradleKts = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun String.sortLines(separator: String = "\n") =


/** Replace characters that don't match [isLetterOrDigit] with [replacement]. */
internal fun String.replaceNonAlphaNumeric(
fun String.replaceNonAlphaNumeric(
replacement: String = "-"
): String =
asIterable().joinToString("") { c ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package org.jetbrains.dokka.gradle

import io.kotest.core.spec.style.FunSpec
import io.kotest.core.test.TestScope
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.file.shouldExist
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
import org.jetbrains.dokka.gradle.internal.DokkaConstants.DOKKA_VERSION
import org.jetbrains.dokka.gradle.utils.*
import kotlin.io.path.invariantSeparatorsPathString
import kotlin.io.path.name
import kotlin.io.path.relativeToOrSelf

class DumpDokkaConfigurationTest : FunSpec({

context("enabling dumpDokkaConfigurationDebugFile should dump configuration to file") {

val project = initProject()

project.gradleProperties {
dokka {
dumpDokkaConfigurationDebugFile = true
}
}

project.runner
.addArguments(
":dokkaGenerateModuleHtml",
":dokkaGeneratePublicationHtml",
"--rerun-tasks",
)
.forwardOutput()
.build {
test("expect dokka tasks run successfully") {
shouldHaveTasksWithOutcome(
":dokkaGenerateModuleHtml" to SUCCESS,
":dokkaGeneratePublicationHtml" to SUCCESS,
)
}

test("expect dokka tasks generate config files") {
project.dir("build/tmp")
.findFiles { it.name == "dokka-configuration.json" }
.map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString }
.toList()
.shouldContainExactlyInAnyOrder(
"build/tmp/dokkaGeneratePublicationHtml/dokka-configuration.json",
"build/tmp/dokkaGenerateModuleHtml/dokka-configuration.json"
)
}
}
}

context("implicit task outputs should not contain Dokka config file") {
listOf(
"dumpDokkaConfigurationDebugFile is not set" to null,
"dumpDokkaConfigurationDebugFile is disabled" to false,
).forEach { (testName, value) ->
context("when $testName ") {
val project = initProject()

project.gradleProperties {
dokka {
dumpDokkaConfigurationDebugFile = value
}
}

project.runner
.forwardOutput()
.addArguments(
":syncDokkaOutput",
"--rerun",
)
.build {
test("expect syncDokkaOutput runs successfully") {
shouldHaveTasksWithOutcome(
":syncDokkaOutput" to SUCCESS,
)
}

test("verify sync task collects Dokka output files") {
// Just a simple check to make sure that some files were copied, because testing the config
// files don't exist could mean that nothing was copied and something else broke.
project.file("build/tmp/syncDokkaOutput/module/module-descriptor.json")
.toFile()
.shouldExist()
project.file("build/tmp/syncDokkaOutput/publication/index.html")
.toFile()
.shouldExist()
}

test("expect no Dokka config file in output") {
project
.dir("build/tmp/syncDokkaOutput")
.findFiles { it.name == "dokka-configuration.json" }
.map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString }
.toList()
.shouldBeEmpty()
}
}
}
}
}
})

private fun TestScope.initProject(
config: GradleProjectTest.() -> Unit = {},
): GradleProjectTest {
val baseDirName = testCase.descriptor.ids()
.joinToString("/") { it.value.substringAfter("org.jetbrains.dokka.gradle.").replaceNonAlphaNumeric() }

return gradleKtsProjectTest(
projectLocation = baseDirName,
rootProjectName = "DumpDokkaConfigurationTest",
) {
buildGradleKts = """
|plugins {
| kotlin("jvm") version embeddedKotlinVersion
| id("org.jetbrains.dokka") version "$DOKKA_VERSION"
|}
|
|val syncDokkaOutput by tasks.registering(Sync::class) {
| // fetch the implicit output files of the DokkaGenerate tasks
| from(tasks.dokkaGeneratePublicationHtml) {
| into("publication")
| }
| from(tasks.dokkaGenerateModuleHtml) {
| into("module")
| }
| into(temporaryDir)
|}
|
""".trimMargin()

createKotlinFile("src/main/kotlin/Foo.kt", "class Foo")

config()
}
}

0 comments on commit 8df567d

Please sign in to comment.