Skip to content

Commit

Permalink
Move KSP into ftl-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Sep 5, 2023
1 parent b6369b0 commit 468edb8
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 147 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.git/
.idea/
.gradle/
.hermit/
.vscode/
.idea/
Expand Down
4 changes: 2 additions & 2 deletions examples/echo-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ repositories {

dependencies {
implementation("xyz.block.ftl:ftl-runtime")
implementation("xyz.block.ftl:ftl-schema")
ksp("xyz.block.ftl:ftl-schema")
implementation("com.google.devtools.ksp:symbol-processing-api:1.9.0-1.0.11")
ksp(project("xyz.block.ftl:ftl-plugin"))
}

ftl {
Expand Down
1 change: 1 addition & 0 deletions examples/echo-kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
kotlin.code.style=official
org.gradle.caching=true
7 changes: 2 additions & 5 deletions examples/echo-kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
plugins {}

rootProject.name = "echo"

includeBuild("../../kotlin-runtime/ftl-runtime") {
Expand All @@ -9,9 +7,8 @@ includeBuild("../../kotlin-runtime/ftl-runtime") {
}

includeBuild("../../kotlin-runtime/ftl-plugin")

includeBuild("../../kotlin-runtime/ftl-schema") {
includeBuild("../../kotlin-runtime/ftl-plugin") {
dependencySubstitution {
substitute(module("xyz.block.ftl:ftl-schema")).using(project(":"))
substitute(module("xyz.block.ftl:ftl-plugin")).using(project(":"))
}
}
3 changes: 3 additions & 0 deletions kotlin-runtime/ftl-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildscript {
dependencies {
classpath(kotlin("gradle-plugin", version = "1.9.0"))
classpath("com.squareup.wire:wire-gradle-plugin:4.7.2")
}
}
Expand All @@ -8,6 +9,7 @@ plugins {
kotlin("jvm") version "1.9.0"
id("java-gradle-plugin")
id("com.squareup.wire") version "4.7.2"
id("com.google.devtools.ksp") version "1.9.0-1.0.11"
}

repositories {
Expand All @@ -31,6 +33,7 @@ gradlePlugin {

dependencies {
compileOnly(gradleApi())
implementation(libs.kspApi)
implementation(project(":ftl-runtime"))

// Use the Kotlin JUnit 5 integration.
Expand Down
File renamed without changes.
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
42 changes: 0 additions & 42 deletions kotlin-runtime/ftl-schema/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion kotlin-runtime/ftl-schema/bin

This file was deleted.

41 changes: 0 additions & 41 deletions kotlin-runtime/ftl-schema/build.gradle.kts

This file was deleted.

15 changes: 0 additions & 15 deletions kotlin-runtime/ftl-schema/settings.gradle.kts

This file was deleted.

1 change: 1 addition & 0 deletions kotlin-runtime/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ grpcNetty = { module = "io.grpc:grpc-netty", version.ref = "grpc" }
grpcProtobuf = { module = "io.grpc:grpc-protobuf", version.ref = "grpc" }
grpcStub = { module = "io.grpc:grpc-stub", version.ref = "grpc" }
kotlinGradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" }
kspApi = { module = "com.google.devtools.ksp:symbol-processing-api", version = "1.9.0-1.0.11" }

[plugins]
wire = { id = "com.squareup.wire", version.ref = "wire" }

0 comments on commit 468edb8

Please sign in to comment.