Skip to content

Commit

Permalink
A more flexible way to execute the protobuf compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
gseitz committed Mar 12, 2015
1 parent 6590cd4 commit 779c6ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A plugin for sbt-0.(12|13).x that transforms *.proto files into gazillion-loc Ja
### Adding the plugin dependency
In your project, create a file for plugin library dependencies `project/plugins.sbt` and add the following lines:

addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.3.3")
addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.4.0")

The dependency to `"com.google.protobuf" % "protobuf-java"` is automatically added to the `Compile` scope.
The version for `protobuf-java` can be controlled by the setting `version in protobufConfig` (set to `2.5.0` by default).
Expand Down Expand Up @@ -164,6 +164,13 @@ All settings and tasks are in the `protobuf` scope. If you want to execute the `
<tr><th>name</th><th>shell-name</th><th>description</th></tr>
<tr><td>generate</td><td>protobuf-generate</td><td>Performs the hardcore compiling action and is automatically executed as a "source generator" in the <code>Compile</code> scope.</td></tr>
<tr><td>unpackDependencies</td><td>protobuf-unpack-dependencies</td><td>Extracts proto files from <code>library-dependencies</code> into <code>protobuf-external-inlude-patch</code></td></tr>
<tr>
<td>runProtoc</td>
<td>protobuf-run-protoc</td>
<td>A function that executes the protobuf compiler with the given arguments,
returning the exit code. The default implementation runs the executable referenced by the `protoc` setting.</td>
</tr>

</table>

## Credits
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ organization := "com.github.gseitz"

name := "sbt-protobuf"

version := "0.3.3"
version := "0.4.0-SNAPSHOT"

scalacOptions := Seq("-deprecation", "-unchecked")

Expand Down
2 changes: 2 additions & 0 deletions notes/0.4.0.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Added task `runProtoc`, representing a function that executes the protobuf compiler.
This opens up the possibility to invoke *protoc* via means other than just an executable on the filesystem (eg. via [os72/protoc-jar](https://github.com/os72/protoc-jar)).
15 changes: 6 additions & 9 deletions src/main/scala/sbtprotobuf/ProtobufPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object ProtobufPlugin extends Plugin {

val includePaths = TaskKey[Seq[File]]("protobuf-include-paths", "The paths that contain *.proto dependencies.")
val protoc = SettingKey[String]("protobuf-protoc", "The path+name of the protoc executable.")
val runProtoc = TaskKey[Seq[String] => Int]("protobuf-run-protoc", "A function that executes the protobuf compiler with the given arguments, returning the exit code of the compilation run.")
val externalIncludePath = SettingKey[File]("protobuf-external-include-path", "The path to which protobuf:library-dependencies are extracted and which is used as protobuf:include-path for protoc")
val generatedTargets = SettingKey[Seq[(File,String)]]("protobuf-generated-targets", "Targets for protoc: target directory and glob for generated source files")
val generate = TaskKey[Seq[File]]("protobuf-generate", "Compile the protobuf sources.")
Expand All @@ -23,6 +24,7 @@ object ProtobufPlugin extends Plugin {
javaSource <<= (sourceManaged in Compile) { _ / "compiled_protobuf" },
externalIncludePath <<= target(_ / "protobuf_external"),
protoc := "protoc",
runProtoc <<= (protoc, streams) map ((cmd, s) => args => Process(cmd, args) ! s.log),
version := "2.5.0",

generatedTargets := Nil,
Expand Down Expand Up @@ -58,20 +60,15 @@ object ProtobufPlugin extends Plugin {

case class UnpackedDependencies(dir: File, files: Seq[File])

private def executeProtoc(protocCommand: String, schemas: Set[File], includePaths: Seq[File], protocOptions: Seq[String], log: Logger) =
private def executeProtoc(protocCommand: Seq[String] => Int, schemas: Set[File], includePaths: Seq[File], protocOptions: Seq[String], log: Logger) : Int =
try {
val incPath = includePaths.map("-I" + _.absolutePath)
val proc = Process(
protocCommand,
incPath ++ protocOptions ++ schemas.map(_.absolutePath)
)
proc ! log
protocCommand(incPath ++ protocOptions ++ schemas.map(_.absolutePath))
} catch { case e: Exception =>
throw new RuntimeException("error occured while compiling protobuf files: %s" format(e.getMessage), e)
}


private def compile(protocCommand: String, schemas: Set[File], includePaths: Seq[File], protocOptions: Seq[String], generatedTargets: Seq[(File, String)], log: Logger) = {
private def compile(protocCommand: Seq[String] => Int, schemas: Set[File], includePaths: Seq[File], protocOptions: Seq[String], generatedTargets: Seq[(File, String)], log: Logger) = {
val generatedTargetDirs = generatedTargets.map(_._1)

generatedTargetDirs.foreach(_.mkdirs())
Expand Down Expand Up @@ -103,7 +100,7 @@ object ProtobufPlugin extends Plugin {
}

private def sourceGeneratorTask =
(streams, sourceDirectories in protobufConfig, includePaths in protobufConfig, protocOptions in protobufConfig, generatedTargets in protobufConfig, cacheDirectory, protoc) map {
(streams, sourceDirectories in protobufConfig, includePaths in protobufConfig, protocOptions in protobufConfig, generatedTargets in protobufConfig, cacheDirectory, runProtoc) map {
(out, srcDirs, includePaths, protocOpts, otherTargets, cache, protocCommand) =>
val schemas = srcDirs.toSet[File].flatMap(srcDir => (srcDir ** "*.proto").get.map(_.getAbsoluteFile))
val cachedCompile = FileFunction.cached(cache / "protobuf", inStyle = FilesInfo.lastModified, outStyle = FilesInfo.exists) { (in: Set[File]) =>
Expand Down

1 comment on commit 779c6ee

@gseitz
Copy link
Member Author

@gseitz gseitz commented on 779c6ee Mar 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #23.

Please sign in to comment.