|
| 1 | +import com.github.gradle.node.npm.task.NpxTask |
| 2 | + |
| 3 | +plugins { |
| 4 | + base |
| 5 | + alias(libs.plugins.node) |
| 6 | +} |
| 7 | + |
| 8 | +description = "OpenAPI specification for the model-server" |
| 9 | + |
| 10 | +val specDir = layout.projectDirectory.dir("specifications") |
| 11 | + |
| 12 | +val bundleDir = layout.buildDirectory.dir("bundled").get() |
| 13 | + |
| 14 | +val joinedFile = layout.buildDirectory.file("model-server.yaml").get() |
| 15 | + |
| 16 | +// We bundle the specs to apply the decorator that prepends the server path to each route. |
| 17 | +val bundleSpecs = tasks.register<NpxTask>("bundleSpecs") { |
| 18 | + description = "preprocesses OpenAPI specifications before joining them" |
| 19 | + |
| 20 | + dependsOn(tasks.getByName("npmInstall")) |
| 21 | + |
| 22 | + inputs.dir(specDir) |
| 23 | + |
| 24 | + outputs.cacheIf { true } |
| 25 | + outputs.dir(bundleDir) |
| 26 | + |
| 27 | + command.set("redocly") |
| 28 | + args.addAll("bundle", "--output", bundleDir.toString()) |
| 29 | +} |
| 30 | + |
| 31 | +// We combine all specifications into one to deduplicate things like the Problem type |
| 32 | +val joinSpecs = tasks.register<NpxTask>("joinSpecs") { |
| 33 | + description = "combines all OpenAPI specifications into a single one" |
| 34 | + |
| 35 | + dependsOn(tasks.getByName("npmInstall")) |
| 36 | + dependsOn(bundleSpecs) |
| 37 | + |
| 38 | + inputs.dir(bundleDir) |
| 39 | + |
| 40 | + outputs.cacheIf { true } |
| 41 | + outputs.file(joinedFile) |
| 42 | + |
| 43 | + command.set("redocly") |
| 44 | + args.addAll("join", "--output", joinedFile.toString()) |
| 45 | + // We sort the v2 file first because it determines the meta-data of the generated joined file. |
| 46 | + // This list of files needs to be kept in sync with the contents of redocly.yaml. We cannot dynamically detect the |
| 47 | + // files generated by redocly here as NPM argas can only be passed in the configuration phase of Gradle. At that |
| 48 | + // point in time, no files have been generated yet on fresh builds. |
| 49 | + args.addAll( |
| 50 | + listOf( |
| 51 | + "model-server-v2.yaml", |
| 52 | + "model-server-v1.yaml", |
| 53 | + "model-server-operative.yaml", |
| 54 | + ).map { bundleDir.file(it).toString() }, |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +// This provides the resulting joined OpenAPI specification to the model-server project to be declared as a dependency. |
| 59 | +// Cf. https://docs.gradle.org/current/userguide/cross_project_publications.html#cross_project_publications |
| 60 | +val openApiSpec by configurations.creating { |
| 61 | + isCanBeConsumed = true |
| 62 | + isCanBeResolved = false |
| 63 | +} |
| 64 | +artifacts { |
| 65 | + add(openApiSpec.name, joinedFile) { |
| 66 | + builtBy(joinSpecs) |
| 67 | + } |
| 68 | +} |
0 commit comments