aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Christopher Vogt <oss.nsp@cvogt.org>2017-03-24 14:42:46 -0400
committerGitHub <noreply@github.com>2017-03-24 14:42:46 -0400
commitb1da71bdb69b01feb4ce7bd90a39ffef4e8dc1b1 (patch)
treec97032dd1026334a57fdf16a5c1ff2ecd947d397
parent376abfa34a53b1bb0ffd546650429fff52ba90a0 (diff)
parent01855e0ed4eae2b1ab11b854176da940347f6131 (diff)
downloadcbt-b1da71bdb69b01feb4ce7bd90a39ffef4e8dc1b1.tar.gz
cbt-b1da71bdb69b01feb4ce7bd90a39ffef4e8dc1b1.tar.bz2
cbt-b1da71bdb69b01feb4ce7bd90a39ffef4e8dc1b1.zip
Merge pull request #452 from cvogt/scalapb-plugin
ScalaPB plugin
-rw-r--r--.gitignore1
-rw-r--r--examples/scalapb-example/build/build.scala8
-rw-r--r--examples/scalapb-example/build/build/build.scala5
-rw-r--r--examples/scalapb-example/protobuf-schemas/address.proto6
-rw-r--r--examples/scalapb-example/protobuf-schemas/person.proto12
-rw-r--r--examples/scalapb-example/src/Main.scala14
-rw-r--r--plugins/scalapb/ScalaPB.scala72
-rw-r--r--plugins/scalapb/build/build.scala23
-rw-r--r--plugins/scalapb/src_generated/BuildInfo.scala5
-rw-r--r--stage2/BuildBuild.scala1
-rw-r--r--stage2/Lib.scala1
-rw-r--r--test/test.scala7
12 files changed, 155 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index b6ecdc7..979f6df 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,3 +17,4 @@ examples/scalajs-plain-example/server/public/generated
examples/scalajs-react-example/server/public/generated
.cbt-loop.tmp
test/simple/src_generated
+examples/scalapb-example/src_generated
diff --git a/examples/scalapb-example/build/build.scala b/examples/scalapb-example/build/build.scala
new file mode 100644
index 0000000..31597d4
--- /dev/null
+++ b/examples/scalapb-example/build/build.scala
@@ -0,0 +1,8 @@
+import cbt._
+
+class Build(val context: Context) extends BaseBuild with Scalapb {
+ override def compile = taskCache[Build]("compile").memoize{
+ scalapb.apply
+ super.compile
+ }
+}
diff --git a/examples/scalapb-example/build/build/build.scala b/examples/scalapb-example/build/build/build.scala
new file mode 100644
index 0000000..c461c61
--- /dev/null
+++ b/examples/scalapb-example/build/build/build.scala
@@ -0,0 +1,5 @@
+import cbt._
+
+class Build(val context: Context) extends BuildBuild {
+ override def dependencies = super.dependencies :+ plugins.scalapb
+}
diff --git a/examples/scalapb-example/protobuf-schemas/address.proto b/examples/scalapb-example/protobuf-schemas/address.proto
new file mode 100644
index 0000000..c588512
--- /dev/null
+++ b/examples/scalapb-example/protobuf-schemas/address.proto
@@ -0,0 +1,6 @@
+syntax = "proto2";
+
+package foo;
+message Address {
+ optional string street = 1;
+}
diff --git a/examples/scalapb-example/protobuf-schemas/person.proto b/examples/scalapb-example/protobuf-schemas/person.proto
new file mode 100644
index 0000000..d829767
--- /dev/null
+++ b/examples/scalapb-example/protobuf-schemas/person.proto
@@ -0,0 +1,12 @@
+syntax = "proto2";
+
+import "address.proto";
+
+package foo;
+message Person {
+ optional string name = 1;
+ optional int32 age = 2;
+
+ // Address is a message defined somewhere else.
+ repeated Address addresses = 3;
+}
diff --git a/examples/scalapb-example/src/Main.scala b/examples/scalapb-example/src/Main.scala
new file mode 100644
index 0000000..83de69d
--- /dev/null
+++ b/examples/scalapb-example/src/Main.scala
@@ -0,0 +1,14 @@
+import foo._
+object Main extends App {
+ println(
+ person.Person(
+ Some("name"),
+ Some(123),
+ Seq(
+ address.Address(
+ Some("High Street")
+ )
+ )
+ )
+ )
+}
diff --git a/plugins/scalapb/ScalaPB.scala b/plugins/scalapb/ScalaPB.scala
new file mode 100644
index 0000000..e514476
--- /dev/null
+++ b/plugins/scalapb/ScalaPB.scala
@@ -0,0 +1,72 @@
+package cbt
+
+import com.trueaccord.scalapb._
+import _root_.protocbridge.ProtocBridge
+import _root_.scalapb.ScalaPbCodeGenerator
+import java.io.File
+
+trait Scalapb extends BaseBuild{
+ override def dependencies = super.dependencies ++ Resolver( mavenCentral ).bind(
+ ScalaDependency(
+ "com.trueaccord.scalapb", "scalapb-runtime", cbt.scalapb.BuildInfo.scalaPBVersion
+ )
+ )
+
+ def scalapb = Scalapb.apply(lib).config(
+ input = projectDirectory / "protobuf-schemas",
+ output = projectDirectory / "src_generated"
+ )
+}
+
+sealed trait ProtocVersion
+object ProtocVersion{
+ case object v310 extends ProtocVersion
+ case object v300 extends ProtocVersion
+ case object v261 extends ProtocVersion
+ case object v250 extends ProtocVersion
+ case object v241 extends ProtocVersion
+}
+
+object Scalapb{
+ case class apply(lib: Lib){
+ case class config(
+ input: File,
+ output: File,
+ version: ProtocVersion = ProtocVersion.v310,
+ flatPackage: Boolean = false,
+ javaConversions: Boolean = false,
+ grpc: Boolean = true,
+ singleLineToString: Boolean = false
+ ){
+ def apply = {
+ output.mkdirs
+ val protoFiles = input.listRecursive.filter(_.isFile).map(_.string).toArray
+ Seq(
+ javaConversions.option("javaConversions"),
+ grpc.option("grpc"),
+ singleLineToString.option("singleLineToString"),
+ flatPackage.option("flatPackage")
+ ).flatten.mkString(",")
+ import _root_.protocbridge.frontend.PluginFrontend
+ val pluginFrontend: PluginFrontend = PluginFrontend.newInstance()
+ val (scriptPath, state) = pluginFrontend.prepare( ScalaPbCodeGenerator )
+ try {
+ lib.redirectOutToErr(
+ ExitCode(
+ com.github.os72.protocjar.Protoc.runProtoc(
+ Array(
+ "-" ++ version.getClass.getSimpleName.stripSuffix("$"),
+ "-I=" ++ input.string,
+ "--scala_out=" ++ output.string,
+ s"--plugin=protoc-gen-scala=${scriptPath}"
+ ) ++ protoFiles
+ )
+ )
+ )
+ } finally {
+ pluginFrontend.cleanup(state)
+ }
+ }
+ }
+ }
+}
diff --git a/plugins/scalapb/build/build.scala b/plugins/scalapb/build/build.scala
new file mode 100644
index 0000000..cadee1b
--- /dev/null
+++ b/plugins/scalapb/build/build.scala
@@ -0,0 +1,23 @@
+import cbt._
+
+class Build(val context: Context) extends Plugin {
+ private val scalaPBVersion = "0.5.47"
+
+ override def dependencies =
+ super.dependencies ++
+ Resolver( mavenCentral ).bind(
+ ScalaDependency( "com.trueaccord.scalapb", "scalapbc", scalaPBVersion )
+ )
+
+ override def compile = { buildInfo; super.compile }
+
+ def buildInfo = lib.writeIfChanged(
+ projectDirectory / "src_generated/BuildInfo.scala",
+ s"""// generated file
+package cbt.scalapb
+object BuildInfo{
+ def scalaPBVersion = "$scalaPBVersion"
+}
+"""
+ )
+}
diff --git a/plugins/scalapb/src_generated/BuildInfo.scala b/plugins/scalapb/src_generated/BuildInfo.scala
new file mode 100644
index 0000000..8498d10
--- /dev/null
+++ b/plugins/scalapb/src_generated/BuildInfo.scala
@@ -0,0 +1,5 @@
+// generated file
+package cbt.scalapb
+object BuildInfo{
+ def scalaPBVersion = "0.5.47"
+}
diff --git a/stage2/BuildBuild.scala b/stage2/BuildBuild.scala
index 71a229c..17ccb36 100644
--- a/stage2/BuildBuild.scala
+++ b/stage2/BuildBuild.scala
@@ -11,6 +11,7 @@ class plugins(implicit context: Context){
final lazy val sbtLayout = plugin( "sbt_layout" )
final lazy val scalafmt = plugin( "scalafmt" )
final lazy val scalaJs = plugin( "scalajs" )
+ final lazy val scalapb = plugin( "scalapb" )
final lazy val scalariform = plugin( "scalariform" )
final lazy val scalaTest = plugin( "scalatest" )
final lazy val sonatypeRelease = plugin( "sonatype-release" )
diff --git a/stage2/Lib.scala b/stage2/Lib.scala
index 4fdcf2d..fd3346e 100644
--- a/stage2/Lib.scala
+++ b/stage2/Lib.scala
@@ -131,6 +131,7 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){
//case e: ExitCode => System.err.println(e.integer); System.exit(e.integer); ???
case s: Seq[_] => s.map(render).mkString("\n")
case s: Set[_] => s.map(render).toSeq.sorted.mkString("\n")
+ case null => "null"
case _ => obj.toString
}
}
diff --git a/test/test.scala b/test/test.scala
index 9bb3f6b..dbf4f9e 100644
--- a/test/test.scala
+++ b/test/test.scala
@@ -270,6 +270,12 @@ object Main{
}
{
+ val res = runCbt("../examples/scalapb-example", Seq("run"))
+ assert(res.exit0)
+ assert(res.out contains "age: 123", res.out ++ "\n--\n" ++ res.err)
+ }
+
+ {
val res = runCbt("broken-build/build-class-with-wrong-arguments", Seq("run"))
assert(!res.exit0)
assert(res.err contains s"Expected class ${lib.buildClassName}(val context: Context), but found different constructor", res.err)
@@ -388,6 +394,7 @@ object Main{
{
val sourceFile = cbtHome / "examples" / "scalafix-example" / "Main.scala"
val sourceBefore = sourceFile.readAsString
+ runCbt("../examples/scalafix-example", Seq("clean","force"))
val res = runCbt("../examples/scalafix-example", Seq("compile"))
assert(res.exit0)
val sourceAfter = sourceFile.readAsString