As Daniel C. Sobral mentioned, parsed
and evaluated
are macros, defined in InputWrapper
.
Since they are executed at compile time, and the arguments are retrieved at runtime, they do not mix well. In particular, the value of args is only really defined at runtime and cannot be retrieved by the evaluated
macro.
EDIT: After a chat session with the OP, I've determined that the aim he had was a shortcut for writing myTask Foo bar
instead of testOnly *Foo* -- --tests=*bar*
, I've updated my answer accordingly.
Updated answer
As discussed, since you basically want a "macro" for writing myTask Foo bar
instead of testOnly *Foo* -- --tests=*bar*
, here's my solution:
val filtersParser = {
import complete.DefaultParsers._
(token(Space) ~> token(StringBasic, "<classFilter>")) ~
(token(Space) ~> token(StringBasic, "<methodFilter>"))
}
lazy val testFiltered = inputKey[Unit]("runs test methods matching *<methodFilter>* within classes matching *<classFilter>*")
testFiltered.in(Test) := Def.inputTaskDyn {
val (classFilter, methodFilter) = filtersParser.parsed
runTestsFiltered(classFilter, methodFilter)
}.evaluated
def runTestsFiltered(classFilter: String, methodFilter: String) = Def.taskDyn {
(testOnly in Test).toTask(s" *$classFilter* -- --tests *$methodFilter*")
}
In more detail
You need a custom parser to retrieve the two arguments you're expecting. This is achieved with the following code, which basically defines two groups, "chomping" both spaces without remembering them, and two StringBasic
arguments, which are the result of the parser (filtersParser
is of type Parser[(String, String)]
)
val filtersParser = {
import complete.DefaultParsers._
(token(Space) ~> token(StringBasic, "<classFilter>")) ~
(token(Space) ~> token(StringBasic, "<methodFilter>"))
}
Then you need an input task to use the results of the parser and forward them to the test framework.
This is done in the next snippet (if someone more knowledgeable than me wishes to chime in on the subtleties of using an inputTaskDyn
, I'll gladly be enlightened :) ). Do note the definition of the scope of the task .in(Test)
which grants access to the test dependencies.
lazy val testFiltered = inputKey[Unit]("runs test methods matching *<methodFilter>* within classes matching *<classFilter>*")
testFiltered.in(Test) := Def.inputTaskDyn {
val (classFilter, methodFilter) = filtersParser.parsed
runTestsFiltered(classFilter, methodFilter)
}.evaluated
And the last bit of code simply forwards the arguments to the pre-existing testOnly
task:
def runTestsFiltered(classFilter: String, methodFilter: String) = Def.taskDyn {
(testOnly in Test).toTask(s" *$classFilter* -- --tests *$methodFilter*")
}
Previous answer
However, you should be able to go around it by splitting definition and usage in two tasks:
import sbt._
import complete.DefaultParsers._
lazy val loadArgTask = inputKey[Unit]("loads and transforms argument")
lazy val runStuff = taskKey[Unit]("Runs some stuff")
lazy val loadArgIntoPropertyTask: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val myArg = (token(Space) ~> token(StringBasic, "<myArg>")).parsed
System.setProperty("myArg", myArg + "foo")
}
loadArgTask <<= loadArgIntoPropertyTask
runStuff := {
println(System.getProperty("myArg"))
}
Which can be used as follows
> loadArgTask orange
[success] Total time: 0 s, completed [...]
> runStuff
orangefoo
[success] Total time: 0 s, completed [...]
parsed
,evaluated
, and the like are macros, I think. Possibly, the way to get around the problem is to use the non-macro expansion of this stuff. And, no, I don't know what that might be.