在SBT任务中手动调用ScalaPB编译器

时间:2016-04-24 19:11:44

标签: scala sbt protocol-buffers scalapb

我正在使用ScalaPB来合成Scala类,以便将数据转换为Protobuf表示形式。默认情况下,the SBT setup会挂钩sbt compile以生成target文件夹下的文件。

因为我希望我的.proto文件不经常更改,所以我宁愿手动调用ScalaPB进程,并将生成的文件保留在版本控制之下。这与我用于Slick's code generation functionality的方法相同。

我可以做类似的事情:

lazy val genProto = TaskKey[Unit]("gen-proto", "Generate Scala classes from a proto file")
genProto := {
  val protoSources = ...
  val outputDirectory = ...

  // ? run the same process 
}

但我不确定如何使用自定义输入和输出从SBT调用该过程。

我的最新尝试:

ScalaPbPlugin.runProtoc in ScalaPbPlugin.protobufConfig := (args =>
  com.github.os72.protocjar.Protoc.runProtoc("-v261" +: args.toArray))

lazy val genProto = TaskKey[Unit]("gen-proto", "Generate Scala classes from a proto file")

genProto := {
  val protoSourceDirectory = sourceDirectory.value / "main" / "protobuf"
  val outputDirectory = (scalaSource in Compile).value / outputProtoDirectory
  val schemas = (protoSourceDirectory ** "*.proto").get.map(_.getAbsoluteFile)
  val includeOption = Seq(s"-I$protoSourceDirectory")
  val outputOption = Seq(s"--scala_out=${outputDirectory.absolutePath}")
  val options = schemas.map(_.absolutePath) ++ includeOption ++ outputOption
  (ScalaPbPlugin.runProtoc in ScalaPbPlugin.protobufConfig).value(options)
  (outputDirectory ** "*.scala").get.toSet
}

我收到以下错误:

> genProto
protoc-jar: protoc version: 261, detected platform: mac os x/x86_64
protoc-jar: executing: [/var/folders/lj/_85rbyf5525d3ktt666yjztr0000gn/T/protoc2879794465962204787.exe, /Users/alan/projects/causality/src/main/protobuf/lotEventStoreModel.proto, -I/Users/alan/projects/causality/src/main/protobuf, --scala_out=/Users/alan/projects/causality/src/main/scala/net/artsy/auction/protobuf]
protoc-gen-scala: program not found or is not executable
--scala_out: protoc-gen-scala: Plugin failed with status code 1.
[success] Total time: 0 s, completed Apr 25, 2016 9:39:09 AM

1 个答案:

答案 0 :(得分:0)

MM