Skip to content

Commit

Permalink
chore: create codegenScalaCompilationTest module for quick turnaround…
Browse files Browse the repository at this point in the history
… codegen checking (#664)
  • Loading branch information
jrudolph committed Oct 21, 2021
1 parent 02d6592 commit 59476f2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
19 changes: 19 additions & 0 deletions build.sbt
Expand Up @@ -13,6 +13,7 @@ lazy val `akkaserverless-java-sdk` = project
codegenJava,
codegenJavaCompilationTest,
codegenScala,
codegenScalaCompilationTest,
sbtPlugin)

def common: Seq[Setting[_]] =
Expand Down Expand Up @@ -219,6 +220,7 @@ lazy val codegenJavaCompilationTest = project
.settings(common)
.settings(libraryDependencies ++= Seq(Dependencies.junit4))
.settings(
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java),
(publish / skip) := true,
name := "akkaserverless-codegen-java-compilation-tests",
Compile / PB.protoSources += baseDirectory.value / ".." / ".." / "sbt-plugin" / "src" / "sbt-test" / "sbt-akkaserverless" / "compile-only" / "src" / "main" / "protobuf",
Expand Down Expand Up @@ -294,6 +296,23 @@ lazy val codegenScala =
testFrameworks += new TestFramework("munit.Framework"))
.dependsOn(codegenCore % "compile->compile;test->test")

lazy val codegenScalaCompilationTest = project
.in(file("codegen/scala-gen-compilation-tests"))
.enablePlugins(ReflectiveCodeGen)
.dependsOn(sdkScala)
// code generated by the codegen requires the testkit, junit4
// Note: we don't use test scope since all code is generated in src_managed
// and the goal is to verify if it compiles
.dependsOn(sdkScalaTestKit)
.settings(common)
.settings(libraryDependencies ++= Seq(Dependencies.junit4, Dependencies.akkaslsSdkProtocol % "protobuf-src"))
.settings(
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Scala),
(publish / skip) := true,
name := "akkaserverless-codegen-scala-compilation-tests",
Compile / PB.protoSources += baseDirectory.value / ".." / ".." / "sbt-plugin" / "src" / "sbt-test" / "sbt-akkaserverless" / "compile-only" / "src" / "main" / "protobuf",
ReflectiveCodeGen.copyUnmanagedSources := false)

lazy val sbtPlugin = Project(id = "sbt-akkaserverless", base = file("sbt-plugin"))
.enablePlugins(SbtPlugin)
.enablePlugins(PublishSonatype)
Expand Down
45 changes: 43 additions & 2 deletions project/ReflectiveCodeGen.scala
Expand Up @@ -5,6 +5,7 @@ import sbtprotoc.ProtocPlugin
import ProtocPlugin.autoImport.PB
import akka.grpc.sbt.AkkaGrpcPlugin
import akka.grpc.sbt.AkkaGrpcPlugin.autoImport._
import protocbridge.{ Artifact, SandboxedJvmGenerator }
import sbt.internal.inc.classpath.ClasspathUtilities

import java.nio.file.{ Files, Path, Paths }
Expand All @@ -23,9 +24,49 @@ object ReflectiveCodeGen extends AutoPlugin {

override def projectSettings =
Seq(
Compile / akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java),
Compile / akkaGrpcCodeGeneratorSettings += "flat_package",
Test / akkaGrpcGeneratedSources := Seq(AkkaGrpc.Client),
Compile / sourceGenerators += runCodeGenTask.taskValue) ++ attachProtobufDescriptorSets
// Java side directly calls generator reflectively
Compile / sourceGenerators ++= {
if ((Compile / akkaGrpcGeneratedLanguages).value contains AkkaGrpc.Java) Seq(runCodeGenTask.taskValue)
else Seq.empty
},
// Scala side registers protoc plugins for generation
Compile / PB.targets ++= {
if ((Compile / akkaGrpcGeneratedLanguages).value contains AkkaGrpc.Scala)
Seq(
gen(
akkaGrpcCodeGeneratorSettings.value ++ Seq("enable-debug", "flat_package"),
"com.akkaserverless.codegen.scalasdk.AkkaserverlessGenerator$") -> (Compile / sourceManaged).value,
gen(
akkaGrpcCodeGeneratorSettings.value ++ Seq("enable-debug", "flat_package"),
"com.akkaserverless.codegen.scalasdk.AkkaserverlessUnmanagedGenerator$") -> (Compile / temporaryUnmanagedDirectory).value)
else Seq.empty
},
PB.artifactResolver := Def.taskDyn {
val cp = (ProjectRef(file("."), "codegenScala") / Compile / fullClasspath).value.map(_.data)
val oldResolver = PB.artifactResolver.value
Def.task { (artifact: Artifact) =>
artifact.groupId match {
case "com.akkaserverless.codegen.scalasdk.BuildInfo.organization" =>
cp
case _ =>
oldResolver(artifact)
}
}
}.value) ++ attachProtobufDescriptorSets

def gen(options: Seq[String] = Seq.empty, generatorClass: String): (SandboxedJvmGenerator, Seq[String]) =
(
SandboxedJvmGenerator.forModule(
"scala",
Artifact(
"com.akkaserverless.codegen.scalasdk.BuildInfo.organization",
"akkaserverless-codegen-scala_2.12",
"SNAPSHOT"),
generatorClass,
Nil),
options)

def runAkkaServerlessCodegen(
classpath: Classpath,
Expand Down

0 comments on commit 59476f2

Please sign in to comment.