setting cwd and env in process builder

182 views
Skip to first unread message

Jeremy

unread,
Nov 4, 2009, 8:42:57 AM11/4/09
to simple-build-tool
Hi Mark,

We discussed this several months ago, but I never got around to
finalizing a patch. Here is a diff for sbt.Process that makes it
easier to set the working directory and environment of an external
process, which I hope you will consider merging into trunk. I have
also updated the code to use the CommandParser to parse command
strings. With this diff, one could say:
Process("command arg arg", someOtherDirectory) ! log
or
Process("cmd2 arg arg", dir, "CMD_HOME" -> "/usr/local/cmd",
CMD_OPTIONS" -> "-x9")
or
Process(List("cmd", "arg", "arg"), None, CMD_OPTIONS" -> "-x9")


Index: src/main/scala/sbt/Process.scala
===================================================================
--- src/main/scala/sbt/Process.scala (revision 1122)
+++ src/main/scala/sbt/Process.scala (working copy)
@@ -3,6 +3,7 @@
*/
package sbt

+import sbt.impl.CommandParser
import java.lang.{Process => JProcess, ProcessBuilder =>
JProcessBuilder}
import java.io.{Closeable, File, IOException}
import java.io.{BufferedReader, InputStream, InputStreamReader,
OutputStream, PipedInputStream, PipedOutputStream}
@@ -11,9 +12,28 @@
/** Methods for constructing simple commands that can then be
combined. */
object Process
{
- implicit def apply(command: String): ProcessBuilder = apply
(command.split("""\s+""")) // TODO: use CommandParser
- implicit def apply(command: Seq[String]): ProcessBuilder = apply(new
JProcessBuilder(command.toArray : _*))
- def apply(command: String, arguments: Seq[String]): ProcessBuilder =
apply(new JProcessBuilder((command :: arguments.toList).toArray : _*))
+ implicit def apply(command: String): ProcessBuilder = apply(command,
None)
+ implicit def apply(command: Seq[String]): ProcessBuilder = apply
(command.toArray, None)
+ def apply(command: String, arguments: Seq[String]): ProcessBuilder =
apply(command :: arguments.toList, None)
+ /** create ProcessBuilder with working dir set to path and extra
environment variables */
+ def apply(command: String, cwd: Path, extraEnv: (String,String)*):
ProcessBuilder = apply(command, cwd.asFile, extraEnv : _*)
+ /** create ProcessBuilder with working dir set to File and extra
environment variables */
+ def apply(command: String, cwd: File, extraEnv: (String,String)*):
ProcessBuilder =
+ apply(command, Some(cwd), extraEnv : _*)
+ /** create ProcessBuilder with working dir optionaly set to File and
extra environment variables */
+ def apply(command: String, cwd: Option[File], extraEnv:
(String,String)*): ProcessBuilder = {
+ CommandParser.parse(command) match {
+ case Left(errorMsg) => error(errorMsg)
+ case Right((cmd, args)) => apply(cmd :: args, cwd, extraEnv : _*)
+ }
+ }
+ /** create ProcessBuilder with working dir optionaly set to File and
extra environment variables */
+ def apply(command: Seq[String], cwd: Option[File], extraEnv:
(String,String)*): ProcessBuilder = {
+ val jpb = new JProcessBuilder(command.toArray : _*)
+ cwd.foreach(jpb directory _)
+ extraEnv.foreach { case (k, v) => jpb.environment.put(k, v) }
+ apply(jpb)
+ }
implicit def apply(builder: JProcessBuilder): ProcessBuilder = new
SimpleProcessBuilder(builder)
implicit def apply(file: File): FilePartialBuilder = new FileBuilder
(file)
implicit def apply(url: URL): URLPartialBuilder = new URLBuilder
(url)

Mark Harrah

unread,
Nov 4, 2009, 4:14:17 PM11/4/09
to simple-build-tool
Hi Jeremy,

Looks good, thanks for resubmitting it. I'll apply it when I get a
chance (it will also go to the split out process library on github).

Thanks,
Mark
Reply all
Reply to author
Forward
0 new messages