From cbdd74f55dccc19185ea43655e21109a09b003fe Mon Sep 17 00:00:00 2001 From: Gabriel Feo Date: Thu, 2 Mar 2023 01:55:07 +0000 Subject: [PATCH 1/3] Support overriding API spec with property --- build.gradle.kts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 1ecdb96f6..0c81e19fb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,10 @@ group = "com.github.gabrielfeo" version = "SNAPSHOT" val repoUrl = "https://github.com/gabrielfeo/gradle-enterprise-api-kotlin" +val localSpecPath: String? by project + val downloadApiSpec by tasks.registering { + onlyIf { localSpecPath == null } val geVersion = providers.gradleProperty("gradle.enterprise.version").get() val specName = "gradle-enterprise-$geVersion-api.yaml" val spec = resources.text.fromUri("https://docs.gradle.com/enterprise/api-manual/ref/$specName") @@ -28,7 +31,14 @@ val downloadApiSpec by tasks.registering { openApiGenerate { generatorName.set("kotlin") - inputSpec.set(downloadApiSpec.map { it.outputs.files.first().absolutePath }) + val spec = when (localSpecPath) { + null -> downloadApiSpec.map { it.outputs.files.first().absolutePath } + else -> provider { + println(File(localSpecPath).absolutePath) + File(localSpecPath).absolutePath + } + } + inputSpec.set(spec) val generateDir = project.layout.buildDirectory.file("generated/openapi-generator") outputDir.set(generateDir.map { it.asFile.absolutePath }) val ignoreFile = project.layout.projectDirectory.file(".openapi-generator-ignore") From ebfcde63d1d9fe5976deb6ea9601d07def24c1cb Mon Sep 17 00:00:00 2001 From: Gabriel Feo Date: Thu, 2 Mar 2023 02:01:43 +0000 Subject: [PATCH 2/3] Workarounds for new spec --- build.gradle.kts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 0c81e19fb..06c9cde5e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -67,6 +67,38 @@ tasks.openApiGenerate.configure { ) } } + // Workaround for properties generated with `arrayListOf(null,null)` as default value + doLast { + val srcDir = File(outputDir.get(), "src/main/kotlin") + ant.withGroovyBuilder { + "replaceregexp"( + "match" to """arrayListOf\(null,null\)""", + "replace" to """emptyList()""", + "flags" to "gm", + ) { + "fileset"( + "dir" to srcDir + ) + } + } + } + // Workaround for missing imports of exploded queries + doLast { + val srcDir = File(outputDir.get(), "src/main/kotlin") + val modelPackage = openApiGenerate.modelPackage.get() + val modelPackagePattern = modelPackage.replace(".", "\\.") + ant.withGroovyBuilder { + "replaceregexp"( + "match" to """(?:import $modelPackagePattern.[.\w]+\s)+""", + "replace" to "import $modelPackage.*\n", + "flags" to "m", + ) { + "fileset"( + "dir" to srcDir + ) + } + } + } } sourceSets { From 6481612e986fcf5bbdf54fa9ff5433e5d1fcdf26 Mon Sep 17 00:00:00 2001 From: Gabriel Feo Date: Fri, 3 Mar 2023 14:32:12 +0000 Subject: [PATCH 3/3] Support a custom remote URL --- build.gradle.kts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 06c9cde5e..738d1cca6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,29 +14,32 @@ group = "com.github.gabrielfeo" version = "SNAPSHOT" val repoUrl = "https://github.com/gabrielfeo/gradle-enterprise-api-kotlin" -val localSpecPath: String? by project +val localSpecPath = providers.gradleProperty("localSpecPath") +val remoteSpecUrl = providers.gradleProperty("remoteSpecUrl").orElse( + providers.gradleProperty("gradle.enterprise.version").map { geVersion -> + val specName = "gradle-enterprise-$geVersion-api.yaml" + "https://docs.gradle.com/enterprise/api-manual/ref/$specName" + } +) val downloadApiSpec by tasks.registering { - onlyIf { localSpecPath == null } - val geVersion = providers.gradleProperty("gradle.enterprise.version").get() - val specName = "gradle-enterprise-$geVersion-api.yaml" - val spec = resources.text.fromUri("https://docs.gradle.com/enterprise/api-manual/ref/$specName") + onlyIf { !localSpecPath.isPresent() } + val spec = resources.text.fromUri(remoteSpecUrl) + val specName = remoteSpecUrl.map { it.substringAfterLast('/') } val outFile = project.layout.buildDirectory.file(specName) - inputs.property("GE version", geVersion) + inputs.property("Spec URL", remoteSpecUrl) outputs.file(outFile) doLast { + logger.info("Downloaded API spec from ${remoteSpecUrl.get()}") spec.asFile().renameTo(outFile.get().asFile) } } openApiGenerate { generatorName.set("kotlin") - val spec = when (localSpecPath) { - null -> downloadApiSpec.map { it.outputs.files.first().absolutePath } - else -> provider { - println(File(localSpecPath).absolutePath) - File(localSpecPath).absolutePath - } + val spec = when { + localSpecPath.isPresent() -> localSpecPath.map { File(it).absolutePath } + else -> downloadApiSpec.map { it.outputs.files.first().absolutePath } } inputSpec.set(spec) val generateDir = project.layout.buildDirectory.file("generated/openapi-generator") @@ -52,6 +55,9 @@ openApiGenerate { } tasks.openApiGenerate.configure { + doFirst { + logger.info("Using API spec ${inputSpec.get()}") + } // Replace Response with X in every method return type of GradleEnterpriseApi.kt doLast { val apiFile = File(