4

我正在使用ScalaPB来合成 Scala 类,以便将我的数据与Protobuf表示进行转换。默认情况下,SBT 安装程序会钩入sbt compile以生成文件夹下的target文件。

因为我希望我的.proto文件很少更改,所以我宁愿在它们发生更改时手动调用 ScalaPB 进程,并将生成的文件置于版本控制之下。这与我用于Slick 的代码生成功能的方法相同。

我可以做类似的事情:

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
4

1 回答 1

0
import sbt._
import Keys._

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

genProto := {
    Seq("/path/to/scalapbc-0.5.24/bin/scalapbc",
        "src/main/protobuf/test.proto",
        "--scala_out=src/main/scala/") !
}
于 2016-04-25T05:36:41.250 回答