Skip to content

Commit

Permalink
Use two named pipes with scalapbgen.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Dec 5, 2015
1 parent de53ada commit a9459ba
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,25 @@ object ProtocDriverFactory {
/** A driver that creates a named pipe and sets up a shell script as a protoc plugin */
class PosixProtocDriver extends ProtocDriver {
def buildRunner[A](runner: Seq[String] => A)(params: Seq[String]): A = {
val pipe = createPipe()
val sh = createShellScript(pipe)
val inputPipe = createPipe()
val outputPipe = createPipe()
val sh = createShellScript(inputPipe, outputPipe)
Future {
val fsin = Files.newInputStream(pipe)
val fsin = Files.newInputStream(inputPipe)
val response = Process.runWithInputStream(fsin)
val fsout = Files.newOutputStream(pipe)
fsin.close()

val fsout = Files.newOutputStream(outputPipe)
fsout.write(response.toByteArray)
fsout.close()
fsin.close()
}

try {
val args = Seq(s"--plugin=protoc-gen-scala=$sh") ++ params
runner(args)
} finally {
Files.delete(pipe)
Files.delete(inputPipe)
Files.delete(outputPipe)
Files.delete(sh)
}
}
Expand All @@ -75,12 +78,12 @@ class PosixProtocDriver extends ProtocDriver {
pipeName
}

private def createShellScript(tmpFile: Path): Path = {
private def createShellScript(inputPipe: Path, outputPipe: Path): Path = {
val scriptName = Process.createTempFile("",
s"""|#!/usr/bin/env sh
|set -e
|cat /dev/stdin > "$tmpFile"
|cat "$tmpFile"
|cat /dev/stdin > "$inputPipe"
|cat "$outputPipe"
""".stripMargin)
Files.setPosixFilePermissions(scriptName, Set(
PosixFilePermission.OWNER_EXECUTE,
Expand Down

0 comments on commit a9459ba

Please sign in to comment.