-cp/-classpath option to 'apply' command

This commit is contained in:
Mark Harrah 2012-12-04 21:08:39 -05:00
parent 96ddf5b742
commit 8f25b07d71
4 changed files with 47 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package sbt
import complete.{DefaultParsers, HistoryCommands, Parser}
import classpath.ClasspathUtilities.toLoader
import DefaultParsers._
import Types.{const,idFun}
import Function.tupled
@ -79,11 +80,22 @@ object BasicCommands
}
def rebootParser(s: State) = token(Space ~> "full" ^^^ true) ?? false
def call = Command(ApplyCommand, Help.more(ApplyCommand, ApplyDetailed))(_ => spaceDelimited("<class name>")) { (state,args) =>
val loader = getClass.getClassLoader
def call = Command(ApplyCommand, Help.more(ApplyCommand, ApplyDetailed))(_ => callParser) { case (state,(cp,args)) =>
val parentLoader = getClass.getClassLoader
state.log.info("Applying State transformations " + args.mkString(", ") + (if(cp.isEmpty) "" else " from " + cp.mkString(File.separator)))
val loader = if(cp.isEmpty) parentLoader else toLoader(cp.map(f => new File(f)), parentLoader)
val loaded = args.map(arg => ModuleUtilities.getObject(arg, loader))
(state /: loaded) { case (s, obj: (State => State)) => obj(s) }
}
def callParser: Parser[(Seq[String], Seq[String])] = token(Space) ~> ((classpathOptionParser ?? Nil) ~ rep1sep(className, token(Space)))
private[this] def className: Parser[String] =
token(StringBasic.filter(s => !s.contains("-"), const("- not allowed in class name")), "<class name>")
private[this] def classpathOptionParser: Parser[Seq[String]] =
token( ("-cp" | "-classpath") ~> Space ) ~> rep1sep(classpathString, token(File.separatorChar)) <~ token(Space)
private[this] def classpathString: Parser[String] =
token(charClass(entryClass).+.string, "<classpath-entry>")
private[this] def entryClass(c: Char): Boolean =
c != File.separatorChar && !java.lang.Character.isWhitespace(c)
def exit = Command.command(TerminateAction, exitBrief, exitBrief ) ( _ exit true )

View File

@ -0,0 +1,13 @@
package demo
import sbt.{Command,State}
object AddNewCommand extends (State => State)
{
def apply(s: State): State = s ++ Seq(newCommand)
def newCommand = Command.command("newCommand") { (s: State) =>
println("This is a new command")
s
}
}

View File

@ -0,0 +1,9 @@
sbtPlugin := true
val copyOutputDir = taskKey[Unit]("Copies the compiled classes to a root-level directory")
copyOutputDir := {
val cd = (classDirectory in Compile).value
val to = baseDirectory.value / "out"
IO.copyDirectory(cd, to)
}

View File

@ -0,0 +1,11 @@
# compiles a new State => State instance
> compile
# puts the classes in a stable location (out/)
> copyOutputDir
# checks that `apply` can load from a specific classpath
> apply -cp out demo.AddNewCommand
# check that AddNewCommand was actually run
# If it was, it should have registered `newCommand`
> newCommand