Skip to content

Commit

Permalink
Use sbt-protoc and protocbridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Jan 11, 2016
1 parent bf10eac commit ad62532
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,6 +18,7 @@ project/plugins/project/
.idea

# Auto-generated file.
compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/Version.scala
e2e/project/project/Version.scala
e2e/project/Version.scala
e2e/.bin
Expand Down
13 changes: 13 additions & 0 deletions build.sbt
Expand Up @@ -96,6 +96,19 @@ lazy val grpcRuntime = project.in(file("scalapb-runtime-grpc"))

lazy val compilerPlugin = project.in(file("compiler-plugin"))
.dependsOn(runtimeJVM)
.settings(
sourceGenerators in Compile <+= Def.task {
val file = (sourceManaged in Compile).value / "com" / "trueaccord" / "scalapb" / "compiler" / "Version.scala"
IO.write(file,
s"""package com.trueaccord.scalapb.compiler
|object Version {
| val scalapbVersion = "${version.value}"
|}""".stripMargin)
Seq(file)
},
libraryDependencies ++= Seq(
"com.trueaccord.scalapb" %% "protoc-bridge" % "0.1"
))

lazy val scalapbc = project.in(file("scalapbc"))
.dependsOn(compilerPlugin, runtimeJVM)
Expand Down

This file was deleted.

Expand Up @@ -33,10 +33,12 @@ import scala.util.Try
* 6. the plugin reads the data and writes it to standard out.
* 7. protoc handles the CodeGenerationResponse (creates Scala sources)
*/
@deprecated("Use protoc-bridge instead.", "0.5.20")
trait ProtocDriver {
def buildRunner[A](runner: Seq[String] => A)(params: Seq[String]): A
}

@deprecated("Use protoc-bridge instead.", "0.5.20")
object ProtocDriverFactory {
private def isWindows: Boolean = sys.props("os.name").startsWith("Windows")

Expand All @@ -46,6 +48,7 @@ object ProtocDriverFactory {
}

/** A driver that creates a named pipe and sets up a shell script as a protoc plugin */
@deprecated("Use protoc-bridge instead.", "0.5.20")
class PosixProtocDriver extends ProtocDriver {
def buildRunner[A](runner: Seq[String] => A)(params: Seq[String]): A = {
val inputPipe = createPipe()
Expand Down Expand Up @@ -92,10 +95,11 @@ class PosixProtocDriver extends ProtocDriver {
}
}

/** A driver that binds a server socket to a local interface. The plugin
* is a batch script that invokes Python, which will communicate wire its
* stdin and stdout to this socket.
*/
/** A driver that binds a server socket to a local interface. The plugin
* is a batch script that invokes Python, which will communicate wire its
* stdin and stdout to this socket.
*/
@deprecated("Use protoc-bridge instead.", "0.5.20")
class WindowsProtocDriver(pythonExecutable: String) extends ProtocDriver {
def buildRunner[A](runner: Seq[String] => A)(params: Seq[String]): A = {
val ss = new ServerSocket(0)
Expand Down Expand Up @@ -142,6 +146,7 @@ class WindowsProtocDriver(pythonExecutable: String) extends ProtocDriver {
}
}

@deprecated("Use protoc-bridge instead.", "0.5.20")
object Process {
private def getStackTrace(e: Throwable): String = {
val stringWriter = new StringWriter
Expand Down
26 changes: 26 additions & 0 deletions compiler-plugin/src/main/scala/scalapb/ScalaPbCodeGenerator.scala
@@ -0,0 +1,26 @@
package scalapb

import com.google.protobuf.ExtensionRegistry
import com.google.protobuf.compiler.PluginProtos.{CodeGeneratorRequest, CodeGeneratorResponse}
import com.trueaccord.scalapb.Scalapb
import com.trueaccord.scalapb.compiler.ProtobufGenerator
import protocbridge.{ProtocCodeGenerator, Artifact}


object ScalaPbCodeGenerator extends ProtocCodeGenerator {
override def name: String = "scala"

override def run(req: CodeGeneratorRequest): CodeGeneratorResponse = {
ProtobufGenerator.handleCodeGeneratorRequest(req)
}

override def registerExtensions(registry: ExtensionRegistry): Unit = {
Scalapb.registerAllExtensions(registry)
}

override def suggestedDependencies: Seq[Artifact] = Seq(
Artifact("com.google.protobuf", "protobuf-java", "3.0.0-beta-2"),
Artifact("com.trueaccord.scalapb", "scalapb-runtime",
com.trueaccord.scalapb.compiler.Version.scalapbVersion, crossVersion = true)
)
}
13 changes: 13 additions & 0 deletions compiler-plugin/src/main/scala/scalapb/package.scala
@@ -0,0 +1,13 @@
package object scalapb {
def generator: protocbridge.Generator = generator()

def generator(flatPackage: Boolean = false,
javaConversions: Boolean = false,
grpc: Boolean = false): protocbridge.Generator = {
scalapb.ScalaPbCodeGenerator.toGenerator(
Seq(
javaConversions -> "java_conversions",
flatPackage -> "flat_package",
grpc -> "grpc").filter(_._1).map(_._2))
}
}
9 changes: 5 additions & 4 deletions proptest/src/test/scala/SchemaGenerators.scala
Expand Up @@ -4,11 +4,13 @@ import java.nio.file.Files
import javax.tools.ToolProvider

import com.google.protobuf.Message.Builder
import com.trueaccord.scalapb.compiler.{ProtocDriverFactory, PosixProtocDriver, WindowsProtocDriver, FunctionalPrinter}
import com.trueaccord.scalapb.compiler._
import org.scalacheck.Gen
import com.trueaccord.scalapb._
import protocbridge.ProtocBridge

import scala.reflect.ClassTag
import scalapb.ScalaPbCodeGenerator

object SchemaGenerators {

Expand Down Expand Up @@ -88,9 +90,8 @@ object SchemaGenerators {
}

private def runProtoc(args: String*) =
ProtocDriverFactory
.create()
.buildRunner(args => com.github.os72.protocjar.Protoc.runProtoc("-v300" +: args.toArray))(args)
ProtocBridge.runWithGenerators(args => com.github.os72.protocjar.Protoc.runProtoc("-v300" +: args.toArray), args,
Seq("scala" -> ScalaPbCodeGenerator))

def compileProtos(rootNode: RootNode, tmpDir: File): Unit = {
val files = rootNode.files.map {
Expand Down
12 changes: 8 additions & 4 deletions scalapbc/src/main/scala/com/trueaccord/scalapb/ScalaPBC.scala
@@ -1,11 +1,15 @@
package com.trueaccord.scalapb

import com.trueaccord.scalapb.compiler.ProtocDriverFactory
import protocbridge.ProtocBridge

import scalapb.ScalaPbCodeGenerator

object ScalaPBC extends App {
val code = ProtocDriverFactory.create().buildRunner({
val code = ProtocBridge.runWithGenerators(
args =>
com.github.os72.protocjar.Protoc.runProtoc("-v300" +: args.toArray)
})(args)
com.github.os72.protocjar.Protoc.runProtoc("-v300" +: args.toArray),
args,
Seq("scala" -> ScalaPbCodeGenerator))

sys.exit(code)
}

0 comments on commit ad62532

Please sign in to comment.