diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-04 23:30:11 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-04 23:34:22 -0800 |
commit | 395a856d91b318957963ab24011862653452a101 (patch) | |
tree | a3472871359e026a1d05bef9c65e8b817d7c8ef8 /core/src/main/scala | |
parent | 9aed4601a04a48b3192d1871ff0ebb98fa352726 (diff) | |
download | mill-395a856d91b318957963ab24011862653452a101.tar.gz mill-395a856d91b318957963ab24011862653452a101.tar.bz2 mill-395a856d91b318957963ab24011862653452a101.zip |
Avoid `inheritIO` in `Jvm.subprocess`, and instead feed it into the proper `log.outputStream`
Diffstat (limited to 'core/src/main/scala')
-rw-r--r-- | core/src/main/scala/mill/eval/Evaluator.scala | 15 | ||||
-rw-r--r-- | core/src/main/scala/mill/modules/Jvm.scala | 37 | ||||
-rw-r--r-- | core/src/main/scala/mill/util/Logger.scala | 2 |
3 files changed, 50 insertions, 4 deletions
diff --git a/core/src/main/scala/mill/eval/Evaluator.scala b/core/src/main/scala/mill/eval/Evaluator.scala index 619b1521..96c868e9 100644 --- a/core/src/main/scala/mill/eval/Evaluator.scala +++ b/core/src/main/scala/mill/eval/Evaluator.scala @@ -170,7 +170,20 @@ class Evaluator(workspacePath: Path, targetDestPath.orNull, log ) - target.evaluate(args) + val out = System.out + val err = System.err + try{ + System.setErr(log.outputStream) + System.setOut(log.outputStream) + Console.withOut(log.outputStream){ + Console.withErr(log.outputStream){ + target.evaluate(args) + } + } + }finally{ + System.setErr(err) + System.setOut(out) + } } newResults(target) = res diff --git a/core/src/main/scala/mill/modules/Jvm.scala b/core/src/main/scala/mill/modules/Jvm.scala index a0f586c3..43382b8d 100644 --- a/core/src/main/scala/mill/modules/Jvm.scala +++ b/core/src/main/scala/mill/modules/Jvm.scala @@ -9,6 +9,7 @@ import mill.define.Task import mill.eval.PathRef import mill.util.Ctx +import scala.annotation.tailrec import scala.collection.mutable @@ -17,8 +18,40 @@ object Jvm { def subprocess(mainClass: String, classPath: Seq[Path], options: Seq[String] = Seq.empty, - workingDir: Path = ammonite.ops.pwd) = { - %("java", "-cp", classPath.mkString(":"), mainClass, options)(workingDir) + workingDir: Path = ammonite.ops.pwd) + (implicit ctx: Ctx) = { + val proc = + new java.lang.ProcessBuilder() + .directory(workingDir.toIO) + .command(Vector("java", "-cp", classPath.mkString(":"), mainClass) ++ options:_*) + .redirectOutput(ProcessBuilder.Redirect.PIPE) + .redirectError(ProcessBuilder.Redirect.PIPE) + .start() + + val stdout = proc.getInputStream + val stderr = proc.getErrorStream + val sources = Seq(stdout , stderr) + while( + // Process.isAlive doesn't exist on JDK 7 =/ + util.Try(proc.exitValue).isFailure || + stdout.available() > 0 || + stderr.available() > 0 + ){ + var readSomething = false + for (std <- sources){ + while (std.available() > 0){ + readSomething = true + val array = new Array[Byte](std.available()) + val actuallyRead = std.read(array) + ctx.log.outputStream.write(array, 0, actuallyRead) + } + } + // if we did not read anything sleep briefly to avoid spinning + if(!readSomething) + Thread.sleep(2) + } + + if (proc.exitValue() != 0) throw new InteractiveShelloutException() } private def createManifest(mainClass: Option[String]) = { diff --git a/core/src/main/scala/mill/util/Logger.scala b/core/src/main/scala/mill/util/Logger.scala index d998a601..ba8aa95f 100644 --- a/core/src/main/scala/mill/util/Logger.scala +++ b/core/src/main/scala/mill/util/Logger.scala @@ -12,7 +12,7 @@ trait Logger{ } object DummyLogger extends Logger{ - val outputStream = new PrintStream(new OutputStream { + object outputStream extends PrintStream(new OutputStream { def write(b: Int) = () }) def info(s: String) = () |