Skip to content

Commit f51177f

Browse files
authored
Merge pull request #749 from Kotlin/kdocs-only-on-publish
[KDocs] auto preprocess only when publishing
2 parents 0bb38ea + fd20ae4 commit f51177f

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

core/build.gradle.kts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,14 @@ tasks.withType<KorroTask> {
175175
val generatedSourcesFolderName = "generated-sources"
176176

177177
// Backup the kotlin source files location
178-
val kotlinMainSources: FileCollection = kotlin.sourceSets.main.get().kotlin.sourceDirectories
179-
val kotlinTestSources: FileCollection = kotlin.sourceSets.test.get().kotlin.sourceDirectories
178+
val kotlinMainSources = kotlin.sourceSets.main
179+
.get()
180+
.kotlin.sourceDirectories
181+
.toList()
182+
val kotlinTestSources = kotlin.sourceSets.test
183+
.get()
184+
.kotlin.sourceDirectories
185+
.toList()
180186

181187
fun pathOf(vararg parts: String) = parts.joinToString(File.separator)
182188

@@ -193,6 +199,8 @@ val processKDocsMain by creatingProcessDocTask(processKDocsMainSources) {
193199
}
194200
task {
195201
group = "KDocs"
202+
// making sure it always runs, so targets is set
203+
outputs.upToDateWhen { false }
196204
}
197205
}
198206

@@ -203,17 +211,25 @@ idea {
203211
}
204212
}
205213

206-
// Modify all Jar tasks such that before running the Kotlin sources are set to
207-
// the target of processKdocMain and they are returned back to normal afterwards.
214+
// if `processKDocsMain` runs, the Jar tasks must run after it so the generated-sources are there
208215
tasks.withType<Jar> {
209-
dependsOn(processKDocsMain)
210-
mustRunAfter(tasks.generateKeywordsSrc)
211-
outputs.upToDateWhen { false }
216+
mustRunAfter(tasks.generateKeywordsSrc, processKDocsMain)
217+
}
212218

219+
// If `changeJarTask` is run, modify all Jar tasks such that before running the Kotlin sources are set to
220+
// the target of `processKdocMain`, and they are returned to normal afterward.
221+
// This is usually only done when publishing
222+
val changeJarTask by tasks.creating {
223+
outputs.upToDateWhen { false }
213224
doFirst {
214-
kotlin.sourceSets.main {
215-
kotlin.setSrcDirs(
216-
processKDocsMain.targets
225+
tasks.withType<Jar> {
226+
dependsOn(processKDocsMain)
227+
doFirst {
228+
val targets = processKDocsMain.targets
229+
require(targets.toList().isNotEmpty()) {
230+
logger.error("`processKDocsMain.targets` was empty, did it run before this task?")
231+
}
232+
val srcDirs = targets
217233
.filterNot {
218234
pathOf("src", "test", "kotlin") in it.path ||
219235
pathOf("src", "test", "java") in it.path
@@ -222,15 +238,32 @@ tasks.withType<Jar> {
222238
kotlinMainSources.filter {
223239
pathOf("build", "generated") in it.path
224240
},
225-
), // Include generated sources (which were excluded above)
226-
)
241+
) // Include generated sources (which were excluded above)
242+
243+
kotlin.sourceSets.main {
244+
kotlin.setSrcDirs(srcDirs)
245+
}
246+
logger.lifecycle("$this is run with modified sources: \"$generatedSourcesFolderName\"")
247+
}
248+
249+
doLast {
250+
kotlin.sourceSets.main {
251+
kotlin.setSrcDirs(kotlinMainSources)
252+
}
253+
}
227254
}
228255
}
256+
}
229257

230-
doLast {
231-
kotlin.sourceSets.main {
232-
kotlin.setSrcDirs(kotlinMainSources)
233-
}
258+
// modify all publishing tasks to depend on `changeJarTask` so the sources are swapped out with generated sources
259+
tasks.named { it.startsWith("publish") }.configureEach {
260+
dependsOn(processKDocsMain, changeJarTask)
261+
}
262+
263+
// Exclude the generated/processed sources from the IDE
264+
idea {
265+
module {
266+
excludeDirs.add(file(generatedSourcesFolderName))
234267
}
235268
}
236269

0 commit comments

Comments
 (0)