From 24cb72e4bc39064d8fb3b134bba05c29cfbf0199 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Wed, 6 Sep 2023 11:11:23 +1000 Subject: [PATCH] fix(kt): use `GetSchema()` rather than `PullSchema()` as the latter blocks If there is no schema available, `PullSchema()` will block until one is available. This times out the Gradle build. Fixes #354 --- .../kotlin/xyz/block/ftl/gradle/FTLClient.kt | 34 +++---------------- .../kotlin/xyz/block/ftl/gradle/FTLPlugin.kt | 4 +-- .../xyz/block/ftl/gradle/ModuleGenerator.kt | 21 ++++++------ 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLClient.kt b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLClient.kt index e81dd440ef..5f16c1c75f 100644 --- a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLClient.kt +++ b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLClient.kt @@ -3,13 +3,13 @@ package xyz.block.ftl.gradle import com.squareup.wire.GrpcClient import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.SendChannel -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import okhttp3.OkHttpClient import okhttp3.Protocol import xyz.block.ftl.v1.ControllerServiceClient +import xyz.block.ftl.v1.GetSchemaRequest import xyz.block.ftl.v1.PullSchemaRequest import xyz.block.ftl.v1.PullSchemaResponse +import xyz.block.ftl.v1.schema.Schema import java.net.ConnectException import java.time.Duration @@ -41,33 +41,9 @@ class FTLClient(ftlEndpoint: String) { .build() } - fun pullSchemas(): List { + fun getSchema(): Schema? { val schemas = mutableListOf() - runBlocking { - launch { - grpcClient.create(ControllerServiceClient::class) - .PullSchema() - .executeIn(this) - .let { (sendChannel, receiveChannel) -> - sendSchemaChannel = sendChannel - receiveSchemaChannel = receiveChannel - } - - require(sendSchemaChannel.trySend(PullSchemaRequest()).isSuccess) - try { - for (schema in receiveSchemaChannel) { - schemas.add(schema) - if (!schema.more) { - receiveSchemaChannel.cancel() - return@launch - } - } - } catch (e: Exception) { - receiveSchemaChannel.cancel() - throw e - } - } - } - return schemas + val client = grpcClient.create(ControllerServiceClient::class) + return client.GetSchema().executeBlocking(GetSchemaRequest()).schema } } diff --git a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLPlugin.kt b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLPlugin.kt index 35f44c117f..75a454a19a 100644 --- a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLPlugin.kt +++ b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/FTLPlugin.kt @@ -25,12 +25,12 @@ class FTLPlugin : Plugin { extension.endpoint?.let { val client = FTLClient(it) - val schemas = client.pullSchemas() + val schema = client.getSchema() ?: throw RuntimeException("Failed to get schema") val outputDirectory = project.file(extension.outputDirectory) outputDirectory.deleteRecursively() outputDirectory.mkdir() extension.module.let { module -> - ModuleGenerator().run(schemas, outputDirectory, module) + ModuleGenerator().run(schema, outputDirectory, module) } } diff --git a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/ModuleGenerator.kt b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/ModuleGenerator.kt index ae88535970..ccbf5d6a64 100644 --- a/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/ModuleGenerator.kt +++ b/kotlin-runtime/ftl-plugin/src/main/kotlin/xyz/block/ftl/gradle/ModuleGenerator.kt @@ -15,31 +15,32 @@ import org.gradle.configurationcache.extensions.capitalized import xyz.block.ftl.Context import xyz.block.ftl.Ignore import xyz.block.ftl.Ingress -import xyz.block.ftl.v1.PullSchemaResponse import xyz.block.ftl.v1.schema.Data +import xyz.block.ftl.v1.schema.Module +import xyz.block.ftl.v1.schema.Schema import xyz.block.ftl.v1.schema.Type import xyz.block.ftl.v1.schema.Verb import java.io.File class ModuleGenerator() { - fun run(schemas: List, outputDirectory: File, module: String) { - schemas.filter { it.module_name != module }.forEach { + fun run(schema: Schema, outputDirectory: File, module: String) { + schema.modules.filter { it.name != module }.forEach { val file = generateModule(it) file.writeTo(outputDirectory) println( - "Generated module: ${outputDirectory.absolutePath}/ftl/${it.module_name}/${file.name}.kt" + "Generated module: ${outputDirectory.absolutePath}/ftl/${it.name}/${file.name}.kt" ) } } - internal fun generateModule(schema: PullSchemaResponse): FileSpec { - val namespace = "ftl.${schema.module_name}" - val className = schema.module_name.capitalized() + internal fun generateModule(schema: Module): FileSpec { + val namespace = "ftl.${schema.name}" + val className = schema.name.capitalized() val file = FileSpec.builder(namespace, className) .addFileComment("Code generated by FTL-Plugin, do not edit.") - schema.schema?.comments?.let { + schema.comments?.let { file.addFileComment("\n") file.addFileComment(it.joinToString("\n")) } @@ -50,10 +51,10 @@ class ModuleGenerator() { FunSpec.constructorBuilder().build() ) - val types = schema.schema?.decls?.mapNotNull { it.data_ } ?: listOf() + val types = schema.decls?.mapNotNull { it.data_ } ?: listOf() types.forEach { file.addType(buildDataClass(it)) } - val verbs = schema.schema?.decls?.mapNotNull { it.verb } ?: listOf() + val verbs = schema.decls?.mapNotNull { it.verb } ?: listOf() verbs.forEach { moduleClass.addFunction(buildVerbFunction(className, it)) } file.addType(moduleClass.build())