Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet committed Dec 15, 2024
1 parent 8dbd600 commit 86ffb8a
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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.KotlinTarget
import protokt.v1.codegen.util.Message
import protokt.v1.codegen.util.Oneof
import protokt.v1.codegen.util.StandardField
Expand Down Expand Up @@ -67,9 +66,7 @@ private class DeserializerGenerator(
.addFunction(
buildFunSpec("deserialize") {
addModifiers(KModifier.OVERRIDE)
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
}
addAnnotation(JvmStatic::class)
addParameter(READER, Reader::class)
returns(msg.className)
if (properties.isNotEmpty()) {
Expand Down Expand Up @@ -111,9 +108,7 @@ private class DeserializerGenerator(
)
.addFunction(
buildFunSpec("invoke") {
if (ctx.info.context.kotlinTarget != KotlinTarget.JS) {
addAnnotation(JvmStatic::class) // todo: remove when JS code is common in multiplatform
}
addAnnotation(JvmStatic::class)
addModifiers(KModifier.OPERATOR)
returns(msg.className)
addParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ 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 && ctx.info.context.kotlinTarget == KotlinTarget.COMMON) {
if (ctx.info.context.generateTypes && ctx.info.context.kotlinTarget.isPrimaryTarget) {
EnumGenerator(e, ctx).generate()
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private class FileGenerator(
builder.addType(it.typeSpec)
}

if (contents.info.context.generateDescriptors) {
if (contents.info.context.generateDescriptors && contents.info.context.kotlinTarget.isPrimaryTarget) {
val fileDescriptorInfo = FileDescriptorResolver.resolveFileDescriptor(contents)

if (fileDescriptorInfo != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ 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.generate.Nullability.nonNullPropName
import protokt.v1.codegen.util.KotlinTarget
import protokt.v1.codegen.util.Message

internal fun generateMessage(msg: Message, ctx: Context) =
if (ctx.info.context.generateTypes && ctx.info.context.kotlinTarget == KotlinTarget.COMMON) {
if (ctx.info.context.generateTypes && ctx.info.context.kotlinTarget.isPrimaryTarget) {
MessageGenerator(msg, ctx).generate()
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ import io.grpc.kotlin.ServerCalls
import io.grpc.kotlin.generator.protoc.ProtoMethodName
import kotlinx.coroutines.flow.Flow
import protokt.v1.codegen.generate.CodeGenerator.Context
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
import protokt.v1.gradle.KotlinTarget
import protokt.v1.grpc.KtMarshaller
import protokt.v1.grpc.SchemaDescriptor
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -121,7 +121,7 @@ private class ServiceGenerator(
}

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

private fun pivotClassName(jvmClass: KClass<*>) =
when (kotlinTarget) {
KotlinTarget.JS -> ClassName(KtMarshaller::class.java.`package`!!.name, jvmClass.asTypeName().simpleNames)
KotlinTarget.MultiplatformJs -> ClassName(KtMarshaller::class.java.`package`!!.name, jvmClass.asTypeName().simpleNames)
else -> jvmClass.asTypeName()
}

Expand All @@ -431,13 +431,13 @@ private class ServiceGenerator(

private fun <T> pivotPlugin(jvm: T, js: T) =
when (kotlinTarget) {
KotlinTarget.JS -> js
KotlinTarget.MultiplatformJs -> js
else -> jvm
}

private fun FunSpec.Builder.staticIfAppropriate() =
apply {
if (kotlinTarget != KotlinTarget.JS) {
if (kotlinTarget != KotlinTarget.MultiplatformJs) {
addAnnotation(JvmStatic::class)
}
}
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.kotlinTarget in setOf(KotlinTarget.JVM, KotlinTarget.ANDROID) &&
params.kotlinTarget.treatTargetAsJvm &&
params.generateGrpcKotlinStubs
) {
val out = ReadableByteArrayOutputStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ package protokt.v1.codegen.util

import com.google.common.base.CaseFormat
import com.squareup.kotlinpoet.asClassName
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
import protokt.v1.gradle.GENERATE_GRPC_KOTLIN_STUBS
import protokt.v1.gradle.GENERATE_TYPES
import protokt.v1.gradle.KOTLIN_EXTRA_CLASSPATH
import protokt.v1.gradle.KOTLIN_TARGET
import protokt.v1.gradle.KotlinTarget
import protokt.v1.gradle.ProtoktExtension
import protokt.v1.gradle.ProtoktExtension.Generate
import protokt.v1.reflect.ClassLookup
Expand All @@ -45,7 +46,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 kotlinTarget = params[KOTLIN_TARGET]?.toKotlinPluginEnum()
val kotlinTarget = KotlinTarget.fromString(params.getValue(KOTLIN_TARGET))
}

private inline fun <reified T> Map<String, String>.getOrDefault(key: String): Boolean {
Expand All @@ -71,19 +72,3 @@ private inline fun <reified T> Map<String, String>.getOrDefault(key: String): Bo
.call(default) as Boolean
}
}

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

enum class KotlinTarget {
COMMON,
JS,
JVM,
ANDROID
}
61 changes: 61 additions & 0 deletions shared-src/codegen/protokt/v1/gradle/PluginConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 Toast, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package protokt.v1.gradle

internal const val KOTLIN_EXTRA_CLASSPATH = "kotlin_extra_classpath"
internal const val GENERATE_TYPES = "generate_types"
internal const val GENERATE_DESCRIPTORS = "generate_descriptors"
internal const val GENERATE_GRPC_DESCRIPTORS = "generate_grpc_descriptors"
internal const val GENERATE_GRPC_KOTLIN_STUBS = "generate_grpc_kotlin_stubs"
internal const val FORMAT_OUTPUT = "format_output"
internal const val KOTLIN_TARGET = "kotlin_target"

private val namesByKotlinTarget =
mapOf(
KotlinTarget.Android to "android",
KotlinTarget.Jvm to "jvm",
KotlinTarget.MultiplatformAndroid to "android-mp",
KotlinTarget.MultiplatformCommon to "common-mp",
KotlinTarget.MultiplatformJs to "js-mp",
KotlinTarget.MultiplatformJvm to "jvm-mp"
)

private val kotlinTargetsByName =
namesByKotlinTarget.entries.associate { (k, v) -> v to k }

internal sealed class KotlinTarget(
val isPrimaryTarget: Boolean,
val treatTargetAsJvm: Boolean,
val pluginSuffix: String,
val name: String
) {
object MultiplatformCommon : KotlinTarget(true, false, "-common", "common")
object MultiplatformJs : KotlinTarget(false, false, "-js", "js")
object MultiplatformJvm : KotlinTarget(false, true, "-jvm", "jvm")
object MultiplatformAndroid : KotlinTarget(false, true, "-android", "android")
class MultiplatformOther(name: String) : KotlinTarget(false, false, "-$name", name)

object Jvm : KotlinTarget(true, true, "", "jvm")
object Android : KotlinTarget(true, true, "", "android")

override fun toString() =
namesByKotlinTarget[this] ?: name

companion object {
fun fromString(string: String) =
kotlinTargetsByName[string] ?: MultiplatformOther(string.removeSuffix("-mp"))
}
}
8 changes: 0 additions & 8 deletions shared-src/codegen/protokt/v1/gradle/ProtoktExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@

package protokt.v1.gradle

const val KOTLIN_EXTRA_CLASSPATH = "kotlin_extra_classpath"
const val GENERATE_TYPES = "generate_types"
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 KOTLIN_TARGET = "kotlin_target"

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

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

task.plugins {
id("protokt-$target") {
id("protokt" + target.pluginSuffix) {
project.afterEvaluate {
option("$KOTLIN_EXTRA_CLASSPATH=${extraClasspath(project, task)}")
option("$GENERATE_TYPES=${ext.generate.types}")
Expand Down
11 changes: 5 additions & 6 deletions shared-src/gradle-plugin/protokt/v1/gradle/ProtoktBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ import org.gradle.api.artifacts.Dependency
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.project
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
Expand Down Expand Up @@ -99,14 +97,15 @@ private fun Project.createExtensionConfigurationsAndConfigureProtobuf(
}

pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
configureProtobufPlugin(project, ext, disableJava, "common", resolveBinary())
configureProtobufPlugin(project, ext, disableJava, KotlinTarget.MultiplatformCommon, resolveBinary())
linkGenerateProtoToSourceCompileForKotlinMpp("commonMain", "commonTest")

@Suppress("DEPRECATION")
kotlinExtension
.targets
.filterNot { it.targetName == "metadata" }
.forEach {
configureProtobufPlugin(project, ext, disableJava, it.targetName, resolveBinary())
configureProtobufPlugin(project, ext, disableJava, KotlinTarget.fromString("${it.targetName}-mp"), resolveBinary())
configureProtoktConfigurations(KotlinMultiplatformExtension::class, "${it.targetName}Main", "${it.targetName}Test")
linkGenerateProtoToSourceCompileForKotlinMpp("${it.targetName}Main", "${it.targetName}Test")
}
Expand All @@ -118,12 +117,12 @@ private fun Project.createExtensionConfigurationsAndConfigureProtobuf(
}

pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
configureProtobufPlugin(project, ext, disableJava, "jvm", resolveBinary())
configureProtobufPlugin(project, ext, disableJava, KotlinTarget.Jvm, resolveBinary())
otherwise()
}

pluginManager.withPlugin("org.jetbrains.kotlin.android") {
configureProtobufPlugin(project, ext, disableJava, "jvm", resolveBinary())
configureProtobufPlugin(project, ext, disableJava, KotlinTarget.Android, resolveBinary())
otherwise()
}
}
Expand Down

0 comments on commit 86ffb8a

Please sign in to comment.