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

Split up codegen library #465

Merged
merged 1 commit into from
Mar 12, 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
15 changes: 12 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ lazy val commonSettings = List(
)

lazy val codegen = (project in file("codegen"))
.settings(
commonSettings,
name := "twinagle-codegen",
crossScalaVersions := Seq(scala212, scala213, scala3LTS),
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % scalapb.compiler.Version.scalapbVersion,
publishLocal := publishLocal.dependsOn(runtime / publishLocal).value,
)

lazy val plugin = (project in file("plugin"))
.enablePlugins(SbtPlugin, BuildInfoPlugin)
.dependsOn(codegen)
.settings(
commonSettings,
name := "twinagle-scalapb-plugin",
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.7"),
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % scalapb.compiler.Version.scalapbVersion,
buildInfoKeys := Seq[BuildInfoKey](version, scalaBinaryVersion),
buildInfoPackage := "com.soundcloud.twinagle.codegen",
buildInfoPackage := "com.soundcloud.twinagle.plugin",
buildInfoUsePackageAsPath := true,
publishLocal := publishLocal.dependsOn(runtime / publishLocal).value,
scriptedLaunchOpts ++= Seq("-Xmx1024M", "-Dplugin.version=" + version.value),
Expand Down Expand Up @@ -77,7 +86,7 @@ lazy val runtime = (project in file("runtime")).settings(
)

lazy val root = (project in file("."))
.aggregate(runtime, codegen)
.aggregate(runtime, codegen, plugin)
.settings(
name := "twinagle-root",
resolvers += Resolver.typesafeIvyRepo("releases"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,16 @@ import com.google.protobuf.Descriptors._
import com.google.protobuf.ExtensionRegistry
import com.google.protobuf.compiler.PluginProtos
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse
import protocbridge.Artifact
import protocgen.{CodeGenApp, CodeGenRequest, CodeGenResponse}
import scalapb.compiler.{DescriptorImplicits, FunctionalPrinter, ProtobufGenerator}

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

object ServerClientCodeGenerator extends CodeGenApp {
trait ServerClientCodeGenerator extends CodeGenApp {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the naming could be simplified, since the package location already defines much of the semantics.

I'd suggest to name/move this to com.soundcloud.twinagle.CodeGenerator. If it's a class instead of a trait it could be used without the need of an special Standalone* object.

Consequently, the sbt extension could then be moved to com.soundcloud.twinagle.plugin.CodeGenerator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CodeGenerator defines the main method though (via inheriting the CodeGenApp trait). Doesn't it need to be on a Scala object for it to be statically callable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the trait need to extend CodeGenApp? I wonder if you could have something along the lines of

trait ServerClientCodeGenerator {
  // ...lots of definitions
}

object StandaloneCodeGen extends CodeGenApp with ServerClientCodeGenerator {}
object SbtCodeGen extends CodeGenApp with ServerClientCodeGenerator {
  override def suggestedDependencies = ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work unfortunately, because I need the override def for registerExtensions. When I try that, I get:

[error] object StandaloneServerClientCodeGenerator inherits conflicting members:
[error]   method registerExtensions in trait CodeGenApp of type (registry: com.google.protobuf.ExtensionRegistry)Unit  an
[error]   method registerExtensions in trait ServerClientCodeGenerator of type (registry: com.google.protobuf.ExtensionRegistry)Unit
[error] (Note: this can be resolved by declaring an override in object StandaloneServerClientCodeGenerator.)
[error] object StandaloneServerClientCodeGenerator extends CodeGenApp with ServerClientCodeGenerator {}
[error]        ^
[error] one error found

override def registerExtensions(registry: ExtensionRegistry): Unit = {
scalapb.options.Scalapb.registerAllExtensions(registry)
}

override def suggestedDependencies: Seq[protocbridge.Artifact] = Seq(
Artifact(
"com.soundcloud",
"twinagle-runtime",
BuildInfo.version,
crossVersion = true
),
Artifact(
"com.thesamet.scalapb",
"scalapb-runtime",
scalapb.compiler.Version.scalapbVersion,
crossVersion = true
)
)

def process(request: CodeGenRequest): CodeGenResponse =
ProtobufGenerator.parseParameters(request.parameter) match {
case Right(params) =>
Expand All @@ -54,7 +38,7 @@ object ServerClientCodeGenerator extends CodeGenApp {
file: FileDescriptor,
di: DescriptorImplicits
): Seq[PluginProtos.CodeGeneratorResponse.File] = {
file.getServices.asScala.map { service =>
file.getServices.asScala.toSeq.map { service =>
val p = new TwinagleServicePrinter(service, di)

import di.{ExtendedFileDescriptor, ExtendedServiceDescriptor}
Expand All @@ -68,3 +52,5 @@ object ServerClientCodeGenerator extends CodeGenApp {
}

}

object StandaloneServerClientCodeGenerator extends ServerClientCodeGenerator {}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.soundcloud.twinagle.plugin

import com.soundcloud.twinagle.codegen.ServerClientCodeGenerator
import protocbridge.Artifact

object SbtServerClientCodeGenerator extends ServerClientCodeGenerator {

override def suggestedDependencies: Seq[Artifact] = Seq(
Artifact(
"com.soundcloud",
"twinagle-runtime",
BuildInfo.version,
crossVersion = true
),
Artifact(
"com.thesamet.scalapb",
"scalapb-runtime",
scalapb.compiler.Version.scalapbVersion,
crossVersion = true
)
)

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soundcloud.twinagle.codegen
package com.soundcloud.twinagle.plugin

import protocbridge.{JvmGenerator, Target}
import sbt._
Expand Down Expand Up @@ -33,7 +33,7 @@ object Twinagle extends AutoPlugin {
(Compile / sourceManaged).value / "twinagle-protobuf"
),
Target(
JvmGenerator("scala-twinagle", ServerClientCodeGenerator),
JvmGenerator("scala-twinagle", SbtServerClientCodeGenerator),
(Compile / sourceManaged).value / "twinagle-services",
scalapb.gen(scalapbCodeGeneratorOptions.value)._2
)
Expand Down