|
8 | 8 | package sbt
|
9 | 9 |
|
10 | 10 | import java.io.File
|
| 11 | +import java.io.PrintWriter |
11 | 12 | import java.lang.ProcessBuilder.Redirect
|
12 | 13 | import scala.sys.process.Process
|
13 | 14 | import OutputStrategy._
|
14 | 15 | import sbt.internal.util.{ RunningProcesses, Util }
|
15 | 16 | import Util.{ AnyOps, none }
|
16 | 17 |
|
17 | 18 | import java.lang.{ ProcessBuilder => JProcessBuilder }
|
| 19 | +import java.util.Locale |
18 | 20 |
|
19 | 21 | /**
|
20 | 22 | * Represents a command that can be forked.
|
@@ -57,7 +59,11 @@ final class Fork(val commandName: String, val runnerClass: Option[String]) {
|
57 | 59 | (classpathEnv map { value =>
|
58 | 60 | Fork.ClasspathEnvKey -> value
|
59 | 61 | })
|
60 |
| - val jpb = new JProcessBuilder(command.toArray: _*) |
| 62 | + val jpb = |
| 63 | + if (Fork.shouldUseArgumentsFile(options)) |
| 64 | + new JProcessBuilder(executable, Fork.createArgumentsFile(options)) |
| 65 | + else |
| 66 | + new JProcessBuilder(command.toArray: _*) |
61 | 67 | workingDirectory foreach (jpb directory _)
|
62 | 68 | environment foreach { case (k, v) => jpb.environment.put(k, v) }
|
63 | 69 | if (connectInput) {
|
@@ -125,4 +131,57 @@ object Fork {
|
125 | 131 | val home = javaHome.getOrElse(new File(System.getProperty("java.home")))
|
126 | 132 | new File(new File(home, "bin"), name)
|
127 | 133 | }
|
| 134 | + |
| 135 | + /* copied from SysProp.scala for consistency while avoiding |
| 136 | + * introducing a circular dependency |
| 137 | + */ |
| 138 | + private def parseBoolean(value: String): Option[Boolean] = |
| 139 | + value.toLowerCase(Locale.ENGLISH) match { |
| 140 | + case "1" | "always" | "true" => Some(true) |
| 141 | + case "0" | "never" | "false" => Some(false) |
| 142 | + case "auto" => None |
| 143 | + case _ => None |
| 144 | + } |
| 145 | + private def booleanOpt(name: String): Option[Boolean] = |
| 146 | + sys.props.get(name) match { |
| 147 | + case Some(x) => parseBoolean(x) |
| 148 | + case _ => |
| 149 | + sys.env.get(name.toUpperCase(Locale.ENGLISH).replace('.', '_')) match { |
| 150 | + case Some(x) => parseBoolean(x) |
| 151 | + case _ => None |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** Use an arguments file if: |
| 156 | + * - we are on jdk >= 9 |
| 157 | + * - sbt.argfile is unset or not falsy |
| 158 | + * - the command line length would exceed MaxConcatenatedOptionLength |
| 159 | + */ |
| 160 | + private def shouldUseArgumentsFile(options: Seq[String]): Boolean = |
| 161 | + (sys.props.getOrElse("java.vm.specification.version", "1").toFloat >= 9.0) && |
| 162 | + booleanOpt("sbt.argsfile").getOrElse(true) && |
| 163 | + (options.mkString.length > MaxConcatenatedOptionLength) |
| 164 | + |
| 165 | + /** |
| 166 | + * Create an arguments file from a sequence of command line arguments |
| 167 | + * by quoting each argument to a line with escaped backslashes |
| 168 | + * |
| 169 | + * @param options command line options to write to the args file |
| 170 | + * @return |
| 171 | + */ |
| 172 | + private def createArgumentsFile(options: Seq[String]): String = { |
| 173 | + val file = File.createTempFile(s"sbt-args", ".tmp") |
| 174 | + file.deleteOnExit() |
| 175 | + |
| 176 | + val pw = new PrintWriter(file) |
| 177 | + options.foreach { option => |
| 178 | + pw.write("\"") |
| 179 | + pw.write(option.replace("\\", "\\\\")) |
| 180 | + pw.write("\"") |
| 181 | + pw.write(System.lineSeparator()) |
| 182 | + } |
| 183 | + pw.flush() |
| 184 | + pw.close() |
| 185 | + s"@${file.getAbsolutePath}" |
| 186 | + } |
128 | 187 | }
|
0 commit comments