|
Process
sbt has movedsbt has now completely moved to GitHub. See https://github.com/harrah/xsbt/wiki/Process. External ProcessesUsagesbt includes a process library to simplify working with external processes. To use it in your project definition, import the implicits defined in sbt.Process: import Process._ Note: the interpreter started by the console-project command imports the members of Process on startup. To run a command, follow it with an exclamation mark !: "find project -name *.jar" ! An implicit converts the String to sbt.ProcessBuilder, which defines the ! method. This method runs the constructed command, waits until the command completes, and returns the exit code. Alternatively, the run method defined on ProcessBuilder runs the command and returns an instance of sbt.Process, which can be used to destroy the process before it completes. With no arguments, the ! method sends output to standard output and and standard error. You can pass a Logger to the ! method to send output to the Logger: "find project -name *.jar" ! log Two alternative implicit conversions are from scala.xml.Elem or List[String] to sbt.ProcessBuilder. These are useful for constructing commands. An example of the first variant from the android plugin: <x> {dxPath.absolutePath} --dex --output={classesDexPath.absolutePath} {classesMinJarPath.absolutePath}</x> !If you need to set the working directory or modify the environment, use a java.lang.ProcessBuilder instead of a String: ( (new java.lang.ProcessBuilder("ls", "-l")) directory new File(System.getProperty("user.home")) ) ! logOperators are defined to combine commands. These operators start with # in order to keep the precedence the same and to separate them from the operators defined elsewhere in sbt for filters. In the following operator definitions, a and b are subcommands.
There are also operators defined for redirecting output to Files and input from Files and URLs. In the following definitions, url is an instance of URL and file is an instance of File.
There are some additional methods to get the output from a forked process into a String or the output lines as a Stream[String]. Here are some examples, but see the ProcessBuilder API documentation for details. val listed: String = "ls" !! val lines2: Stream[String] = "ls" lines_! Finally, there is a cat method to send the contents of Files and URLs to standard output. ExamplesThese examples assume the following imports: import java.io.File import java.io.URL import Process._ Download a URL to a File: new URL("http://databinder.net/dispatch/About") #> new File("About.html") !
or
new File("About.html") #< new URL("http://databinder.net/dispatch/About") !Copy a File: new File("About.html") #> new File("About_copy.html") !
or
new File("About_copy.html") #< new File("About.html") !Append the contents of a URL to a File after filtering through grep: new URL("http://databinder.net/dispatch/About") #> "grep JSON" #>> new File("About_JSON") !
or
new File("About_JSON") #<< ( "grep JSON" #< new URL("http://databinder.net/dispatch/About") ) !Search for uses of null in the source directory: "find src -name *.scala -exec grep null {} ;" #| "xargs test -z" #&& "echo null-free" #|| "echo null detected" !Use cat: val spde = new URL("http://technically.us/spde/About")
val dispatch = new URL("http://databinder.net/dispatch/About")
val build = new File("project/build.properties")
cat(spde, dispatch, build) #| "grep -i scala" !
| |
God, I want this on the standard library!
It now is in the standard library!
Jul 28, 2009 - Jun 20, 2011 :)