Skip to content

Commit 8df567d

Browse files
committed
Add flag to disable dumping Dokka config file
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
1 parent fc574c8 commit 8df567d

File tree

7 files changed

+189
-6
lines changed

7 files changed

+189
-6
lines changed

dokka-runners/dokka-gradle-plugin/src/main/kotlin/DokkaBasePlugin.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.jetbrains.dokka.gradle.engine.parameters.DokkaSourceSetSpec
2828
import org.jetbrains.dokka.gradle.engine.parameters.KotlinPlatform
2929
import org.jetbrains.dokka.gradle.engine.parameters.VisibilityModifier
3030
import org.jetbrains.dokka.gradle.internal.*
31+
import org.jetbrains.dokka.gradle.internal.PluginFeaturesService.Companion.pluginFeaturesService
3132
import org.jetbrains.dokka.gradle.tasks.*
3233
import java.io.File
3334
import javax.inject.Inject
@@ -69,7 +70,7 @@ constructor(
6970

7071
configureDokkaPublicationsDefaults(dokkaExtension)
7172

72-
initDokkaTasks(target, dokkaExtension)
73+
initDokkaTasks(target, dokkaExtension, target.pluginFeaturesService)
7374
}
7475

7576
private fun createExtension(project: Project): DokkaExtension {
@@ -257,6 +258,7 @@ constructor(
257258
private fun initDokkaTasks(
258259
target: Project,
259260
dokkaExtension: DokkaExtension,
261+
pluginFeaturesService: PluginFeaturesService,
260262
) {
261263
target.tasks.register<DokkaBaseTask>(taskNames.generate) {
262264
description = "Generates Dokka publications for all formats"
@@ -266,6 +268,7 @@ constructor(
266268
target.tasks.withType<DokkaGenerateTask>().configureEach {
267269
cacheDirectory.convention(dokkaExtension.dokkaCacheDirectory)
268270
workerLogFile.convention(temporaryDir.resolve("dokka-worker.log"))
271+
dumpDokkaConfigurationDebugFile.convention(pluginFeaturesService.dumpDokkaConfigurationDebugFile)
269272
dokkaConfigurationJsonFile.convention(temporaryDir.resolve("dokka-configuration.json"))
270273
workerIsolation.convention(dokkaExtension.dokkaGeneratorIsolation)
271274
publicationEnabled.convention(true)

dokka-runners/dokka-gradle-plugin/src/main/kotlin/internal/PluginFeaturesService.kt

+23-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ constructor(
5050
/** If `true`, suppress [k2AnalysisEnabled] messages. */
5151
val k2AnalysisNoWarn: Property<Boolean>
5252

53+
/** @see PluginFeaturesService.dumpDokkaConfigurationDebugFile */
54+
val dumpDokkaConfigurationDebugFile: Property<Boolean>
55+
5356
/** [Project.getDisplayName] - only used for log messages. */
5457
val projectDisplayName: Property<String>
5558

@@ -231,6 +234,19 @@ constructor(
231234
}
232235
}
233236

237+
/**
238+
* Enable saving the [org.jetbrains.dokka.DokkaConfiguration] used to run
239+
* [org.jetbrains.dokka.DokkaGenerator] to a file.
240+
*
241+
* The configuration file is only useful for debugging.
242+
* (For example, checking the generated configuration on CI, which might use Linux/Windows/macOS).
243+
*
244+
* @see org.jetbrains.dokka.gradle.tasks.DokkaGenerateTask.dokkaConfigurationJsonFile
245+
*/
246+
internal val dumpDokkaConfigurationDebugFile: Boolean by lazy {
247+
parameters.dumpDokkaConfigurationDebugFile.getOrElse(false)
248+
}
249+
234250
/** Values for [pluginMode]. */
235251
private enum class PluginMode {
236252
V1Enabled,
@@ -267,7 +283,7 @@ constructor(
267283
* ORG_GRADLE_PROJECT_org.jetbrains.dokka.gradle.enabledLogHtmlPublicationLink=false
268284
* ```
269285
*/
270-
val enableLogHtmlPublicationLink: Provider<Boolean> =
286+
val enableLogHtmlPublicationLink: Provider<Boolean> =
271287
providers.gradleProperty("org.jetbrains.dokka.gradle.enableLogHtmlPublicationLink")
272288
.toBoolean()
273289
.orElse(true)
@@ -296,6 +312,10 @@ constructor(
296312
private const val K2_ANALYSIS_NO_WARN_FLAG_PRETTY =
297313
"$K2_ANALYSIS_ENABLED_FLAG.noWarn"
298314

315+
/** @see PluginFeaturesService.dumpDokkaConfigurationDebugFile */
316+
private const val DUMP_DOKKA_CONFIG_DEBUG_FILE =
317+
"org.jetbrains.dokka.internal.dumpDokkaConfigurationDebugFile"
318+
299319
@Suppress("ObjectPrivatePropertyName")
300320
private val `To learn about migrating read the migration guide` = /* language=text */ """
301321
|To learn about migrating read the migration guide https://kotl.in/dokka-gradle-migration
@@ -381,6 +401,8 @@ constructor(
381401
.toBoolean()
382402
)
383403

404+
dumpDokkaConfigurationDebugFile.set(getFlag(DUMP_DOKKA_CONFIG_DEBUG_FILE).toBoolean())
405+
384406
configureParamsDuringAccessorsGeneration(project)
385407
}
386408
}

dokka-runners/dokka-gradle-plugin/src/main/kotlin/tasks/DokkaGenerateTask.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,17 @@ constructor(
9797
abstract val workerLogFile: RegularFileProperty
9898

9999
/**
100-
* The [DokkaConfiguration] by Dokka Generator can be saved to a file for debugging purposes.
101-
* To disable this behaviour set this property to `null`.
100+
* Enable saving the [DokkaConfiguration] used to config Dokka Generator to a file, for debugging purposes.
101+
*
102+
* @see dokkaConfigurationJsonFile
103+
*/
104+
@InternalDokkaGradlePluginApi
105+
@get:Optional
106+
@get:Input
107+
abstract val dumpDokkaConfigurationDebugFile: Property<Boolean>
108+
109+
/**
110+
* @see dumpDokkaConfigurationDebugFile
102111
*/
103112
@InternalDokkaGradlePluginApi
104113
@get:Optional
@@ -205,6 +214,9 @@ constructor(
205214
private fun dumpDokkaConfigurationJson(
206215
dokkaConfiguration: DokkaConfiguration,
207216
) {
217+
if (dumpDokkaConfigurationDebugFile.orNull != true) {
218+
return
219+
}
208220
val destFile = dokkaConfigurationJsonFile.asFile.orNull ?: return
209221
destFile.parentFile.mkdirs()
210222
destFile.createNewFile()

dokka-runners/dokka-gradle-plugin/src/testFixtures/kotlin/GradlePropertiesBuilder.kt

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ data class GradlePropertiesBuilder(
7373
var k2Analysis: Boolean? = null,
7474
var k2AnalysisNoWarn: Boolean? = null,
7575
var enableLogHtmlPublicationLink: Boolean? = false,
76+
/** @see org.jetbrains.dokka.gradle.internal.PluginFeaturesService.dumpDokkaConfigurationDebugFile */
77+
var dumpDokkaConfigurationDebugFile: Boolean? = true,
7678
)
7779

7880
/** Gradle Daemon JVM args. */
@@ -107,6 +109,7 @@ data class GradlePropertiesBuilder(
107109
putNotNull("org.jetbrains.dokka.experimental.tryK2", k2Analysis)
108110
putNotNull("org.jetbrains.dokka.experimental.tryK2.noWarn", k2AnalysisNoWarn)
109111
putNotNull("org.jetbrains.dokka.gradle.enableLogHtmlPublicationLink", enableLogHtmlPublicationLink)
112+
putNotNull("org.jetbrains.dokka.internal.dumpDokkaConfigurationDebugFile", dumpDokkaConfigurationDebugFile)
110113
}
111114

112115
with(kotlin) {

dokka-runners/dokka-gradle-plugin/src/testFixtures/kotlin/GradleTestKitUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fun gradleKtsProjectTest(
159159

160160
return gradleProjectTest(
161161
testProjectName = rootProjectNameValue,
162-
baseDir = baseDir,
162+
baseDir = baseDir.resolve(projectLocation),
163163
) {
164164

165165
settingsGradleKts = """

dokka-runners/dokka-gradle-plugin/src/testFixtures/kotlin/stringUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fun String.sortLines(separator: String = "\n") =
2525

2626

2727
/** Replace characters that don't match [isLetterOrDigit] with [replacement]. */
28-
internal fun String.replaceNonAlphaNumeric(
28+
fun String.replaceNonAlphaNumeric(
2929
replacement: String = "-"
3030
): String =
3131
asIterable().joinToString("") { c ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
package org.jetbrains.dokka.gradle
5+
6+
import io.kotest.core.spec.style.FunSpec
7+
import io.kotest.core.test.TestScope
8+
import io.kotest.matchers.collections.shouldBeEmpty
9+
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
10+
import io.kotest.matchers.file.shouldExist
11+
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
12+
import org.jetbrains.dokka.gradle.internal.DokkaConstants.DOKKA_VERSION
13+
import org.jetbrains.dokka.gradle.utils.*
14+
import kotlin.io.path.invariantSeparatorsPathString
15+
import kotlin.io.path.name
16+
import kotlin.io.path.relativeToOrSelf
17+
18+
class DumpDokkaConfigurationTest : FunSpec({
19+
20+
context("enabling dumpDokkaConfigurationDebugFile should dump configuration to file") {
21+
22+
val project = initProject()
23+
24+
project.gradleProperties {
25+
dokka {
26+
dumpDokkaConfigurationDebugFile = true
27+
}
28+
}
29+
30+
project.runner
31+
.addArguments(
32+
":dokkaGenerateModuleHtml",
33+
":dokkaGeneratePublicationHtml",
34+
"--rerun-tasks",
35+
)
36+
.forwardOutput()
37+
.build {
38+
test("expect dokka tasks run successfully") {
39+
shouldHaveTasksWithOutcome(
40+
":dokkaGenerateModuleHtml" to SUCCESS,
41+
":dokkaGeneratePublicationHtml" to SUCCESS,
42+
)
43+
}
44+
45+
test("expect dokka tasks generate config files") {
46+
project.dir("build/tmp")
47+
.findFiles { it.name == "dokka-configuration.json" }
48+
.map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString }
49+
.toList()
50+
.shouldContainExactlyInAnyOrder(
51+
"build/tmp/dokkaGeneratePublicationHtml/dokka-configuration.json",
52+
"build/tmp/dokkaGenerateModuleHtml/dokka-configuration.json"
53+
)
54+
}
55+
}
56+
}
57+
58+
context("implicit task outputs should not contain Dokka config file") {
59+
listOf(
60+
"dumpDokkaConfigurationDebugFile is not set" to null,
61+
"dumpDokkaConfigurationDebugFile is disabled" to false,
62+
).forEach { (testName, value) ->
63+
context("when $testName ") {
64+
val project = initProject()
65+
66+
project.gradleProperties {
67+
dokka {
68+
dumpDokkaConfigurationDebugFile = value
69+
}
70+
}
71+
72+
project.runner
73+
.forwardOutput()
74+
.addArguments(
75+
":syncDokkaOutput",
76+
"--rerun",
77+
)
78+
.build {
79+
test("expect syncDokkaOutput runs successfully") {
80+
shouldHaveTasksWithOutcome(
81+
":syncDokkaOutput" to SUCCESS,
82+
)
83+
}
84+
85+
test("verify sync task collects Dokka output files") {
86+
// Just a simple check to make sure that some files were copied, because testing the config
87+
// files don't exist could mean that nothing was copied and something else broke.
88+
project.file("build/tmp/syncDokkaOutput/module/module-descriptor.json")
89+
.toFile()
90+
.shouldExist()
91+
project.file("build/tmp/syncDokkaOutput/publication/index.html")
92+
.toFile()
93+
.shouldExist()
94+
}
95+
96+
test("expect no Dokka config file in output") {
97+
project
98+
.dir("build/tmp/syncDokkaOutput")
99+
.findFiles { it.name == "dokka-configuration.json" }
100+
.map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString }
101+
.toList()
102+
.shouldBeEmpty()
103+
}
104+
}
105+
}
106+
}
107+
}
108+
})
109+
110+
private fun TestScope.initProject(
111+
config: GradleProjectTest.() -> Unit = {},
112+
): GradleProjectTest {
113+
val baseDirName = testCase.descriptor.ids()
114+
.joinToString("/") { it.value.substringAfter("org.jetbrains.dokka.gradle.").replaceNonAlphaNumeric() }
115+
116+
return gradleKtsProjectTest(
117+
projectLocation = baseDirName,
118+
rootProjectName = "DumpDokkaConfigurationTest",
119+
) {
120+
buildGradleKts = """
121+
|plugins {
122+
| kotlin("jvm") version embeddedKotlinVersion
123+
| id("org.jetbrains.dokka") version "$DOKKA_VERSION"
124+
|}
125+
|
126+
|val syncDokkaOutput by tasks.registering(Sync::class) {
127+
| // fetch the implicit output files of the DokkaGenerate tasks
128+
| from(tasks.dokkaGeneratePublicationHtml) {
129+
| into("publication")
130+
| }
131+
| from(tasks.dokkaGenerateModuleHtml) {
132+
| into("module")
133+
| }
134+
| into(temporaryDir)
135+
|}
136+
|
137+
""".trimMargin()
138+
139+
createKotlinFile("src/main/kotlin/Foo.kt", "class Foo")
140+
141+
config()
142+
}
143+
}

0 commit comments

Comments
 (0)