From 53f91e8efb0416831483c34ad9ee9c2cccc11d8d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 23 Sep 2023 10:25:03 +0100 Subject: [PATCH] support scala 2.13 and scala 3 build for codegen lib (#181) * support scala 2.13 and scala 3 build for codegen lib * Update build.sbt * rework build --- .github/workflows/build-test.yml | 7 +-- .github/workflows/publish-nightly.yml | 2 + build.sbt | 10 +++- .../apache/pekko/grpc/gen/CodeGenerator.scala | 0 .../apache/pekko/grpc/gen/CodeGenerator.scala | 48 +++++++++++++++++++ .../templates/ScalaClient/Client.scala.txt | 2 +- project/Dependencies.scala | 1 + project/plugins.sbt | 2 +- 8 files changed, 66 insertions(+), 6 deletions(-) rename codegen/src/main/{scala => scala-2.12}/org/apache/pekko/grpc/gen/CodeGenerator.scala (100%) create mode 100644 codegen/src/main/scala-2.13+/org/apache/pekko/grpc/gen/CodeGenerator.scala diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index d544305a..7e5abe35 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -65,7 +65,7 @@ jobs: strategy: fail-fast: false matrix: - SCALA_VERSION: [2.12, 2.13, 3.3] + SCALA_VERSION: [2.12.18, 2.13.11, 3.3.1] JAVA_VERSION: [8, 11] steps: - name: Checkout @@ -83,8 +83,9 @@ jobs: uses: coursier/cache-action@v6 - name: Compile and test for JDK ${{ matrix.JAVA_VERSION }}, Scala ${{ matrix.SCALA_VERSION }} - run: sbt ++${{ matrix.SCALA_VERSION }}.* test - + run: |- + sbt ++${{ matrix.SCALA_VERSION }} test + sbt ++${{ matrix.SCALA_VERSION }}! codegen/test test-sbt: name: sbt scripted tests diff --git a/.github/workflows/publish-nightly.yml b/.github/workflows/publish-nightly.yml index 5d8dcf96..f2bc3ae1 100644 --- a/.github/workflows/publish-nightly.yml +++ b/.github/workflows/publish-nightly.yml @@ -27,6 +27,8 @@ jobs: run: |- cp .jvmopts-ci .jvmopts sbt +publish + sbt ++2.13.11! codegen/publish + sbt ++3.3.1! codegen/publish env: NEXUS_USER: ${{ secrets.NEXUS_USER }} NEXUS_PW: ${{ secrets.NEXUS_PW }} diff --git a/build.sbt b/build.sbt index 7f3e6cfe..4bb6607b 100644 --- a/build.sbt +++ b/build.sbt @@ -66,7 +66,15 @@ lazy val codegen = Project(id = "codegen", base = file("codegen")) (assembly / assemblyOption) := (assembly / assemblyOption).value.withPrependShellScript( Some(sbtassembly.AssemblyPlugin.defaultUniversalScript(shebang = true))), crossScalaVersions := Dependencies.Versions.CrossScalaForPlugin, - scalaVersion := scala212) + scalaVersion := scala212, + Compile / unmanagedSourceDirectories ++= { + if (scalaBinaryVersion.value == "2.12") { + Seq.empty + } else { + Seq( + project.base / "src" / "main" / "scala-2.13+") + } + }) .settings(addArtifact(Compile / assembly / artifact, assembly)) .settings(addArtifact(Artifact(pekkoGrpcCodegenId, "bat", "bat", "bat"), mkBatAssemblyTask)) diff --git a/codegen/src/main/scala/org/apache/pekko/grpc/gen/CodeGenerator.scala b/codegen/src/main/scala-2.12/org/apache/pekko/grpc/gen/CodeGenerator.scala similarity index 100% rename from codegen/src/main/scala/org/apache/pekko/grpc/gen/CodeGenerator.scala rename to codegen/src/main/scala-2.12/org/apache/pekko/grpc/gen/CodeGenerator.scala diff --git a/codegen/src/main/scala-2.13+/org/apache/pekko/grpc/gen/CodeGenerator.scala b/codegen/src/main/scala-2.13+/org/apache/pekko/grpc/gen/CodeGenerator.scala new file mode 100644 index 00000000..0859ce79 --- /dev/null +++ b/codegen/src/main/scala-2.13+/org/apache/pekko/grpc/gen/CodeGenerator.scala @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +/* + * Copyright (C) 2018-2021 Lightbend Inc. + */ + +package org.apache.pekko.grpc.gen + +import com.google.protobuf.ExtensionRegistry +import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest +import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse +import protocbridge.Artifact + +/** + * Code generator trait that is not directly bound to scala-pb or protoc (other than the types). + */ +trait CodeGenerator { + import CodeGenerator._ + + /** Generator name; example: `pekko-grpc-scala` */ + def name: String + + def run(request: CodeGeneratorRequest, logger: Logger): CodeGeneratorResponse + + /** Takes Scala binary version and returns suggested dependency Seq */ + def suggestedDependencies: ScalaBinaryVersion => Seq[Artifact] + + def registerExtensions(registry: ExtensionRegistry): Unit = {} + + final def run(request: Array[Byte], logger: Logger): Array[Byte] = { + val registry = ExtensionRegistry.newInstance + registerExtensions(registry) + run(CodeGeneratorRequest.parseFrom(request, registry), logger: Logger).toByteArray + } +} + +object CodeGenerator { + + /** Holds the prefix of a given Scala binary version */ + case class ScalaBinaryVersion(prefix: String) +} diff --git a/codegen/src/main/twirl/templates/ScalaClient/Client.scala.txt b/codegen/src/main/twirl/templates/ScalaClient/Client.scala.txt index c59c5591..312f0e18 100644 --- a/codegen/src/main/twirl/templates/ScalaClient/Client.scala.txt +++ b/codegen/src/main/twirl/templates/ScalaClient/Client.scala.txt @@ -34,7 +34,7 @@ import pekko.grpc.PekkoGrpcGenerated @{ def withSingleResponse(stmt: String) = Set("import pekko.grpc.scaladsl.SingleResponseRequestBuilder", stmt) def withStreamResponse(stmt: String) = Set("import pekko.grpc.scaladsl.StreamResponseRequestBuilder", stmt) - service.methods.flatMap { method: org.apache.pekko.grpc.gen.scaladsl.Method => + service.methods.flatMap { (method: org.apache.pekko.grpc.gen.scaladsl.Method) => val statements = method.methodType match { case org.apache.pekko.grpc.gen.Unary => withSingleResponse("import pekko.grpc.internal.ScalaUnaryRequestBuilder") diff --git a/project/Dependencies.scala b/project/Dependencies.scala index c5aa5f8e..440d13cd 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -15,6 +15,7 @@ import buildinfo.BuildInfo object Dependencies { object Versions { + // Update the .github workflows when these scala versions change val scala212 = "2.12.18" val scala213 = "2.13.11" val scala3 = "3.3.1" diff --git a/project/plugins.sbt b/project/plugins.sbt index 0389ebf7..7b59b823 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -17,7 +17,7 @@ addSbtPlugin("lt.dvim.authors" % "sbt-authors" % "1.3") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.9.0") addSbtPlugin("com.thesamet" % "sbt-protoc" % sbtProtocV) -addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.5.1") +addSbtPlugin("com.typesafe.play" % "sbt-twirl" % "1.6.1") addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1") addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")