Skip to content

Commit 20c99d1

Browse files
committed
Add a method to run protoc with parameters given in a file.
1 parent 497f24e commit 20c99d1

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

bridge/src/main/scala/protocbridge/ProtocRunner.scala

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package protocbridge
22

3+
import java.nio.file.Files
34
import scala.io.Source
45
import scala.sys.process.Process
56
import scala.sys.process.ProcessLogger
@@ -74,4 +75,31 @@ object ProtocRunner {
7475
extraEnv: _*
7576
).!
7677
}
78+
79+
// Transforms the given protoc runner to a new runner that writes the
80+
// options into a temporary file and passes the file to `protoc` as an `@`
81+
// parameter.
82+
def withParametersAsFile[T](underlying: ProtocRunner[T]): ProtocRunner[T] =
83+
fromFunction { (args, extraEnv) =>
84+
{
85+
val argumentFile = Files.createTempFile("protoc-args-", ".txt")
86+
try {
87+
val writer = Files.newBufferedWriter(argumentFile)
88+
89+
try {
90+
args.foreach { arg =>
91+
writer.write(arg)
92+
writer.write('\n')
93+
}
94+
} finally {
95+
writer.close()
96+
}
97+
98+
val fileArgument = s"@${argumentFile.toString}"
99+
underlying.run(Seq(fileArgument), extraEnv)
100+
} finally {
101+
Files.delete(argumentFile)
102+
}
103+
}
104+
}
77105
}

bridge/src/test/scala/protocbridge/ProtocIntegrationSpec.scala

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object TestUtils {
6767
}
6868

6969
class ProtocIntegrationSpec extends AnyFlatSpec with Matchers {
70-
"ProtocBridge.run" should "invoke JVM and Java plugin properly" in {
70+
def invokeProtocProperly(runner: ProtocRunner[Int]) = {
7171
val protoFile =
7272
new File(getClass.getResource("/test.proto").getFile).getAbsolutePath
7373
val protoDir = new File(getClass.getResource("/").getFile).getAbsolutePath
@@ -77,7 +77,7 @@ class ProtocIntegrationSpec extends AnyFlatSpec with Matchers {
7777
(0 to 4).map(i => Files.createTempDirectory(s"testout$i").toFile())
7878

7979
ProtocBridge.execute(
80-
RunProtoc,
80+
runner,
8181
Seq(
8282
protocbridge.gens.java("3.8.0") -> javaOutDir,
8383
TestJvmPlugin -> testOutDirs(0),
@@ -111,6 +111,14 @@ class ProtocIntegrationSpec extends AnyFlatSpec with Matchers {
111111
readLines(new File(testOutDirs(0), "parameters.txt")) must be(Seq("Empty"))
112112
}
113113

114+
"ProtocBridge.run" should "invoke JVM and Java plugin properly" in {
115+
invokeProtocProperly(RunProtoc)
116+
}
117+
118+
"ProtocBridge.run" should "invoke JVM and Java plugin properly with options file" in {
119+
invokeProtocProperly(ProtocRunner.withParametersAsFile(RunProtoc))
120+
}
121+
114122
it should "not deadlock for highly concurrent invocations" in {
115123
val availableProcessors = Runtime.getRuntime.availableProcessors
116124
assert(

0 commit comments

Comments
 (0)