Skip to content

Commit 097d0bf

Browse files
committed
🐛 Add dependency to all Kotlin compile tasks
1 parent a97e4b5 commit 097d0bf

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

plugin/src/main/kotlin/cz/eman/swagger/codegen/SwaggerCodeGenPlugin.kt

+15-26
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ open class SwaggerCodeGenPlugin : Plugin<Project> {
3232
val configsExt = project.extensions.create(SWAGGER_TASK, SwaggerCodeGenConfig::class.java)
3333
autoHookJava(project, configsExt)
3434
autoHookKotlin(project, configsExt)
35-
autoHookKotlin(project, configsExt, true)
3635
}
3736

3837
/**
@@ -69,41 +68,31 @@ open class SwaggerCodeGenPlugin : Plugin<Project> {
6968
}
7069

7170
/**
72-
* Auto-hooks openApi generation to project that contain compile task [COMPILE_KOTLIN] or [COMPILE_KOTLIN_JVM].
73-
* Finds the task and crates java generator task for it with dependency.
71+
* Auto-hooks openApi generation to project that contain Kotlin compile task.
72+
* Finds the task and creates java generator task for it with dependency.
7473
*
7574
* @param project used to search for compile tasks
7675
* @param configsExt swagger gen config
7776
*/
78-
private fun autoHookKotlin(
79-
project: Project,
80-
configsExt: SwaggerCodeGenConfig,
81-
isMpp: Boolean = false
82-
) {
83-
var hooked = false
77+
private fun autoHookKotlin(project: Project, configsExt: SwaggerCodeGenConfig) {
8478
project.afterEvaluate {
8579
if (configsExt.autoHook) {
86-
val compileTaskName = if (isMpp) {
87-
COMPILE_KOTLIN_JVM
88-
} else {
89-
COMPILE_KOTLIN
80+
val generators = configsExt.configs.mapNotNull { taskConfig ->
81+
createGenerator(
82+
project,
83+
LANGUAGE_KOTLIN,
84+
configsExt,
85+
taskConfig
86+
)
9087
}
91-
project.getTasksByName(compileTaskName, false).firstOrNull()?.let { compileKotlin ->
92-
configsExt.configs.forEach { taskConfig ->
93-
createGenerator(
94-
project,
95-
LANGUAGE_KOTLIN,
96-
configsExt,
97-
taskConfig
98-
)?.let { task ->
99-
compileKotlin.dependsOn(task)
100-
}
101-
}
102-
hooked = true
88+
89+
val compileTasks = project.kotlinCompileTasks()
90+
logger.info("Kotlin auto-hooked: ${compileTasks.isNotEmpty()}")
91+
compileTasks.all { task ->
92+
task.dependsOn(generators)
10393
}
10494
}
10595
}
106-
logger.info("Kotlin (isMpp: $isMpp) auto-hooked: $hooked")
10796
}
10897

10998
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cz.eman.swagger.codegen
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.tasks.TaskCollection
5+
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
6+
7+
internal fun Project.kotlinCompileTasks(): TaskCollection<KotlinCompile<*>> =
8+
tasks.withType(KotlinCompile::class.java)

sample/kotlin-client/build.gradle.kts

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cz.eman.swagger.codegen.SwaggerCodeGenConfig
2-
import cz.eman.swagger.codegen.SwaggerCodeGenTask
32
import cz.eman.swagger.codegen.SwaggerCodeGenTaskConfig
43
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
54

@@ -93,7 +92,3 @@ tasks.withType<KotlinCompile> {
9392
jvmTarget = javaVersion.toString()
9493
}
9594
}
96-
97-
tasks.withType<SwaggerCodeGenTask> {
98-
project.tasks.findByName("kaptGenerateStubsKotlin")?.dependsOn(this)
99-
}

0 commit comments

Comments
 (0)