Skip to content

Commit

Permalink
Make the task @CacheableTask, and explicitly add @OutputDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jul 3, 2024
1 parent 2c3eb1f commit 004f7d9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
9 changes: 6 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ Add the following to your `build.gradle.kts` file.

```kotlin
plugin {
id("io.github.nomisrev.openapi.plugin") version "0.0.1"
id("io.github.nomisrev.openapi-kt-plugin") version "0.0.4"
}

openApiConfig {
spec.set(file("openapi.json"))
spec("OpenAI", file("openai.yaml")) {
// packageName = "my.company.openai.generated"
}
}
```

Then run the following command to generate the code.
Then run the following command to generate the code,
but it will also automatically run when you build your project.

```shell
./gradlew generateOpenApi
Expand Down
4 changes: 3 additions & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ plugins {
id("io.github.nomisrev.openapi-kt-plugin") version "0.0.4"
}

openApiConfig { spec("OpenAI", file("openai.yaml")) { packageName = "io.github.nomisrev.openai" } }
openApiConfig { spec("OpenAI", file("openai.yaml")) {
packageName = "io.github.nomisrev.openai" }
}

kotlin {
jvm()
Expand Down
17 changes: 16 additions & 1 deletion plugin/README.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Module OpenAPI Kotlin Gradle Plugin

OpenAPI Kotlin Gradle Plugin is a Gradle plugin that uses `generation` to generate Kotlin Code.
OpenAPI Kotlin Gradle Plugin is a Gradle plugin that uses `generation` to generate Kotlin Code.

To use the plugin, you need to add the following to your `build.gradle.kts` file:

```kotlin
openApiConfig {
spec("OpenAI", file("openai.yaml")) {
// packageName = "io.github.nomisrev.openai"
}
}
```

This will generate the Kotlin code from the `openai.yaml` file,
and the generated code will be placed in the `build/generated` directory.

The Gradle plugin automatically adds the generated code to the source set of the project.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ package io.github.nomisrev.openapi.plugin

import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.workers.WorkerExecutor

@CacheableTask
abstract class GenerateClientTask : DefaultTask() {
init {
description = "Generate a Kotlin client based on a configure OpenAPI Specification."
group = "openapi"
}

@get:Input
@get:SkipWhenEmpty
@get:Option(option = "OpenApiSpec", description = "The OpenAPI configuration")
abstract val spec: ListProperty<SpecDefinition>

@get:OutputDirectory
abstract val output: DirectoryProperty

@Inject abstract fun getWorkerExecutor(): WorkerExecutor

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class OpenAPIPlugin : Plugin<Project> {
val generateOpenApiClient =
register("generateOpenApiClient", GenerateClientTask::class.java) {
it.spec.set(extension.specs)
it.output.set(project.output)
}

maybeCreate("prepareKotlinIdeaImport").dependsOn(generateOpenApiClient)
Expand Down

0 comments on commit 004f7d9

Please sign in to comment.