Skip to content

protoc-plugin: Refactoring of intermediate model #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/BufExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
package kotlinx.rpc.buf

import kotlinx.rpc.buf.tasks.BufExecTask
import kotlinx.rpc.proto.ProtocPlugin
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.property
import java.io.File
import javax.inject.Inject
import kotlin.time.Duration
import kotlinx.rpc.proto.ProtocPlugin
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import java.io.File
import javax.inject.Inject
import kotlin.reflect.KClass
import kotlin.time.Duration

/**
* Options for the Buf tasks.
Expand Down Expand Up @@ -100,14 +100,14 @@ public open class BufExtension @Inject constructor(objects: ObjectFactory) {
* Allows registering custom Buf tasks that can operate on the generated workspace.
*/
public open class BufTasksExtension @Inject constructor(internal val project: Project) {
// TODO change to commonMain/commonTest in docs when it's supported KRPC-180

/**
* Registers a custom Buf task that operates on the generated workspace.
*
* Name conventions:
* `lint` input for [name] will result in tasks
* named 'bufLintMain' and 'bufLintTest' for Kotlin/JVM projects
* and 'bufLintJvmMain' and 'bufLintJvmTest' for Kotlin/Multiplatform projects.
* and 'bufLintCommonMain' and 'bufLintCommonTest' for Kotlin/Multiplatform projects.
*
* Note the by default 'test' task doesn't depend on 'main' task.
*/
Expand All @@ -127,14 +127,14 @@ public open class BufTasksExtension @Inject constructor(internal val project: Pr
return provider
}

// TODO change to commonMain/commonTest in docs when it's supported KRPC-180

/**
* Registers a custom Buf task that operates on the generated workspace.
*
* Name conventions:
* `lint` input for [name] will result in tasks
* named 'bufLintMain' and 'bufLintTest' for Kotlin/JVM projects
* and 'bufLintJvmMain' and 'bufLintJvmTest' for Kotlin/Multiplatform projects.
* and 'bufLintCommonMain' and 'bufLintCommonTest' for Kotlin/Multiplatform projects.
*
* Note the by default 'test' task doesn't depend on 'main' task.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@

package kotlinx.rpc.grpc

import kotlinx.rpc.GRPC_KOTLIN_VERSION
import kotlinx.rpc.GRPC_VERSION
import kotlinx.rpc.PROTOBUF_VERSION
import kotlinx.rpc.buf.BufExtension
import kotlinx.rpc.buf.configureBufExecutable
import kotlinx.rpc.buf.tasks.registerBufExecTask
import kotlinx.rpc.buf.tasks.registerBufGenerateTask
import kotlinx.rpc.buf.tasks.registerGenerateBufGenYamlTask
import kotlinx.rpc.buf.tasks.registerGenerateBufYamlTask
import kotlinx.rpc.proto.*
import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_JAVA
import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_KOTLIN
import kotlinx.rpc.proto.ProtocPlugin.Companion.KOTLIN_MULTIPLATFORM
import kotlinx.rpc.proto.ProtocPlugin.Companion.PROTOBUF_JAVA
import kotlinx.rpc.util.ensureDirectoryExists
import org.gradle.api.Action
import org.gradle.api.GradleException
Expand Down Expand Up @@ -61,9 +55,6 @@ internal open class DefaultGrpcExtension @Inject constructor(
createDefaultProtocPlugins()

project.protoSourceSets.forEach { protoSourceSet ->
protoSourceSet.protocPlugin(protocPlugins.protobufJava)
protoSourceSet.protocPlugin(protocPlugins.grpcJava)
protoSourceSet.protocPlugin(protocPlugins.grpcKotlin)
protoSourceSet.protocPlugin(protocPlugins.kotlinMultiplatform)
}

Expand Down Expand Up @@ -294,30 +285,6 @@ internal open class DefaultGrpcExtension @Inject constructor(
// ignore for bufGenerate task caching
project.normalization.runtimeClasspath.ignore("**/protoc-gen-kotlin-multiplatform.log")
project.normalization.runtimeClasspath.ignore("**/.keep")

protocPlugins.create(GRPC_JAVA) {
isJava.set(true)

remote {
locator.set("buf.build/grpc/java:v$GRPC_VERSION")
}
}

protocPlugins.create(GRPC_KOTLIN) {
remote {
locator.set("buf.build/grpc/kotlin:v$GRPC_KOTLIN_VERSION")
}
}

protocPlugins.create(PROTOBUF_JAVA) {
isJava.set(true)

remote {
// for some reason they omit the first digit in this version:
// https://buf.build/protocolbuffers/java?version=v31.1
locator.set("buf.build/protocolbuffers/java:v${PROTOBUF_VERSION.substringAfter(".")}")
}
}
}

private fun DefaultProtoSourceSet.correspondingMainSourceSetOrNull(): DefaultProtoSourceSet? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ internal fun Project.createProtoExtensions() {
}

project.withKotlinKmpExtension {
findOrCreateAndConfigure("jvmMain", null)
findOrCreateAndConfigure("jvmTest", null)
findOrCreateAndConfigure("commonMain", null)
findOrCreateAndConfigure("commonTest", null)

sourceSets.configureEach {
if (name == "jvmMain" || name == "jvmTest" || name == "commonMain" || name == "commonTest") {
if (name == "commonMain" || name == "commonTest") {
findOrCreateAndConfigure(name, this)
}
}
Expand Down
63 changes: 0 additions & 63 deletions gradle-plugin/src/main/kotlin/kotlinx/rpc/proto/ProtocPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

package kotlinx.rpc.proto

import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_JAVA
import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_KOTLIN
import kotlinx.rpc.proto.ProtocPlugin.Companion.KOTLIN_MULTIPLATFORM
import kotlinx.rpc.proto.ProtocPlugin.Companion.PROTOBUF_JAVA
import kotlinx.rpc.util.OS
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
Expand All @@ -34,45 +31,6 @@ public fun NamedDomainObjectContainer<ProtocPlugin>.kotlinMultiplatform(action:
kotlinMultiplatform.configure(action)
}

/**
* Access to the `protobuf-java` protoc plugin.
*/
public val NamedDomainObjectContainer<ProtocPlugin>.protobufJava: NamedDomainObjectProvider<ProtocPlugin>
get() = named(PROTOBUF_JAVA)

/**
* Configures the `protobuf-java` protoc plugin.
*/
public fun NamedDomainObjectContainer<ProtocPlugin>.protobufJava(action: Action<ProtocPlugin>) {
protobufJava.configure(action)
}

/**
* Access to the `grpc-java` protoc plugin.
*/
public val NamedDomainObjectContainer<ProtocPlugin>.grpcJava: NamedDomainObjectProvider<ProtocPlugin>
get() = named(GRPC_JAVA)

/**
* Configures the grpc-java protoc plugin.
*/
public fun NamedDomainObjectContainer<ProtocPlugin>.grpcJava(action: Action<ProtocPlugin>) {
grpcJava.configure(action)
}

/**
* Access to the `grpc-kotlin` protoc plugin.
*/
public val NamedDomainObjectContainer<ProtocPlugin>.grpcKotlin: NamedDomainObjectProvider<ProtocPlugin>
get() = named(GRPC_KOTLIN)

/**
* Configures the `grpc-kotlin` protoc plugin.
*/
public fun NamedDomainObjectContainer<ProtocPlugin>.grpcKotlin(action: Action<ProtocPlugin>) {
grpcKotlin.configure(action)
}

/**
* Access to a specific protoc plugin.
*/
Expand Down Expand Up @@ -193,27 +151,6 @@ public open class ProtocPlugin(
* @see [kotlinMultiplatform]
*/
public const val KOTLIN_MULTIPLATFORM: String = "kotlin-multiplatform"

/**
* The name of the `protobuf-java` protoc plugin.
*
* @see [protobufJava]
*/
public const val PROTOBUF_JAVA: String = "java"

/**
* The name of the `grpc-java` protoc plugin.
*
* @see [grpcJava]
*/
public const val GRPC_JAVA: String = "grpc-java"

/**
* The name of the `grpc-kotlin` protoc plugin.
*
* @see [grpcKotlin]
*/
public const val GRPC_KOTLIN: String = "grpc-kotlin"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ class GrpcJvmProjectTest : GrpcBaseTest() {
version: v2
clean: true
plugins:
- remote: buf.build/protocolbuffers/java:v${TEST_PROTOBUF_VERSION.substringAfter(".")}
out: java
- remote: buf.build/grpc/java:v$TEST_GRPC_VERSION
out: grpc-java
- remote: buf.build/grpc/kotlin:v$TEST_GRPC_KOTLIN_VERSION
out: grpc-kotlin
- local: [protoc-gen-kotlin-multiplatform]
out: kotlin-multiplatform
opt:
Expand Down Expand Up @@ -174,7 +168,8 @@ inputs:
}

@Suppress("detekt.MaxLineLength")
private val cliFlagsRegex = "- Buf Arguments: \\[.*?, generate, --output, .*?, --include-imports, --include-wkt, --error-format, json, --config, some\\.buf\\.yaml, --log-format, json, --timeout, 60s]".toRegex()
private val cliFlagsRegex =
"- Buf Arguments: \\[.*?, generate, --output, .*?, --include-imports, --include-wkt, --error-format, json, --config, some\\.buf\\.yaml, --log-format, json, --timeout, 60s]".toRegex()

@Test
fun `Custom Buf CLI Flags`() = runGrpcTest {
Expand Down
14 changes: 2 additions & 12 deletions gradle-plugin/src/test/kotlin/kotlinx/rpc/base/GrpcBaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ import org.gradle.testkit.runner.TaskOutcome
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.TestInstance
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.absolutePathString
import kotlin.io.path.deleteRecursively
import kotlin.io.path.exists
import kotlin.io.path.extension
import kotlin.io.path.isRegularFile
import kotlin.io.path.pathString
import kotlin.io.path.readLines
import kotlin.io.path.relativeTo
import kotlin.io.path.walk
import kotlin.lazy
import kotlin.io.path.*
import kotlin.test.assertEquals
import kotlin.test.fail

Expand Down Expand Up @@ -229,7 +219,7 @@ abstract class GrpcBaseTest : BaseTest() {
}

companion object {
private const val KMP_SOURCE_SET = "jvm"
private const val KMP_SOURCE_SET = "common"
private val KMP_SOURCE_SET_CAPITAL = KMP_SOURCE_SET.replaceFirstChar(Char::uppercaseChar)
private const val KOTLIN_MULTIPLATFORM_DIR = "kotlin-multiplatform"
const val RPC_INTERNAL = "_rpc_internal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ plugins {

// should nonetheless be available
protoSourceSets {
jvmMain {}
jvmTest {}
commonMain {}
commonTest {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ kotlin {

// should nonetheless be available
protoSourceSets {
jvmMain {}
jvmTest {}
commonMain {}
commonTest {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ kotlin {
}

protoSourceSets {
jvmMain {}
jvmTest {}
commonMain {}
commonTest {}
}

rpc {
Expand Down
15 changes: 0 additions & 15 deletions grpc/grpc-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ kotlin {
}

protoSourceSets {
jvmTest {
proto {
exclude("exclude/**")
}
}

commonTest {
proto {
exclude("exclude/**")
Expand All @@ -158,15 +152,6 @@ rpc {
}

project.tasks.withType<BufGenerateTask>().configureEach {

// TODO: Remove this once we remove JVM generation
// Set compile for common(native) option
if (name.endsWith("CommonTest")) {
protocPlugins.kotlinMultiplatform {
options.put("targetMode", "common")
}
}

if (name.endsWith("Test")) {
dependsOn(gradle.includedBuild("protoc-gen").task(":jar"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.grpc.core.test

//import kotlinx.coroutines.sync.Mutex
//import kotlinx.coroutines.sync.withLock
//import kotlinx.coroutines.test.runTest
//import kotlinx.rpc.RpcServer
//import kotlinx.rpc.grpc.GrpcClient
//import kotlinx.rpc.grpc.GrpcServer
//
//abstract class GrpcServerTest {
// private val serverMutex = Mutex()
//
// abstract fun RpcServer.registerServices()
//
// protected fun runGrpcTest(test: suspend (GrpcClient) -> Unit, ) = runTest {
// serverMutex.withLock {
// val grpcClient = GrpcClient("localhost", 8080) {
// usePlaintext()
// }
//
// val grpcServer = GrpcServer(8080, builder = {
// registerServices()
// })
//
// grpcServer.start()
// test(grpcClient)
// grpcServer.shutdown()
// grpcServer.awaitTermination()
// grpcClient.shutdown()
// grpcClient.awaitTermination()
// }
// }
//}
Loading
Loading