Skip to content
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
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ val playActionsTestData = Project("play-grpc-actions-testdata", file("play-actio
.pluginTestingSettings

val playGenerators = Project(id = "play-grpc-generators", file("play-generators"))
.configs(WorkaroundTwirlFormatCompat.Sources)
.enablePlugins(SbtTwirl, BuildInfoPlugin)
.settings(
// Temporary Twirl compatibility shim:
Expand All @@ -118,6 +119,7 @@ val playGenerators = Project(id = "play-grpc-generators", file("play-generators"
Compile / sourceGenerators += WorkaroundTwirlFormatCompat.generate.taskValue,
libraryDependencies ++= Seq(
Dependencies.Compile.pekkoGrpcCodegen,
Dependencies.Compile.pekkoGrpcCodegen.classifier("sources").intransitive() % WorkaroundTwirlFormatCompat.Sources,
Dependencies.Test.scalaTest,
),
buildInfoKeys ++= Seq[BuildInfoKey](organization, name, version, scalaVersion, sbtVersion),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import play.grpc.internal.PlayRouter
extends PlayRouter(@{service.name}.name)
with @{serviceName} {

override protected val respond = @{serviceName}Handler(this, @{service.name}.name, eHandler)(system)
override protected val respond = @{serviceName}Handler(this, @{service.name}.name, eHandler)(@{service.scalaCompatConstants.ImplicitUsing}system)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import scala.concurrent.ExecutionContext
*/
@@InternalApi
final override protected def createHandler(serviceName: String, system: ActorSystem): RequestHeader => EssentialAction = {
val handler = @{serviceName}Handler(this, serviceName, eHandler)(system)
val handler = @{serviceName}Handler(this, serviceName, eHandler)(@{service.scalaCompatConstants.ImplicitUsing}system)
reqOuter =>
implicit val ec: ExecutionContext = system.dispatcher
Path(reqOuter.path) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package org.apache.pekko.grpc.gen.scaladsl

object BadCitizen {
def scalaCompatConstants(): ScalaCompatConstants = new org.apache.pekko.grpc.gen.scaladsl.ScalaCompatConstants(
emitScala3Sources = true,
)
def scalaCompatConstants(emitScala3Sources: Boolean = true): ScalaCompatConstants =
new org.apache.pekko.grpc.gen.scaladsl.ScalaCompatConstants(
emitScala3Sources = emitScala3Sources,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.grpc.gen.scaladsl

import com.google.protobuf.DescriptorProtos.ServiceOptions
import org.apache.pekko.grpc.gen.scaladsl.BadCitizen
import org.apache.pekko.grpc.gen.scaladsl.Service
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class PlayScalaServerCodeGeneratorSpec extends AnyWordSpec with Matchers {
"The Scala server router templates" must {
for {
scala3Sources <- Seq(false, true)
powerApis <- Seq(false, true)
useActions <- Seq(false, true)
} {
s"pass the actor system correctly with scala3Sources=$scala3Sources, powerApis=$powerApis and useActions=$useActions" in {
val service = Service(
"descriptor",
"example",
"GreeterService",
"example.GreeterService",
Nil,
powerApis,
useActions,
ServiceOptions.getDefaultInstance,
None,
BadCitizen.scalaCompatConstants(scala3Sources),
)
val source =
if (useActions) templates.PlayScala.txt.RouterUsingActions(service, powerApis).body
else templates.PlayScala.txt.Router(service, powerApis).body
val name = if (powerApis) "GreeterServicePowerApi" else "GreeterService"
val prefix = if (useActions) "serviceName" else "GreeterService.name"
val systemArgument = if (scala3Sources) "using system" else "system"
source should include(s"${name}Handler(this, $prefix, eHandler)($systemArgument)")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import play.api.mvc.request.RequestTarget
import play.api.mvc.request.Scheme
import play.api.mvc.request.TransportConnection
import play.api.mvc.Headers
import GreeterServiceMarshallers._
import GreeterServiceMarshallers.HelloReplySerializer
import GreeterServiceMarshallers.HelloRequestSerializer

class PlayScalaRouterSpec extends AnyWordSpec with Matchers with BeforeAndAfterAll with ScalaFutures {
implicit val sys: ActorSystem = ActorSystem()
Expand Down
4 changes: 4 additions & 0 deletions project/ProjectExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ object ProjectExtensions {
// Defaults to `Seq("Scala")` so we only need to add Java
ReflectiveCodeGen.generatedLanguages += PekkoGrpc.Java,
ReflectiveCodeGen.codeGeneratorSettings -= "flat_package", // avoid Java+Scala fqcn conflicts
// Match the sbt plugin's Scala 3 source generation in the integration fixtures.
ReflectiveCodeGen.codeGeneratorSettings ++= {
if (scalaBinaryVersion.value == "3") Seq("scala3_sources") else Seq.empty
},
)
}
}
15 changes: 12 additions & 3 deletions project/WorkaroundTwirlFormatCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import sbt._
import sbt.Keys._

object WorkaroundTwirlFormatCompat {
val Sources = config("twirlCompatSources").hide

private val TemplateSources = Seq(
"templates/JavaClient/txt/GenMethodImports.template.scala",
"templates/JavaClient/txt/ClientPowerApi.template.scala",
Expand Down Expand Up @@ -35,10 +37,17 @@ object WorkaroundTwirlFormatCompat {
val outputDir = (Compile / sourceManaged).value / "pekko-grpc-twirl-compat"
val extractDir = streams.value.cacheDirectory / "pekko-grpc-twirl-compat"

val sourceJar = (Compile / updateClassifiers).value.allFiles
.find(file =>
file.getName == s"pekko-grpc-codegen_${scalaBinaryVersion.value}-${Dependencies.Versions.pekkoGrpc}-sources.jar"
// This is a required build input; optional IDE classifier downloads can omit it.
val sourceJar = update.value
.matching(
configurationFilter(Sources.name) &&
moduleFilter(
organization = "org.apache.pekko",
name = s"pekko-grpc-codegen_${scalaBinaryVersion.value}",
revision = Dependencies.Versions.pekkoGrpc,
) && artifactFilter(classifier = "sources")
)
.headOption
.getOrElse(sys.error("Could not locate pekko-grpc-codegen sources jar for Twirl compatibility generation"))

IO.delete(outputDir)
Expand Down
Loading