Skip to content

Commit

Permalink
support scala 2.13 and scala 3 build for codegen lib (#181)
Browse files Browse the repository at this point in the history
* support scala 2.13 and scala 3 build for codegen lib

* Update build.sbt

* rework build
  • Loading branch information
pjfanning authored Sep 23, 2023
1 parent c95da03 commit 53f91e8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 6 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
10 changes: 9 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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,

This comment has been minimized.

Copy link
@WojciechMazur

WojciechMazur Oct 31, 2023

Hey, shouldn't the crossScalaVersions should be changed? Now it still default to 2.12 only.
I'm asking, because we're getting a conflicting information in the Scala 3 Open Community Build for this project.
In the Scaladex there is a published version of codegen for Scala 3 and 2.13, but the pekko-grpc build setup does say it only cross compiles to 2.12.

This comment has been minimized.

Copy link
@pjfanning

pjfanning Oct 31, 2023

Author Contributor

sbt build stopped working when I changed the crossScalaVersions value. This can be worked around when building - see https://github.com/apache/incubator-pekko-site/wiki/Pekko-gRPC-Release#diffs

If you can find another solution, that be appreciated.

This comment has been minimized.

Copy link
@WojciechMazur

WojciechMazur Oct 31, 2023

Oh, right, that's because of dependency from non-cross-build project (sbt plugin) on the codegen which would become cross-compiled. Then we might get the errors about the incompatible suffixes, that is incompatible Scala binary versions.
One solution for that would be introducing a matrix of projects, so have a codegen2_12, codegen2_13, codegen3 and then depend on the exactly needed binary version. Something like that is used in ScalaNative using so called MultiScalaProject and similarly in Scala.js, but I don't think it would be worth it introduce it here.

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))

Expand Down
Original file line number Diff line number Diff line change
@@ -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. <https://www.lightbend.com>
*/

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 53f91e8

Please sign in to comment.