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 (#356)

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 authored Sep 6, 2023
1 parent 2e0bc44 commit 6f279a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 49 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class ModuleGeneratorTest {

@Test
fun `should generate basic module`() {
val response = PullSchemaResponse(module_name = "test")
val file = generator.generateModule(response)
val file = generator.generateModule(Module(name = "test"))
val expected = """// Code generated by FTL-Plugin, do not edit.
//
package ftl.test
import xyz.block.ftl.Ignore
Expand Down Expand Up @@ -81,10 +81,9 @@ public class Test()
)
),
)
val module = Module(comments = listOf("Module comments"), decls = decls)
val module = Module(name = "test", comments = listOf("Module comments"), decls = decls)

val response = PullSchemaResponse(module_name = "test", schema = module)
val file = generator.generateModule(response)
val file = generator.generateModule(module)
val expected = """// Code generated by FTL-Plugin, do not edit.
// Module comments
package ftl.test
Expand Down Expand Up @@ -156,9 +155,8 @@ public class Test()
)
),
)
val module = Module(comments = listOf("Module comments"), decls = decls)
val response = PullSchemaResponse(module_name = "test", schema = module)
val file = generator.generateModule(response)
val module = Module(name = "test", comments = listOf("Module comments"), decls = decls)
val file = generator.generateModule(module)
val expected = """// Code generated by FTL-Plugin, do not edit.
// Module comments
package ftl.test
Expand Down

0 comments on commit 6f279a6

Please sign in to comment.