Skip to content

Commit bf3b5ee

Browse files
whyolegadam-enko
andauthored
Fix suppressGeneratedFiles was unused (#4348)
Co-authored-by: Adam Semenenko <152864218+adam-enko@users.noreply.github.com>
1 parent 7ced00d commit bf3b5ee

4 files changed

Lines changed: 330 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ constructor(
208208
sourceSetScope.convention(sourceSetScopeConvention)
209209

210210
suppressGeneratedFiles.convention(true)
211+
suppressedFiles.from(
212+
suppressGeneratedFiles.map { suppressGenerated ->
213+
if (suppressGenerated) {
214+
layout.buildDirectory.dir("generated")
215+
} else {
216+
objects.fileCollection()
217+
}
218+
}
219+
)
211220

212221
sourceLinks.configureEach {
213222
localDirectory.convention(layout.projectDirectory)

dokka-runners/dokka-gradle-plugin/src/main/kotlin/engine/parameters/DokkaSourceSetSpec.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package org.jetbrains.dokka.gradle.engine.parameters
55

66
import org.gradle.api.*
77
import org.gradle.api.file.ConfigurableFileCollection
8+
import org.gradle.api.file.FileCollection
89
import org.gradle.api.model.ObjectFactory
910
import org.gradle.api.plugins.ExtensionAware
1011
import org.gradle.api.provider.Property
@@ -177,8 +178,7 @@ constructor(
177178
*
178179
* By default, source roots are deduced from information provided by the Kotlin Gradle plugin.
179180
*/
180-
@get:InputFiles
181-
@get:PathSensitive(PathSensitivity.RELATIVE)
181+
@get:Internal // tracked by `inputSourceFiles`
182182
abstract val sourceRoots: ConfigurableFileCollection
183183

184184
/**
@@ -263,12 +263,12 @@ constructor(
263263
* Directories or individual files that should be suppressed, meaning declarations from them
264264
* will be not documented.
265265
*
266-
* Will be concatenated with generated files if [suppressGeneratedFiles] is set to `false`.
266+
* Will be concatenated with generated files if [suppressGeneratedFiles] is set to `true`.
267267
*/
268-
@get:InputFiles
269-
@get:PathSensitive(PathSensitivity.RELATIVE)
268+
@get:Internal // tracked by `inputSourceFiles`
270269
abstract val suppressedFiles: ConfigurableFileCollection
271270

271+
272272
/**
273273
* Whether to document/analyze generated files.
274274
*
@@ -444,6 +444,17 @@ constructor(
444444
abstract val noJdkLink: Property<Boolean>
445445
//endregion
446446

447+
// this is just for task input tracking
448+
@get:InputFiles
449+
@get:IgnoreEmptyDirectories
450+
@get:PathSensitive(PathSensitivity.RELATIVE)
451+
internal val inputSourceFiles: FileCollection
452+
get() = sourceRoots.asFileTree.filter { sourceFile ->
453+
suppressedFiles.none { suppressedFile ->
454+
sourceFile.startsWith(suppressedFile)
455+
}
456+
}
457+
447458
companion object {
448459

449460
/**

dokka-runners/dokka-gradle-plugin/src/test/kotlin/engine/parameters/DokkaSourceSetSpecTest.kt

Lines changed: 174 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
package org.jetbrains.dokka.gradle.engine.parameters
55

66
import io.kotest.core.spec.style.FunSpec
7+
import io.kotest.core.test.TestScope
8+
import io.kotest.inspectors.shouldForAll
79
import io.kotest.matchers.collections.shouldBeEmpty
10+
import io.kotest.matchers.collections.shouldContainExactly
811
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
12+
import io.kotest.matchers.file.shouldBeAFile
913
import io.kotest.matchers.shouldBe
1014
import org.gradle.api.Project
1115
import org.gradle.kotlin.dsl.apply
@@ -16,6 +20,11 @@ import org.jetbrains.dokka.gradle.DokkaPlugin
1620
import org.jetbrains.dokka.gradle.engine.parameters.VisibilityModifier.Public
1721
import org.jetbrains.dokka.gradle.utils.create_
1822
import org.jetbrains.dokka.gradle.utils.enableV2Plugin
23+
import java.nio.file.Path
24+
import kotlin.io.path.Path
25+
import kotlin.io.path.createDirectories
26+
import kotlin.io.path.relativeTo
27+
import kotlin.io.path.writeText
1928

2029
class DokkaSourceSetSpecTest : FunSpec({
2130

@@ -39,7 +48,7 @@ class DokkaSourceSetSpecTest : FunSpec({
3948
dss.displayName.orNull shouldBe "foo"
4049
}
4150
test("documentedVisibilities") {
42-
dss.documentedVisibilities.orNull.shouldContainExactlyInAnyOrder(Public)
51+
dss.documentedVisibilities.orNull.shouldContainExactly(Public)
4352
}
4453
test("enableAndroidDocumentationLink") {
4554
dss.enableAndroidDocumentationLink.orNull shouldBe false
@@ -53,7 +62,7 @@ class DokkaSourceSetSpecTest : FunSpec({
5362
test("externalDocumentationLinks") {
5463
dss.externalDocumentationLinks
5564
.map { it.run { "$name enabled:${enabled.orNull} url:${url.orNull} packageList:${packageListUrl.orNull}" } }
56-
.shouldContainExactlyInAnyOrder(
65+
.shouldContainExactly(
5766
"androidSdk enabled:false url:https://developer.android.com/reference/kotlin/ packageList:https://developer.android.com/reference/kotlin/package-list",
5867
"androidX enabled:false url:https://developer.android.com/reference/kotlin/ packageList:https://developer.android.com/reference/kotlin/androidx/package-list",
5968
"jdk enabled:true url:https://docs.oracle.com/en/java/javase/11/docs/api/ packageList:https://docs.oracle.com/en/java/javase/11/docs/api/element-list",
@@ -107,7 +116,10 @@ class DokkaSourceSetSpecTest : FunSpec({
107116
dss.suppressGeneratedFiles.orNull shouldBe true
108117
}
109118
test("suppressedFiles") {
110-
dss.suppressedFiles.shouldBeEmpty()
119+
dss.suppressedFiles.toList() shouldBe listOf(project.file("build/generated"))
120+
}
121+
test("inputSourceFiles") {
122+
dss.inputSourceFiles.shouldBeEmpty() // because `sourceRoots` is empty
111123
}
112124
}
113125

@@ -141,6 +153,165 @@ class DokkaSourceSetSpecTest : FunSpec({
141153
}
142154
}
143155
}
156+
157+
context("DokkaSourceSetSpec inputSourceFiles ->") {
158+
159+
val project = createProject()
160+
161+
val projectDir = project.projectDir.toPath()
162+
fun Path.createClass(name: String) {
163+
createDirectories()
164+
resolve("$name.kt").writeText("class $name")
165+
}
166+
167+
val mainSourceDir =
168+
projectDir.resolve("src/main/kotlin").apply {
169+
resolve("com/example").apply {
170+
createClass("MainCls")
171+
resolve("sub").apply {
172+
createClass("Sub1MainCls")
173+
createClass("Sub2MainCls")
174+
}
175+
}
176+
}
177+
val generatedSrcDir =
178+
projectDir.resolve("build/generated/kotlin").apply {
179+
createClass("BuildGenCls")
180+
resolve("sub").apply {
181+
createClass("Sub1BuildGenCls")
182+
createClass("Sub2BuildGenCls")
183+
}
184+
}
185+
val customGeneratedSrcDir =
186+
projectDir.resolve("src/customGenerated/kotlin").apply {
187+
createClass("CustomGenCls")
188+
resolve("sub").apply {
189+
createClass("Sub1CustomGenCls")
190+
createClass("Sub2CustomGenCls")
191+
}
192+
}
193+
194+
fun TestScope.createDss(): DokkaSourceSetSpec {
195+
val dss = project.createDokkaSourceSetSpec(testCase.name.testName)
196+
dss.sourceRoots.from(mainSourceDir, generatedSrcDir, customGeneratedSrcDir)
197+
return dss
198+
}
199+
200+
fun DokkaSourceSetSpec.inputSourceFilesToRelativePaths(): List<Path> =
201+
inputSourceFiles.map { it.toPath().relativeTo(projectDir) }
202+
203+
test("build/generated should be excluded by default") {
204+
val dss = createDss()
205+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
206+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
207+
Path("src/customGenerated/kotlin/sub/Sub1CustomGenCls.kt"),
208+
Path("src/customGenerated/kotlin/sub/Sub2CustomGenCls.kt"),
209+
Path("src/main/kotlin/com/example/MainCls.kt"),
210+
Path("src/main/kotlin/com/example/sub/Sub1MainCls.kt"),
211+
Path("src/main/kotlin/com/example/sub/Sub2MainCls.kt"),
212+
)
213+
}
214+
test("when suppressGeneratedFiles is set to false, expect all source files are included") {
215+
val dss = createDss()
216+
dss.suppressGeneratedFiles.set(false)
217+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
218+
Path("build/generated/kotlin/BuildGenCls.kt"),
219+
Path("build/generated/kotlin/sub/Sub1BuildGenCls.kt"),
220+
Path("build/generated/kotlin/sub/Sub2BuildGenCls.kt"),
221+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
222+
Path("src/customGenerated/kotlin/sub/Sub1CustomGenCls.kt"),
223+
Path("src/customGenerated/kotlin/sub/Sub2CustomGenCls.kt"),
224+
Path("src/main/kotlin/com/example/MainCls.kt"),
225+
Path("src/main/kotlin/com/example/sub/Sub1MainCls.kt"),
226+
Path("src/main/kotlin/com/example/sub/Sub2MainCls.kt"),
227+
)
228+
}
229+
test("input source files should only contain files, not directories") {
230+
val dss = createDss()
231+
dss.suppressGeneratedFiles.set(false)
232+
dss.inputSourceFiles.files.shouldForAll { it.shouldBeAFile() }
233+
}
234+
test("expect files can be excluded by exact path") {
235+
val dss = createDss()
236+
dss.suppressGeneratedFiles.set(false)
237+
dss.suppressedFiles.from(
238+
"build/generated/kotlin/sub/Sub1BuildGenCls.kt",
239+
"src/customGenerated/kotlin/sub/Sub1CustomGenCls.kt",
240+
"src/main/kotlin/com/example/sub/Sub1MainCls.kt",
241+
)
242+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
243+
Path("build/generated/kotlin/BuildGenCls.kt"),
244+
Path("build/generated/kotlin/sub/Sub2BuildGenCls.kt"),
245+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
246+
Path("src/customGenerated/kotlin/sub/Sub2CustomGenCls.kt"),
247+
Path("src/main/kotlin/com/example/MainCls.kt"),
248+
Path("src/main/kotlin/com/example/sub/Sub2MainCls.kt"),
249+
)
250+
}
251+
test("expect files can be excluded by base directories") {
252+
val dss = createDss()
253+
dss.suppressGeneratedFiles.set(false)
254+
dss.suppressedFiles.from(
255+
"build/",
256+
"src/",
257+
)
258+
dss.inputSourceFilesToRelativePaths().shouldBeEmpty()
259+
}
260+
test("expect files can be excluded by sub directories") {
261+
val dss = createDss()
262+
dss.suppressGeneratedFiles.set(false)
263+
dss.suppressedFiles.from(
264+
"build/generated/kotlin/sub/",
265+
"src/customGenerated/kotlin/sub/",
266+
"src/main/kotlin/com/example/sub/",
267+
)
268+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
269+
Path("build/generated/kotlin/BuildGenCls.kt"),
270+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
271+
Path("src/main/kotlin/com/example/MainCls.kt"),
272+
)
273+
}
274+
test("expect nested files can be excluded") {
275+
val dss = createDss()
276+
dss.suppressGeneratedFiles.set(false)
277+
dss.suppressedFiles.from(
278+
"build/generated/kotlin/sub/",
279+
"src/customGenerated/kotlin/sub/",
280+
"src/main/kotlin/com/example/sub/",
281+
)
282+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
283+
Path("build/generated/kotlin/BuildGenCls.kt"),
284+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
285+
Path("src/main/kotlin/com/example/MainCls.kt"),
286+
)
287+
}
288+
test("expect suppressGeneratedFiles and suppressedFiles (by specific files) exclude all generated and specified files") {
289+
val dss = createDss()
290+
dss.suppressGeneratedFiles.set(true)
291+
dss.suppressedFiles.from(
292+
"src/customGenerated/kotlin/sub/Sub1CustomGenCls.kt",
293+
"src/customGenerated/kotlin/sub/Sub2CustomGenCls.kt",
294+
"src/main/kotlin/com/example/sub/Sub1MainCls.kt",
295+
"src/main/kotlin/com/example/sub/Sub2MainCls.kt",
296+
)
297+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
298+
Path("src/customGenerated/kotlin/CustomGenCls.kt"),
299+
Path("src/main/kotlin/com/example/MainCls.kt"),
300+
)
301+
}
302+
test("expect suppressGeneratedFiles and suppressedFiles (by directories) exclude all generated and specified files") {
303+
val dss = createDss()
304+
dss.suppressGeneratedFiles.set(true)
305+
dss.suppressedFiles.from(
306+
"src/customGenerated/kotlin/",
307+
)
308+
dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
309+
Path("src/main/kotlin/com/example/MainCls.kt"),
310+
Path("src/main/kotlin/com/example/sub/Sub1MainCls.kt"),
311+
Path("src/main/kotlin/com/example/sub/Sub2MainCls.kt"),
312+
)
313+
}
314+
}
144315
}) {
145316
companion object {
146317
private fun createProject(): Project {

0 commit comments

Comments
 (0)