Skip to content

Instantly share code, notes, and snippets.

@everson
Created May 23, 2012 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everson/2777994 to your computer and use it in GitHub Desktop.
Save everson/2777994 to your computer and use it in GitHub Desktop.
How to create DSL in Scala for command lines with minimum boilerplate
package lib
import sys.process.{Process, stringToProcess}
class Shell {
var elems = Vector[String]()
var cwd = "/tmp"
def >(str: String) = elems :+= str
def run() = elems.map( cmd => {
val CdPattern = "cd (.*)".r
cmd match {
case CdPattern(dir) =>
cwd = dir
"directory changed to " + dir
case anyCmd => Process(anyCmd,new java.io.File(cwd))!!
}
}
)
}
trait ShellSupport extends DelayedInit{
var result = Vector[String]()
val $ = new Shell()
def delayedInit(x: => Unit) {
x
result = $.run()
}
}
package scripting
import lib.ShellSupport
class Turismo extends ShellSupport {
$> "rm -rf /tmp/scripting"
$> "mkdir /tmp/scripting"
$> "cd /tmp/scripting"
$> "mkdir oops"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment