Created
May 23, 2012 21:37
-
-
Save everson/2777994 to your computer and use it in GitHub Desktop.
How to create DSL in Scala for command lines with minimum boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
based on SO question: http://stackoverflow.com/questions/10708136/how-to-create-dsl-in-scala-for-command-lines-with-minimum-extra-boilerplate