From b5194aab6f1f57aff6e4538acaf91245fdf15039 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Thu, 15 Jun 2017 22:34:42 -0400 Subject: add process library with extracted and new functions --- stage1/Stage1.scala | 1 + stage1/Stage1Lib.scala | 93 -------------------------------------------------- stage1/resolver.scala | 26 +++++--------- 3 files changed, 10 insertions(+), 110 deletions(-) (limited to 'stage1') diff --git a/stage1/Stage1.scala b/stage1/Stage1.scala index d9bde7c..99c7b1e 100644 --- a/stage1/Stage1.scala +++ b/stage1/Stage1.scala @@ -94,6 +94,7 @@ object Stage1{ stage2.listFiles ++ (stage2 / "plugins").listOrFail ++ (cbtHome / "libraries" / "eval").listOrFail + ++ (cbtHome / "libraries" / "process").listOrFail ).filter(_.isFile).filter(_.toString.endsWith(".scala")) val cls = this.getClass.getClassLoader.loadClass("cbt.NailgunLauncher") diff --git a/stage1/Stage1Lib.scala b/stage1/Stage1Lib.scala index 40b3fed..ab95a41 100644 --- a/stage1/Stage1Lib.scala +++ b/stage1/Stage1Lib.scala @@ -432,99 +432,6 @@ ${sourceFiles.sorted.mkString(" \\\n")} outputLastModified ) } - - def asyncPipeCharacterStreamSyncLines( inputStream: InputStream, outputStream: OutputStream, lock: AnyRef ): Thread = { - new Thread( - new Runnable{ - def run = { - val b = new BufferedInputStream( inputStream ) - Iterator.continually{ - b.read // block until and read next character - }.takeWhile(_ != -1).map{ c => - lock.synchronized{ // synchronize with other invocations - outputStream.write(c) - Iterator - .continually( b.read ) - .takeWhile( _ != -1 ) - .map{ c => - try{ - outputStream.write(c) - outputStream.flush - ( - c != '\n' // release lock when new line was encountered, allowing other writers to slip in - && b.available > 0 // also release when nothing is available to not block other outputs - ) - } catch { - case e: IOException if e.getMessage == "Stream closed" => false - } - } - .takeWhile(identity) - .length // force entire iterator - } - }.length // force entire iterator - } - } - ) - } - - def asyncPipeCharacterStream( inputStream: InputStream, outputStream: OutputStream, continue: => Boolean ) = { - new Thread( - new Runnable{ - def run = { - Iterator - .continually{ inputStream.read } - .takeWhile(_ != -1) - .map{ c => - try{ - outputStream.write(c) - outputStream.flush - true - } catch { - case e: IOException if e.getMessage == "Stream closed" => false - } - } - .takeWhile( identity ) - .takeWhile( _ => continue ) - .length // force entire iterator - } - } - ) - } - - def runWithIO( commandLine: Seq[String], directory: Option[File] = None ): ExitCode = { - val (out,err,in) = lib.getOutErrIn match { case (l,r, in) => (l.get,r.get, in) } - val pb = new ProcessBuilder( commandLine: _* ) - val exitCode = - if( !NailgunLauncher.runningViaNailgun ){ - pb.inheritIO.start.waitFor - } else { - val process = directory.map( pb.directory( _ ) ).getOrElse( pb ) - .redirectInput(ProcessBuilder.Redirect.PIPE) - .redirectOutput(ProcessBuilder.Redirect.PIPE) - .redirectError(ProcessBuilder.Redirect.PIPE) - .start - - val lock = new AnyRef - - val t1 = lib.asyncPipeCharacterStreamSyncLines( process.getErrorStream, err, lock ) - val t2 = lib.asyncPipeCharacterStreamSyncLines( process.getInputStream, out, lock ) - val t3 = lib.asyncPipeCharacterStream( System.in, process.getOutputStream, process.isAlive ) - - t1.start - t2.start - t3.start - - t1.join - t2.join - - val e = process.waitFor - System.err.println( scala.Console.RESET + "Please press ENTER to continue..." ) - t3.join - e - } - - ExitCode( exitCode ) - } } import scala.reflect._ diff --git a/stage1/resolver.scala b/stage1/resolver.scala index f4a9b13..13d4070 100644 --- a/stage1/resolver.scala +++ b/stage1/resolver.scala @@ -77,25 +77,16 @@ trait DependencyImplementation extends Dependency{ ) } */ - def fork = false - def runMain( className: String, args: Seq[String] ): ExitCode = { - if(fork){ - val java_exe = new File(System.getProperty("java.home")) / "bin" / "java" - lib.runWithIO( - java_exe.string +: "-cp" +: classpath.string +: className +: args - ) - } else { - lib.getMain( classLoader.loadClass( className ) )( args ) - } - } + def runMain( className: String, args: Seq[String] ): ExitCode = + lib.getMain( classLoader.loadClass( className ) )( args ) - def runMain( args: Seq[String] ): ExitCode = { - val c = mainClass.getOrElse( - throw new RuntimeException( "No main class found in " + this ) - ) - runMain( c.getName, args ) - } + def runMain( args: Seq[String] ): ExitCode = + runMain( mainClassOrFail.getName, args ) + + def mainClassOrFail = mainClass.getOrElse( + throw new RuntimeException( "No main class found in " + this ) + ) def mainClass = lib.pickOne( "Which one do you want to run?", @@ -209,6 +200,7 @@ case class CbtDependencies(cbtLastModified: Long, mavenCache: File, nailgunTarge stage1Dependency +: MavenResolver(cbtLastModified, mavenCache,mavenCentral).bind( MavenDependency("org.eclipse.jgit", "org.eclipse.jgit", "4.2.0.201601211800-r"), + MavenDependency("net.java.dev.jna", "jna-platform", "4.4.0"), MavenDependency("org.scala-lang","scala-compiler",constants.scalaVersion) ) ) -- cgit v1.2.3