diff --git a/run/src/main/scala/sbt/Fork.scala b/run/src/main/scala/sbt/Fork.scala index 8b88c46a8..1b3a8eed8 100644 --- a/run/src/main/scala/sbt/Fork.scala +++ b/run/src/main/scala/sbt/Fork.scala @@ -23,22 +23,60 @@ trait ForkScalaRun extends ForkScala def workingDirectory: Option[File] def runJVMOptions: Seq[String] } + +/** Configures forking. +* +* @param javaHome The Java installation to use. If not defined, the Java home for the current process is used. +* @param outputStrategy Configures the forked standard output and error streams. If not defined, StdoutOutput is used, which maps the forked output to the output of this process and the forked error to the error stream of the forking process. +* @param bootJars The list of jars to put on the forked boot classpath. By default, this is empty. +* @param workingDirectory The directory to use as the working directory for the forked process. By default, this is the working directory of the forking process. +* @param runJVMOptions The options to prepend to all user-specified arguments. By default, this is empty. +* @param connectInput If true, the standard input of the forked process is connected to the standard input of this process. Otherwise, it is connected to an empty input stream. Connecting input streams can be problematic, especially on versions before Java 7. +* @param envVars The environment variables to provide to the forked process. By default, none are provided. +*/ final case class ForkOptions(javaHome: Option[File] = None, outputStrategy: Option[OutputStrategy] = None, bootJars: Seq[File] = Nil, workingDirectory: Option[File] = None, runJVMOptions: Seq[String] = Nil, connectInput: Boolean = false, envVars: Map[String,String] = Map.empty) extends ForkScalaRun { @deprecated("Use bootJars.", "0.13.0") def scalaJars: Iterable[File] = bootJars } +/** Configures where the standard output and error streams from a forked process go.*/ sealed abstract class OutputStrategy + +/** Configures the forked standard output to go to standard output of this process and +* for the forked standard error to go to the standard error of this process. */ case object StdoutOutput extends OutputStrategy + +/** Logs the forked standard output at the `info` level and the forked standard error at the `error` level. +* The output is buffered until the process completes, at which point the logger flushes it (to the screen, for example). */ case class BufferedOutput(logger: Logger) extends OutputStrategy + +/** Logs the forked standard output at the `info` level and the forked standard error at the `error` level. */ case class LoggedOutput(logger: Logger) extends OutputStrategy + +/** Configures the forked standard output to be sent to `output` and the forked standard error +* to be sent to the standard error of this process.*/ case class CustomOutput(output: OutputStream) extends OutputStrategy import java.lang.{ProcessBuilder => JProcessBuilder} + +/** Represents a commad that can be forked. +* +* @param commandName The java-like binary to fork. This is expected to exist in bin/ of the Java home directory. +* @param runnerClass If Some, this will be prepended to the `arguments` passed to the `apply` or `fork` methods. +*/ sealed class Fork(val commandName: String, val runnerClass: Option[String]) { + /** Forks the configured process, waits for it to complete, and returns the exit code. + * The command executed is the `commandName` defined for this Fork instance. + * It is configured according to `config`. + * If `runnerClass` is defined for this Fork instance, it is prepended to `arguments` to define the arguments passed to the forked command. */ def apply(config: ForkOptions, arguments: Seq[String]): Int = fork(config, arguments).exitValue() + + /** Forks the configured process and returns a `Process` that can be used to wait for completion or to terminate the forked process. + * The command executed is the `commandName` defined for this Fork instance. + * It is configured according to `config`. + * If `runnerClass` is defined for this Fork instance, it is prepended to `arguments` to define the arguments passed to the forked command. */ def fork(config: ForkOptions, arguments: Seq[String]): Process = { import config.{envVars => env, _}