Skip to content

Commit

Permalink
🔀 Fix task dependency resolution (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
smidf authored Dec 27, 2023
2 parents 5a4d19a + 097d0bf commit f33f458
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }

[libraries]
openapiGenerator = { group = "org.openapitools", name = "openapi-generator", version.ref = "openapi" }
kotlinPluginApi = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin-api", version.ref = "kotlin" }

jUnit = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "jUnit" }
kotest = { group = "io.kotest", name = "kotest-runner-junit5", version.ref = "kotest" }
1 change: 1 addition & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {

dependencies {
implementation(libs.openapiGenerator)
implementation(libs.kotlinPluginApi)

testImplementation(libs.jUnit)
testImplementation(libs.kotest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ open class SwaggerCodeGenPlugin : Plugin<Project> {
val configsExt = project.extensions.create(SWAGGER_TASK, SwaggerCodeGenConfig::class.java)
autoHookJava(project, configsExt)
autoHookKotlin(project, configsExt)
autoHookKotlin(project, configsExt, true)
}

/**
Expand Down Expand Up @@ -69,41 +68,31 @@ open class SwaggerCodeGenPlugin : Plugin<Project> {
}

/**
* Auto-hooks openApi generation to project that contain compile task [COMPILE_KOTLIN] or [COMPILE_KOTLIN_JVM].
* Finds the task and crates java generator task for it with dependency.
* Auto-hooks openApi generation to project that contain Kotlin compile task.
* Finds the task and creates java generator task for it with dependency.
*
* @param project used to search for compile tasks
* @param configsExt swagger gen config
*/
private fun autoHookKotlin(
project: Project,
configsExt: SwaggerCodeGenConfig,
isMpp: Boolean = false
) {
var hooked = false
private fun autoHookKotlin(project: Project, configsExt: SwaggerCodeGenConfig) {
project.afterEvaluate {
if (configsExt.autoHook) {
val compileTaskName = if (isMpp) {
COMPILE_KOTLIN_JVM
} else {
COMPILE_KOTLIN
val generators = configsExt.configs.mapNotNull { taskConfig ->
createGenerator(
project,
LANGUAGE_KOTLIN,
configsExt,
taskConfig
)
}
project.getTasksByName(compileTaskName, false).firstOrNull()?.let { compileKotlin ->
configsExt.configs.forEach { taskConfig ->
createGenerator(
project,
LANGUAGE_KOTLIN,
configsExt,
taskConfig
)?.let { task ->
compileKotlin.dependsOn(task)
}
}
hooked = true

val compileTasks = project.kotlinCompileTasks()
logger.info("Kotlin auto-hooked: ${compileTasks.isNotEmpty()}")
compileTasks.all { task ->
task.dependsOn(generators)
}
}
}
logger.info("Kotlin (isMpp: $isMpp) auto-hooked: $hooked")
}

/**
Expand Down
8 changes: 8 additions & 0 deletions plugin/src/main/kotlin/cz/eman/swagger/codegen/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cz.eman.swagger.codegen

import org.gradle.api.Project
import org.gradle.api.tasks.TaskCollection
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

internal fun Project.kotlinCompileTasks(): TaskCollection<KotlinCompile<*>> =
tasks.withType(KotlinCompile::class.java)
5 changes: 0 additions & 5 deletions sample/kotlin-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cz.eman.swagger.codegen.SwaggerCodeGenConfig
import cz.eman.swagger.codegen.SwaggerCodeGenTask
import cz.eman.swagger.codegen.SwaggerCodeGenTaskConfig
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Expand Down Expand Up @@ -93,7 +92,3 @@ tasks.withType<KotlinCompile> {
jvmTarget = javaVersion.toString()
}
}

tasks.withType<SwaggerCodeGenTask> {
project.tasks.findByName("kaptGenerateStubsKotlin")?.dependsOn(this)
}

0 comments on commit f33f458

Please sign in to comment.