-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProtocRunner.scala
60 lines (51 loc) · 1.77 KB
/
ProtocRunner.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package protocbridge
import scala.io.Source
import scala.sys.process.Process
import scala.sys.process.ProcessLogger
abstract class ProtocRunner[ExitCode] {
self =>
def run(args: Seq[String], extraEnv: Seq[(String, String)]): ExitCode
/** Returns a new ProtocRunner that is executed after this value. The exit codes
* are combined into a tuple.
*/
def zip[E](
other: ProtocRunner[E]
): ProtocRunner[(ExitCode, E)] = ProtocRunner.fromFunction {
(args, extraEnv) => (run(args, extraEnv), other.run(args, extraEnv))
}
/** Returns a new ProtocRunner that maps the exit code of this runner. */
def map[E](f: ExitCode => E): ProtocRunner[E] = ProtocRunner.fromFunction {
(args, extraEnv) => f(run(args, extraEnv))
}
}
object ProtocRunner {
/** Makes a ProtocRunner that runs the given command line, but discards the
* extra environment. Exists only for backwards compatility.
*/
private[protocbridge] def apply[A](f: Seq[String] => A): ProtocRunner[A] =
new ProtocRunner[A] {
def run(args: Seq[String], extraEnv: Seq[(String, String)]): A = f(args)
}
def fromFunction[A](
f: (Seq[String], Seq[(String, String)]) => A
): ProtocRunner[A] =
new ProtocRunner[A] {
def run(args: Seq[String], extraEnv: Seq[(String, String)]): A =
f(args, extraEnv)
}
private def maybeNixDynamicLinker(): Option[String] =
sys.env.get("NIX_CC").map { nixCC =>
Source
.fromFile(nixCC + "/nix-support/dynamic-linker")
.mkString
.trim()
}
def apply(executable: String): ProtocRunner[Int] = ProtocRunner.fromFunction {
case (args, extraEnv) =>
Process(
command = (maybeNixDynamicLinker().toSeq :+ executable) ++ args,
cwd = None,
extraEnv: _*
).!
}
}