Skip to content

Commit

Permalink
fix(kt): use GetSchema() rather than PullSchema() as the latter b…
Browse files Browse the repository at this point in the history
…locks

If there is no schema available, `PullSchema()` will block until one is
available. This times out the Gradle build.

Fixes #354
  • Loading branch information
alecthomas committed Sep 6, 2023
1 parent 2e0bc44 commit 24cb72e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,33 +41,9 @@ class FTLClient(ftlEndpoint: String) {
.build()
}

fun pullSchemas(): List<PullSchemaResponse> {
fun getSchema(): Schema? {
val schemas = mutableListOf<PullSchemaResponse>()
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class FTLPlugin : Plugin<Project> {

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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PullSchemaResponse>, 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"))
}
Expand All @@ -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())
Expand Down

0 comments on commit 24cb72e

Please sign in to comment.