|
| 1 | +import com.fasterxml.jackson.core.type.TypeReference |
| 2 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 3 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory |
| 4 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator |
| 5 | +import com.fasterxml.jackson.module.kotlin.registerKotlinModule |
| 6 | + |
| 7 | +abstract class NewTestApiTask : DefaultTask() { |
| 8 | + |
| 9 | + @get:InputDirectory |
| 10 | + abstract val srcDir: DirectoryProperty |
| 11 | + |
| 12 | + @get:Input |
| 13 | + abstract val srcVersion: Property<String> |
| 14 | + |
| 15 | + @get:Input |
| 16 | + abstract val dstVersion: Property<String> |
| 17 | + |
| 18 | + @get:Input |
| 19 | + abstract val apiVersion: Property<String> |
| 20 | + |
| 21 | + @TaskAction |
| 22 | + fun run() { |
| 23 | + val yamlFactory = YAMLFactory.builder() |
| 24 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) |
| 25 | + .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 26 | + .enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR) |
| 27 | + .build() |
| 28 | + |
| 29 | + val mapper = ObjectMapper(yamlFactory).registerKotlinModule() |
| 30 | + |
| 31 | + val tests = srcDir.get().asFile |
| 32 | + val testDirs = tests.listFiles() |
| 33 | + ?.sorted() |
| 34 | + ?.toList() |
| 35 | + ?.filter { it.isDirectory } |
| 36 | + ?: emptyList() |
| 37 | + |
| 38 | + for (testDir in testDirs) { |
| 39 | + println("checking 'tests/${testDir.name}' ...") |
| 40 | + val srcFile = File(testDir, "inputs/openapi${srcVersion.get()}.yaml") |
| 41 | + val dstFile = File(testDir, "inputs/openapi${dstVersion.get()}.yaml") |
| 42 | + |
| 43 | + if(!srcFile.exists()) { |
| 44 | + println("no '${srcFile.name}' ...") |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if (!dstFile.exists()) { |
| 49 | + println("creating '${dstFile.name}' ...") |
| 50 | + srcFile.copyTo(dstFile) |
| 51 | + } |
| 52 | + |
| 53 | + val apiYaml = mapper.readValue(dstFile, object : TypeReference<MutableMap<String, Any?>>() {}) |
| 54 | + if (apiYaml["openapi"] != apiVersion.get()) { |
| 55 | + println("updating '${dstFile.name}' ...") |
| 56 | + apiYaml["openapi"] = apiVersion.get() |
| 57 | + mapper.writeValue(dstFile, apiYaml) |
| 58 | + } |
| 59 | + |
| 60 | + val inputsFile = File(testDir, "inputs.yaml") |
| 61 | + val inputsYaml = mapper.readValue(inputsFile, object : TypeReference<MutableMap<String, Any?>>() {}) |
| 62 | + @Suppress("UNCHECKED_CAST") val items = inputsYaml["items"] as MutableList<String> |
| 63 | + if (!items.contains("inputs/openapi${dstVersion.get()}.yaml")) { |
| 64 | + println("updating inputs.yaml ...") |
| 65 | + items.indexOf("inputs/openapi${srcVersion.get()}.yaml").let { index -> |
| 66 | + items.add(index + 1, "inputs/openapi${dstVersion.get()}.yaml") |
| 67 | + } |
| 68 | + mapper.writeValue(inputsFile, inputsYaml) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +tasks.register<NewTestApiTask>("newTestApi") { |
| 75 | + group = "utility" |
| 76 | + description = "setup new openapiXX.yaml test files." |
| 77 | + |
| 78 | + srcDir = layout.projectDirectory.dir("src/testInt/resources/tests") |
| 79 | + srcVersion = "31" |
| 80 | + dstVersion = "32" |
| 81 | + apiVersion = "3.2.0" |
| 82 | +} |
0 commit comments