I would like to redefine an sbt task conditional on the value of a setting. In the default case, it should execute the original task, in the other case, I want to run a custom task.
Given a task and setting:
val someSetting = Def.settingKey[Boolean]("some setting")
val fooTask = Def.taskKey[String]("foo task")
someSetting := true
fooTask := {
println("** foo effect **")
"foo value"
}
Trying to redefine fooTask:
fooTask := {
if( someSetting.value ) fooTask
else Def.task("whatever")
}.value
gives a compile error:
build.sbt:39: error: Illegal dynamic dependency
if( someSetting.value ) fooTask
^
Using Def.task
the task compiles ...
fooTask := Def.task {
if( someSetting.value ) fooTask.value
else Def.task("whatever").value
}.value
... but gets executed even if someSetting
is false!
> set someSetting := false
[info] Defining *:someSetting
[info] The new value will be used by *:fooTask
[info] Reapplying settings...
> fooTask
** foo effect **
[success] Total time: 1 s, completed Mar 20, 2017 11:40:13 PM
Trying to use Def.taskDyn
instead ...
fooTask := Def.taskDyn {
if( someSetting.value ) fooTask
else Def.task("whatever")
}.value
... results in "References to undefined settings at runtime":
> set someSetting := true
[info] Defining *:someSetting
[info] The new value will be used by *:fooTask
[info] Reapplying settings...
[info] Set current project to sbt-task-games (in build file:/Users/jast/playspace/untitled8/)
> fooTask
[trace] Stack trace suppressed: run last *:fooTask for the full output.
[error] (*:fooTask) sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
[error] setting(ScopedKey(Scope(Select(ProjectRef(file:/Users/jast/playspace/untitled8/,untitled8)),Global,Global,Global),fooTask)) at RangePosition(/Users/jast/playspace/untitled8/build.sbt,LineRange(43,47)) referenced from ScopedKey(Scope(Select(ProjectRef(file:/Users/jast/playspace/untitled8/,untitled8)),Global,Global,Global),fooTask)
[error] Total time: 0 s, completed Mar 20, 2017 11:49:59 PM
You need to "save" a reference to the actual task before returning a task that executes it. This is what taskValue
is for:
fooTask := Def.taskDyn {
val default = fooTask.taskValue
if (someSetting.value) Def.task(default.value)
else Def.task("whatever")
}.value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With