From 779c6ee32486890c327207efff58a05858a178c3 Mon Sep 17 00:00:00 2001 From: Gerolf Seitz Date: Thu, 12 Mar 2015 15:34:33 +0100 Subject: [PATCH] A more flexible way to execute the protobuf compiler. --- README.md | 9 ++++++++- build.sbt | 2 +- notes/0.4.0.markdown | 2 ++ src/main/scala/sbtprotobuf/ProtobufPlugin.scala | 15 ++++++--------- 4 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 notes/0.4.0.markdown diff --git a/README.md b/README.md index 47a09c0..1115a40 100644 --- a/README.md +++ b/README.md @@ -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). @@ -164,6 +164,13 @@ All settings and tasks are in the `protobuf` scope. If you want to execute the ` nameshell-namedescription generateprotobuf-generatePerforms the hardcore compiling action and is automatically executed as a "source generator" in the Compile scope. unpackDependenciesprotobuf-unpack-dependenciesExtracts proto files from library-dependencies into protobuf-external-inlude-patch + + runProtoc + protobuf-run-protoc + 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. + + ## Credits diff --git a/build.sbt b/build.sbt index a2610a3..05c1d8c 100644 --- a/build.sbt +++ b/build.sbt @@ -4,7 +4,7 @@ organization := "com.github.gseitz" name := "sbt-protobuf" -version := "0.3.3" +version := "0.4.0-SNAPSHOT" scalacOptions := Seq("-deprecation", "-unchecked") diff --git a/notes/0.4.0.markdown b/notes/0.4.0.markdown new file mode 100644 index 0000000..3059499 --- /dev/null +++ b/notes/0.4.0.markdown @@ -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)). \ No newline at end of file diff --git a/src/main/scala/sbtprotobuf/ProtobufPlugin.scala b/src/main/scala/sbtprotobuf/ProtobufPlugin.scala index 4abbf51..b5167e6 100644 --- a/src/main/scala/sbtprotobuf/ProtobufPlugin.scala +++ b/src/main/scala/sbtprotobuf/ProtobufPlugin.scala @@ -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.") @@ -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, @@ -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()) @@ -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]) =>