summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/Process.scala
blob: 083b25f2a1b3ac66c92a7a838ec3879c5c92958a (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 */

package scala.tools.nsc
package io

import concurrent.ThreadRunner
import scala.annotation.tailrec
import scala.util.Properties.{ isWin, isMac, lineSeparator }
import scala.util.control.Exception.catching
import java.lang.{ Process => JProcess, ProcessBuilder => JProcessBuilder }
import java.io.{ IOException, InputStream, OutputStream, BufferedReader, InputStreamReader, PrintWriter, File => JFile }
import java.util.concurrent.LinkedBlockingQueue

/** The <code>Process</code> object contains convenience functions
 *  for running external processes.
 *
 *  An example usage:
 *  <pre>
 *    io.Process("ls", cwd = io.File("/")) foreach println
 *  </pre>
 *
 *  See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4109888
 *  for a dated list of the many obstacles to a clean interface.
 *
 *  This is not finished!! Do not rely upon it yet.
 *
 *  TODO - remove requirement that process complete before we
 *  can get an iterator.
 *
 *  @author   Paul Phillips
 *  @since    2.8
 */

object Process
{
  def javaVmArguments: List[String] = {
    import collection.JavaConversions._

    java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toList
  }
  lazy val runtime = Runtime.getRuntime()

  class Pipe[T](xs: Seq[T], stringify: T => String) {
    def |(cmd: String): Seq[String] = {
      val p = Process(cmd)
      xs foreach (x => p.stdin println stringify(x))
      p.stdin.close()
      p.stdout.toList
    }
  }

  object Pipe {
    /* After importing this implicit you can say for instance
     *   xs | "grep foo" | "grep bar"
     * and it will execute shells and pipe input/output.  You can
     * also implicitly or explicitly supply a function which translates
     * the opening sequence into Strings; if none is given toString is used.
     *
     * Also, once you use :sh in the repl, this is auto-imported.
     */
    implicit def seqToPipelinedProcess[T]
      (xs: Seq[T])
      (implicit stringify: T => String = (x: T) => x.toString): Pipe[T] =
    {
      new Pipe(xs, stringify)
    }
  }

  private[Process] class ProcessBuilder(val pb: JProcessBuilder) {
    def this(cmd: String*) = this(new JProcessBuilder(cmd: _*))
    def start() = new Process(() => pb.start())

    def withOnlyEnv(env: Map[String, String]): this.type = {
      pb.environment.clear()
      withEnv(env)
    }

    def withEnv(env: Map[String,String]): this.type = {
      if (env != null) {
        val jmap = pb.environment()
        for ((k, v) <- env) jmap.put(k, v)
      }
      this
    }

    def withCwd(cwd: Path): this.type = {
      if (cwd != null)
        pb directory cwd.jfile

      this
    }
    def withRedirectedErrorStream(merged: Boolean): this.type = {
      pb redirectErrorStream merged
      this
    }

    override def toString() = "ProcessBuilder(%s)" format pb.command()
  }

  // This can be fleshed out if more variations come up
  private val shell: String => Array[String] =
    if (isWin) Array("cmd.exe", "/C", _)
    else Array("sh", "-c", _)

  /** Executes the given command line in a shell.
   *
   *  @param    command   the command line
   *  @return             a Process object
   */
  def apply(
    command: String,
    env: Map[String, String] = null,
    cwd: Path = null,
    redirect: Boolean = false
  ): Process =
      exec(shell(command), env, cwd, redirect)

  /** Executes the given command line.
   *
   *  @param    command   the command line
   *  @return             a Process object
   */
  def exec(
    command: Seq[String],
    env: Map[String, String] = null,
    cwd: Path = null,
    redirect: Boolean = false
  ): Process =
      new ProcessBuilder(command: _*) withEnv env withCwd cwd withRedirectedErrorStream redirect start
}
import Process._

class Process(processCreator: () => JProcess) extends Iterable[String] {
  lazy val process = processCreator()

  def exitValue(): Option[Int] =
    catching(classOf[IllegalThreadStateException]) opt process.exitValue()

  def waitFor() = process.waitFor()
  def destroy() = process.destroy()
  def rerun()   = new Process(processCreator)

  def slurp()   = _out.slurp()
  def stdout    = iterator
  def iterator  = _out.iterator
  def stderr    = _err.iterator
  lazy val stdin = new PrintWriter(_in, true)

  class StreamedConsumer(in: InputStream) extends Thread with Iterable[String] {
    private val queue   = new LinkedBlockingQueue[String]
    private val reader  = new BufferedReader(new InputStreamReader(in))

    private def finish() {
      // make sure this thread is complete
      join()
    }

    def slurp(): String = {
      finish()
      queue.toArray map (_ + lineSeparator) mkString
    }

    def iterator = {
      finish()
      new Iterator[String] {
        val it = queue.iterator()
        def hasNext = it.hasNext
        def next = it.next
      }
    }
    override final def run() {
      @tailrec def loop() {
        reader.readLine match {
          case null =>
            reader.close()
          case x    =>
            queue put x
            loop()
        }
      }

      try loop()
      catch { case _: IOException => () }
    }
  }

  private val _err = createConsumer(process.getErrorStream)
  private val _out = createConsumer(process.getInputStream)
  private val _in  = process.getOutputStream()

  private def createConsumer(in: InputStream) = {
    val t = new StreamedConsumer(in)
    t setDaemon true
    t.start()
    t
  }

  override def toString() = "Process(%s)" format process.toString()
}