diff options
author | Florian Duraffourg <fduraffourg@users.noreply.github.com> | 2019-11-18 09:30:42 +0100 |
---|---|---|
committer | Tobias Roeser <le.petit.fou@web.de> | 2019-11-18 09:30:42 +0100 |
commit | d996fe0d92a977b2006892f3f30f912887853115 (patch) | |
tree | 3f107dd0a7100a156cfb776961d8838df1a786e9 | |
parent | b2bdae3e6d5b15c0db64760955c6d0f817f9ae94 (diff) | |
download | mill-d996fe0d92a977b2006892f3f30f912887853115.tar.gz mill-d996fe0d92a977b2006892f3f30f912887853115.tar.bz2 mill-d996fe0d92a977b2006892f3f30f912887853115.zip |
Add `scalaPBProtocPath` option to scalapblib (#714)
-rw-r--r-- | contrib/scalapblib/src/ScalaPBModule.scala | 3 | ||||
-rw-r--r-- | contrib/scalapblib/src/ScalaPBWorker.scala | 12 | ||||
-rw-r--r-- | contrib/scalapblib/test/src/TutorialTests.scala | 16 | ||||
-rw-r--r-- | docs/pages/9 - Contrib Modules.md | 2 |
4 files changed, 26 insertions, 7 deletions
diff --git a/contrib/scalapblib/src/ScalaPBModule.scala b/contrib/scalapblib/src/ScalaPBModule.scala index 00b977ce..4e663587 100644 --- a/contrib/scalapblib/src/ScalaPBModule.scala +++ b/contrib/scalapblib/src/ScalaPBModule.scala @@ -29,6 +29,8 @@ trait ScalaPBModule extends ScalaModule { def scalaPBSingleLineToProtoString: T[Boolean] = T { false } + def scalaPBProtocPath: T[Option[String]] = T { None } + def scalaPBSources: Sources = T.sources { millSourcePath / 'protobuf } @@ -63,6 +65,7 @@ trait ScalaPBModule extends ScalaModule { ScalaPBWorkerApi.scalaPBWorker .compile( scalaPBClasspath().map(_.path), + scalaPBProtocPath(), scalaPBSources().map(_.path), scalaPBOptions(), T.ctx().dest) diff --git a/contrib/scalapblib/src/ScalaPBWorker.scala b/contrib/scalapblib/src/ScalaPBWorker.scala index 125cd3fd..49c7492f 100644 --- a/contrib/scalapblib/src/ScalaPBWorker.scala +++ b/contrib/scalapblib/src/ScalaPBWorker.scala @@ -11,7 +11,7 @@ class ScalaPBWorker { private var scalaPBInstanceCache = Option.empty[(Long, ScalaPBWorkerApi)] - private def scalaPB(scalaPBClasspath: Agg[os.Path]) = { + private def scalaPB(scalaPBClasspath: Agg[os.Path], protocPath: Option[String]) = { val classloaderSig = scalaPBClasspath.map(p => p.toString().hashCode + os.mtime(p)).sum scalaPBInstanceCache match { case Some((sig, instance)) if sig == classloaderSig => instance @@ -23,15 +23,13 @@ class ScalaPBWorker { val instance = new ScalaPBWorkerApi { override def compileScalaPB(source: File, scalaPBOptions: String, generatedDirectory: File) { val opts = if (scalaPBOptions.isEmpty) "" else scalaPBOptions + ":" - mainMethod.invoke( - null, - Array( + val args = protocPath.map(path => s"--protoc=$path").toList ++ List( "--throw", s"--scala_out=${opts}${generatedDirectory.getCanonicalPath}", s"--proto_path=${source.getParentFile.getCanonicalPath}", source.getCanonicalPath ) - ) + mainMethod.invoke(null, args.toArray) } } scalaPBInstanceCache = Some((classloaderSig, instance)) @@ -39,9 +37,9 @@ class ScalaPBWorker { } } - def compile(scalaPBClasspath: Agg[os.Path], scalaPBSources: Seq[os.Path], scalaPBOptions: String, dest: os.Path) + def compile(scalaPBClasspath: Agg[os.Path], protocPath: Option[String], scalaPBSources: Seq[os.Path], scalaPBOptions: String, dest: os.Path) (implicit ctx: mill.api.Ctx): mill.api.Result[PathRef] = { - val compiler = scalaPB(scalaPBClasspath) + val compiler = scalaPB(scalaPBClasspath, protocPath) def compileScalaPBDir(inputDir: os.Path) { // ls throws if the path doesn't exist diff --git a/contrib/scalapblib/test/src/TutorialTests.scala b/contrib/scalapblib/test/src/TutorialTests.scala index fe0ce8d5..35ad69e0 100644 --- a/contrib/scalapblib/test/src/TutorialTests.scala +++ b/contrib/scalapblib/test/src/TutorialTests.scala @@ -24,6 +24,12 @@ object TutorialTests extends TestSuite { } } + object TutorialWithProtoc extends TutorialBase { + object core extends TutorialModule { + override def scalaPBProtocPath = Some("/dev/null") + } + } + val resourcePath: os.Path = os.pwd / 'contrib / 'scalapblib / 'test / 'protobuf / 'tutorial def protobufOutPath(eval: TestEvaluator): os.Path = @@ -108,5 +114,15 @@ object TutorialTests extends TestSuite { // assert(unchangedEvalCount == 0) // } } + + 'useExternalProtocCompiler - { + /* This ensure that the `scalaPBProtocPath` is properly used. + * As the given path is incorrect, the compilation should fail. + */ + 'calledWithWrongProtocFile - workspaceTest(TutorialWithProtoc) { eval => + val result = eval.apply(TutorialWithProtoc.core.compileScalaPB) + assert(result.isLeft) + } + } } } diff --git a/docs/pages/9 - Contrib Modules.md b/docs/pages/9 - Contrib Modules.md index 40544d86..81607b71 100644 --- a/docs/pages/9 - Contrib Modules.md +++ b/docs/pages/9 - Contrib Modules.md @@ -564,6 +564,8 @@ example/ * scalaPBSingleLineToProtoString - A `Boolean` option which determines whether the generated `.toString` methods should use a single line format. +* scalaPBProtocPath - A `Option[Path]` option which determines the protoc compiler to use. If `None`, a java embedded protoc will be used, if set to `Some` path, the given binary is used. + If you'd like to configure the options that are passed to the ScalaPB compiler directly, you can override the `scalaPBOptions` task, for example: ```scala |