@@ -175,8 +175,14 @@ tasks.withType<KorroTask> {
175
175
val generatedSourcesFolderName = " generated-sources"
176
176
177
177
// 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()
180
186
181
187
fun pathOf (vararg parts : String ) = parts.joinToString(File .separator)
182
188
@@ -193,6 +199,8 @@ val processKDocsMain by creatingProcessDocTask(processKDocsMainSources) {
193
199
}
194
200
task {
195
201
group = " KDocs"
202
+ // making sure it always runs, so targets is set
203
+ outputs.upToDateWhen { false }
196
204
}
197
205
}
198
206
@@ -203,17 +211,25 @@ idea {
203
211
}
204
212
}
205
213
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
208
215
tasks.withType<Jar > {
209
- dependsOn(processKDocsMain)
210
- mustRunAfter(tasks.generateKeywordsSrc)
211
- outputs.upToDateWhen { false }
216
+ mustRunAfter(tasks.generateKeywordsSrc, processKDocsMain)
217
+ }
212
218
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 }
213
224
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
217
233
.filterNot {
218
234
pathOf(" src" , " test" , " kotlin" ) in it.path ||
219
235
pathOf(" src" , " test" , " java" ) in it.path
@@ -222,15 +238,32 @@ tasks.withType<Jar> {
222
238
kotlinMainSources.filter {
223
239
pathOf(" build" , " generated" ) in it.path
224
240
},
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
+ }
227
254
}
228
255
}
256
+ }
229
257
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))
234
267
}
235
268
}
236
269
0 commit comments