From f61e4b3360a3c5d16af472f901c1916aa8280948 Mon Sep 17 00:00:00 2001 From: Gabriel Feo Date: Tue, 3 Jan 2023 21:07:34 +0000 Subject: [PATCH] Make methods of GradleEnterpriseApi suspend Even with useCoroutines, openapi-generator outputs methods with Response<> wrapping by default. Response<> shouldn't be useful here, so they're replaced with the bare types. --- build.gradle.kts | 19 +++++++++++++++++++ .../gradle/enterprise/api/ApiExtensions.kt | 3 +-- .../internal/operator/pagedUntilLastBuild.kt | 2 +- .../internal/operator/withGradleAttributes.kt | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7b284214..57ddd85d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,6 +37,25 @@ openApiGenerate { packageName.set("com.gabrielfeo.gradle.enterprise.api.internal") invokerPackage.set("com.gabrielfeo.gradle.enterprise.api.internal") additionalProperties.put("library", "jvm-retrofit2") + additionalProperties.put("useCoroutines", true) +} + +tasks.openApiGenerate.configure { + // Replace Response with X in every method return type of GradleEnterpriseApi.kt + doLast { + val apiFile = File( + outputDir.get(), + "src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt", + ) + ant.withGroovyBuilder { + "replaceregexp"( + "file" to apiFile, + "match" to ": Response<(.*?)>$", + "replace" to """: \1""", + "flags" to "gm", + ) + } + } } sourceSets { diff --git a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/ApiExtensions.kt b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/ApiExtensions.kt index 1a5d12e6..bcc05e1b 100644 --- a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/ApiExtensions.kt +++ b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/ApiExtensions.kt @@ -8,7 +8,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.* -import retrofit2.await /** * Gets builds on demand from the API, in as many requests as necessary. It allows @@ -31,7 +30,7 @@ fun GradleEnterpriseApi.getBuildsFlow( fromInstant = fromInstant, fromBuild = fromBuild, maxBuilds = API_MAX_BUILDS, - ).await() + ) val pagedBuilds = firstBuilds.asFlow().pagedUntilLastBuild(maxPerRequest = API_MAX_BUILDS) emitAll(pagedBuilds) } diff --git a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/pagedUntilLastBuild.kt b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/pagedUntilLastBuild.kt index da677c7b..35e3c4d3 100644 --- a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/pagedUntilLastBuild.kt +++ b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/pagedUntilLastBuild.kt @@ -23,7 +23,7 @@ internal fun Flow.pagedUntilLastBuild( if (lastBuildId.isEmpty()) { return@flow } else while (true) { - val builds = api.getBuilds(fromBuild = lastBuildId, maxBuilds = maxPerRequest).await() + val builds = api.getBuilds(fromBuild = lastBuildId, maxBuilds = maxPerRequest) emitAll(builds.asFlow()) when { builds.isEmpty() || builds.size < API_MAX_BUILDS -> break diff --git a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/withGradleAttributes.kt b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/withGradleAttributes.kt index ea3f1818..9fda82b0 100644 --- a/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/withGradleAttributes.kt +++ b/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/operator/withGradleAttributes.kt @@ -18,7 +18,7 @@ internal fun Flow.withGradleAttributes( ): Flow> = map { build -> build to scope.async { - api.getGradleAttributes(build.id).await() + api.getGradleAttributes(build.id) } }.buffer(Int.MAX_VALUE).map { (build, attrs) -> build to attrs.await()