Skip to content
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

Remove support for the Kotlin JS plugin #271

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
37 changes: 18 additions & 19 deletions examples/grpc-node/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
* limitations under the License.
*/

import com.google.protobuf.gradle.proto
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
import protokt.v1.gradle.protokt

plugins {
id("org.jetbrains.kotlin.js")
id("org.jetbrains.kotlin.multiplatform")
}

kotlin {
Expand All @@ -31,6 +30,23 @@ kotlin {
binaries.executable()
useCommonJs()
}

sourceSets {
val jsMain by getting {
dependencies {
api(project(":protokt-runtime-grpc-lite"))
api(project(":examples:protos"))
api(libs.kotlinx.coroutines.core)
}
}

val jsTest by getting {
dependencies {
api(kotlin("test"))
api(libs.kotlinx.coroutines.test)
}
}
}
}

localProtokt()
Expand All @@ -44,23 +60,6 @@ protokt {
}
}

dependencies {
implementation(project(":protokt-runtime-grpc-lite"))
implementation(project(":examples:protos"))
implementation(libs.kotlinx.coroutines.core)

testImplementation(kotlin("test"))
testImplementation(libs.kotlinx.coroutines.test)
}

sourceSets {
named("jsMain") {
proto {
srcDir("../protos/src/main/proto")
}
}
}

tasks.withType<NodeJsExec> {
args(listOfNotNull(properties["service"], properties["mode"], properties["args"]))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal object CodeGenerator {
generateService(
type,
ctx,
ctx.info.context.appliedKotlinPlugin
ctx.info.context.kotlinTarget
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import protokt.v1.UnknownFieldSet
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.generate.Wrapper.interceptRead
import protokt.v1.codegen.generate.Wrapper.wrapField
import protokt.v1.codegen.util.KotlinPlugin
import protokt.v1.codegen.util.KotlinTarget
import protokt.v1.codegen.util.Message
import protokt.v1.codegen.util.Oneof
import protokt.v1.codegen.util.StandardField
Expand Down Expand Up @@ -67,7 +67,7 @@ private class DeserializerGenerator(
.addFunction(
buildFunSpec("deserialize") {
addModifiers(KModifier.OVERRIDE)
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
if (ctx.info.context.kotlinTarget != KotlinTarget.JS) {
addAnnotation(JvmStatic::class) // can't put this here generally until JS code is actually common code in a multiplatform module
}
addParameter(READER, Reader::class)
Expand Down Expand Up @@ -111,7 +111,7 @@ private class DeserializerGenerator(
)
.addFunction(
buildFunSpec("invoke") {
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
if (ctx.info.context.kotlinTarget != KotlinTarget.JS) {
addAnnotation(JvmStatic::class) // todo: remove when JS code is common in multiplatform
}
addModifiers(KModifier.OPERATOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import protokt.v1.EnumReader
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.generate.Deprecation.handleDeprecation
import protokt.v1.codegen.util.Enum
import protokt.v1.codegen.util.KotlinTarget

internal fun generateEnum(e: Enum, ctx: Context) =
if (ctx.info.context.generateTypes) {
if (ctx.info.context.generateTypes && ctx.info.context.kotlinTarget == KotlinTarget.COMMON) {
EnumGenerator(e, ctx).generate()
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.generate.CodeGenerator.generate
import protokt.v1.codegen.generate.Deprecation.handleDeprecation
import protokt.v1.codegen.generate.Implements.handleSuperInterface
import protokt.v1.codegen.util.KotlinTarget
import protokt.v1.codegen.util.Message

internal fun generateMessage(msg: Message, ctx: Context) =
if (ctx.info.context.generateTypes) {
if (ctx.info.context.generateTypes && ctx.info.context.kotlinTarget == KotlinTarget.COMMON) {
MessageGenerator(msg, ctx).generate()
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import io.grpc.kotlin.ClientCalls
import io.grpc.kotlin.ServerCalls
import kotlinx.coroutines.flow.Flow
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.util.KotlinPlugin
import protokt.v1.codegen.util.KotlinTarget
import protokt.v1.codegen.util.Method
import protokt.v1.codegen.util.PROTOKT_V1_GOOGLE_PROTO
import protokt.v1.codegen.util.Service
Expand All @@ -57,13 +57,13 @@ import kotlin.reflect.KClass
import kotlin.reflect.KFunction1
import kotlin.reflect.KFunction3

internal fun generateService(s: Service, ctx: Context, kotlinPlugin: KotlinPlugin?) =
ServiceGenerator(s, ctx, kotlinPlugin).generate()
internal fun generateService(s: Service, ctx: Context, kotlinTarget: KotlinTarget?) =
ServiceGenerator(s, ctx, kotlinTarget).generate()

private class ServiceGenerator(
private val s: Service,
private val ctx: Context,
private val kotlinPlugin: KotlinPlugin?
private val kotlinTarget: KotlinTarget?
) {
fun generate(): List<TypeSpec> =
(grpcImplementations() + serviceDescriptor()).filterNotNull()
Expand Down Expand Up @@ -106,7 +106,7 @@ private class ServiceGenerator(
}

val grpcKtObject =
if (kotlinPlugin == KotlinPlugin.JS && ctx.info.context.generateGrpcKotlinStubs) {
if (kotlinTarget == KotlinTarget.JS && ctx.info.context.generateGrpcKotlinStubs) {
val grpcKtClassName = ClassName(ctx.info.kotlinPackage, s.name + "GrpcKt")
TypeSpec.objectBuilder(grpcKtClassName)
.addType(
Expand Down Expand Up @@ -404,9 +404,8 @@ private class ServiceGenerator(
}

private fun pivotClassName(jvmClass: KClass<*>) =
when (kotlinPlugin) {
KotlinPlugin.JS -> ClassName(KtMarshaller::class.java.`package`!!.name, jvmClass.asTypeName().simpleNames)
KotlinPlugin.MULTIPLATFORM -> error("unsupported plugin for service generation: $kotlinPlugin")
when (kotlinTarget) {
KotlinTarget.JS -> ClassName(KtMarshaller::class.java.`package`!!.name, jvmClass.asTypeName().simpleNames)
else -> jvmClass.asTypeName()
}

Expand All @@ -416,15 +415,14 @@ private class ServiceGenerator(
}

private fun <T> pivotPlugin(jvm: T, js: T) =
when (kotlinPlugin) {
KotlinPlugin.JS -> js
KotlinPlugin.MULTIPLATFORM -> error("unsupported plugin for service generation: $kotlinPlugin")
when (kotlinTarget) {
KotlinTarget.JS -> js
else -> jvm
}

private fun FunSpec.Builder.staticIfAppropriate() =
apply {
if (kotlinPlugin != KotlinPlugin.JS) {
if (kotlinTarget != KotlinTarget.JS) {
addAnnotation(JvmStatic::class)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class GeneratorContext(
val generateGrpcDescriptors = params.generateGrpcDescriptors
val generateGrpcKotlinStubs = params.generateGrpcKotlinStubs
val formatOutput = params.formatOutput
val appliedKotlinPlugin = params.appliedKotlinPlugin
val kotlinTarget = params.kotlinTarget

val protoktVersion = PROTOKT_VERSION

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal fun generateGrpcKotlinStubs(
request: CodeGeneratorRequest
): List<CodeGeneratorResponse.File> =
if (
params.appliedKotlinPlugin in setOf(KotlinPlugin.JVM, KotlinPlugin.ANDROID) &&
params.kotlinTarget in setOf(KotlinTarget.JVM, KotlinTarget.ANDROID) &&
params.generateGrpcKotlinStubs
) {
val out = ReadableByteArrayOutputStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package protokt.v1.codegen.util

import com.google.common.base.CaseFormat
import com.squareup.kotlinpoet.asClassName
import protokt.v1.gradle.APPLIED_KOTLIN_PLUGIN
import protokt.v1.gradle.KOTLIN_TARGET
import protokt.v1.gradle.FORMAT_OUTPUT
import protokt.v1.gradle.GENERATE_DESCRIPTORS
import protokt.v1.gradle.GENERATE_GRPC_DESCRIPTORS
Expand Down Expand Up @@ -45,7 +45,7 @@ internal class PluginParams(
val generateGrpcDescriptors = params.getOrDefault<Generate>(GENERATE_GRPC_DESCRIPTORS)
val generateGrpcKotlinStubs = params.getOrDefault<Generate>(GENERATE_GRPC_KOTLIN_STUBS)
val formatOutput = params.getOrDefault<ProtoktExtension>(FORMAT_OUTPUT)
val appliedKotlinPlugin = params[APPLIED_KOTLIN_PLUGIN]?.toKotlinPluginEnum()
val kotlinTarget = params[KOTLIN_TARGET]?.toKotlinPluginEnum()
}

private inline fun <reified T> Map<String, String>.getOrDefault(key: String): Boolean {
Expand Down Expand Up @@ -74,15 +74,15 @@ private inline fun <reified T> Map<String, String>.getOrDefault(key: String): Bo

private fun String.toKotlinPluginEnum() =
when (this) {
"org.jetbrains.kotlin.multiplatform" -> KotlinPlugin.MULTIPLATFORM
"org.jetbrains.kotlin.js" -> KotlinPlugin.JS
"org.jetbrains.kotlin.jvm" -> KotlinPlugin.JVM
"org.jetbrains.kotlin.android" -> KotlinPlugin.ANDROID
"common" -> KotlinTarget.COMMON
"js" -> KotlinTarget.JS
"jvm" -> KotlinTarget.JVM
"android" -> KotlinTarget.ANDROID
else -> null
}

enum class KotlinPlugin {
MULTIPLATFORM,
enum class KotlinTarget {
COMMON,
JS,
JVM,
ANDROID
Expand Down
2 changes: 1 addition & 1 deletion shared-src/codegen/protokt/v1/gradle/ProtoktExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const val GENERATE_DESCRIPTORS = "generate_descriptors"
const val GENERATE_GRPC_DESCRIPTORS = "generate_grpc_descriptors"
const val GENERATE_GRPC_KOTLIN_STUBS = "generate_grpc_kotlin_stubs"
const val FORMAT_OUTPUT = "format_output"
const val APPLIED_KOTLIN_PLUGIN = "applied_kotlin_plugin"
const val KOTLIN_TARGET = "kotlin_target"

open class ProtoktExtension {
/**
Expand Down
7 changes: 4 additions & 3 deletions shared-src/gradle-plugin/protokt/v1/gradle/ProtobufBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal fun configureProtobufPlugin(
project: Project,
ext: ProtoktExtension,
disableJava: Boolean,
target: String,
binaryPath: String
) {
project.apply<ProtobufPlugin>()
Expand All @@ -46,7 +47,7 @@ internal fun configureProtobufPlugin(
}

plugins {
id("protokt") {
id("protokt-$target") {
path = normalizePath(binaryPath)
}
}
Expand All @@ -60,15 +61,15 @@ internal fun configureProtobufPlugin(
}

task.plugins {
id("protokt") {
id("protokt-$target") {
project.afterEvaluate {
option("$KOTLIN_EXTRA_CLASSPATH=${extraClasspath(project, task)}")
option("$GENERATE_TYPES=${ext.generate.types}")
option("$GENERATE_DESCRIPTORS=${ext.generate.descriptors}")
option("$GENERATE_GRPC_DESCRIPTORS=${ext.generate.grpcDescriptors}")
option("$GENERATE_GRPC_KOTLIN_STUBS=${ext.generate.grpcKotlinStubs}")
option("$FORMAT_OUTPUT=${ext.formatOutput}")
option("$APPLIED_KOTLIN_PLUGIN=${project.appliedKotlinPlugin()}")
option("$KOTLIN_TARGET=$target")
}
}
}
Expand Down
Loading
Loading