|
1 | 1 | import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
2 | 2 | import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
|
| 3 | +import org.openapitools.generator.gradle.plugin.tasks.GenerateTask |
3 | 4 |
|
4 | 5 | plugins {
|
5 | 6 | application
|
@@ -191,50 +192,72 @@ spotless {
|
191 | 192 | val basePackage = project.group.toString()
|
192 | 193 | val openAPIgenerationPath = "$buildDir/generated/openapi"
|
193 | 194 |
|
194 |
| -// We let the Gradle OpenAPI generator plugin build data classes and API interfaces based on the provided |
195 |
| -// OpenAPI specification. That way, the code is forced to stay in sync with the API specification. |
196 |
| -openApiGenerate { |
197 |
| - generatorName.set("kotlin-server") |
198 |
| - inputSpec.set(layout.projectDirectory.file("../api/model-server.yaml").toString()) |
199 |
| - outputDir.set(openAPIgenerationPath) |
200 |
| - packageName.set(basePackage) |
201 |
| - packageName.set(basePackage) |
202 |
| - apiPackage.set(basePackage) |
203 |
| - modelPackage.set(basePackage) |
204 |
| - // WARNING: there are patched mustache files used! |
205 |
| - templateDir.set("$projectDir/src/main/resources/openapi/templates") |
206 |
| - configOptions.set( |
207 |
| - mapOf( |
208 |
| - "library" to "ktor", |
209 |
| - "omitGradleWrapper" to "true", |
210 |
| - "featureResources" to "true", |
211 |
| - "featureAutoHead" to "false", |
212 |
| - "featureCompression" to "false", |
213 |
| - "featureHSTS" to "false", |
214 |
| - "featureMetrics" to "false", |
215 |
| - ), |
216 |
| - ) |
217 |
| -} |
| 195 | +// Pairs of the different OpenAPI files we use. Each pair must have its own 'category' as first argument as these |
| 196 | +// are used to generate corresponding packages |
| 197 | +val openApiFiles = listOf( |
| 198 | + Pair("public", "model-server"), |
| 199 | + Pair("light", "model-server-light"), |
| 200 | + Pair("html", "model-server-html"), |
| 201 | + Pair("deprecated", "model-server-deprecated"), |
| 202 | +) |
| 203 | + |
| 204 | +// generate tasks for each OpenAPI file |
| 205 | +openApiFiles.forEach { |
| 206 | + val targetTaskName = "openApiGenerate-${it.second}" |
| 207 | + val targetPackageName = "$basePackage.api.${it.first}" |
| 208 | + val outputPath = "$openAPIgenerationPath/${it.first}" |
| 209 | + tasks.register<GenerateTask>(targetTaskName) { |
| 210 | + // we let the Gradle OpenAPI generator plugin build data classes and API interfaces based on the provided |
| 211 | + // OpenAPI specification. That way, the code is forced to stay in sync with the API specification. |
| 212 | + generatorName.set("kotlin-server") |
| 213 | + inputSpec.set(layout.projectDirectory.file("../api/${it.second}.yaml").toString()) |
| 214 | + outputDir.set(outputPath) |
| 215 | + packageName.set(targetPackageName) |
| 216 | + apiPackage.set(targetPackageName) |
| 217 | + modelPackage.set(targetPackageName) |
| 218 | + // WARNING: there are patched mustache files used! |
| 219 | + templateDir.set("$projectDir/src/main/resources/openapi/templates") |
| 220 | + configOptions.set( |
| 221 | + mapOf( |
| 222 | + "library" to "ktor", |
| 223 | + "omitGradleWrapper" to "true", |
| 224 | + "featureResources" to "true", |
| 225 | + "featureAutoHead" to "false", |
| 226 | + "featureCompression" to "false", |
| 227 | + "featureHSTS" to "false", |
| 228 | + "featureMetrics" to "false", |
| 229 | + ), |
| 230 | + ) |
| 231 | + // generate only Paths and Models |
| 232 | + globalProperties.putAll( |
| 233 | + mapOf( |
| 234 | + "models" to "", |
| 235 | + "apis" to "", |
| 236 | + "supportingFiles" to "Paths.kt", |
| 237 | + ), |
| 238 | + ) |
| 239 | + } |
218 | 240 |
|
219 |
| -// Ensure that the OpenAPI generator runs before starting to compile |
220 |
| -tasks.named("processResources") { |
221 |
| - dependsOn("openApiGenerate") |
222 |
| -} |
223 |
| -tasks.named("compileKotlin") { |
224 |
| - dependsOn("openApiGenerate") |
225 |
| -} |
226 |
| -tasks.named("runKtlintCheckOverMainSourceSet") { |
227 |
| - dependsOn("openApiGenerate") |
228 |
| -} |
| 241 | + // Ensure that the OpenAPI generator runs before starting to compile |
| 242 | + tasks.named("processResources") { |
| 243 | + dependsOn(targetTaskName) |
| 244 | + } |
| 245 | + tasks.named("compileKotlin") { |
| 246 | + dependsOn(targetTaskName) |
| 247 | + } |
| 248 | + tasks.named("runKtlintCheckOverMainSourceSet") { |
| 249 | + dependsOn(targetTaskName) |
| 250 | + } |
229 | 251 |
|
230 |
| -// do not apply ktlint on the generated files |
231 |
| -ktlint { |
232 |
| - filter { |
233 |
| - exclude { |
234 |
| - it.file.toPath().toAbsolutePath().startsWith(openAPIgenerationPath) |
| 252 | + // do not apply ktlint on the generated files |
| 253 | + ktlint { |
| 254 | + filter { |
| 255 | + exclude { |
| 256 | + it.file.toPath().toAbsolutePath().startsWith(outputPath) |
| 257 | + } |
235 | 258 | }
|
236 | 259 | }
|
237 |
| -} |
238 | 260 |
|
239 |
| -// add openAPI generated artifacts to the sourceSets |
240 |
| -java.sourceSets.getByName("main").java.srcDir(file("$openAPIgenerationPath/src/main/kotlin")) |
| 261 | + // add openAPI generated artifacts to the sourceSets |
| 262 | + java.sourceSets.getByName("main").java.srcDir(file("$outputPath/src/main/kotlin")) |
| 263 | +} |
0 commit comments