Skip to content

Instantly share code, notes, and snippets.

@williamho
Created August 29, 2017 16:26
Show Gist options
  • Save williamho/b175bedb6ad2cdf54bd69b17c6ca9395 to your computer and use it in GitHub Desktop.
Save williamho/b175bedb6ad2cdf54bd69b17c6ca9395 to your computer and use it in GitHub Desktop.
Temporarily change an sbt setting and execute task
val greet = taskKey[Unit]("Says hello to you")
// Reverses the value of `name` and calls `greet`
val greetReverse = Command.command("greetReverse") { state =>
val extracted = Project.extract(state)
val newState = extracted.append(Seq(name := name.value.reverse), state)
Project.runTask(greet, newState)
state // Reset the state back to what it was originally
}
lazy val root = (project in file("."))
.settings(
name := "world",
commands ++= Seq(greetReverse),
greet := {
println("Hello " + name.value + "!")
}
)
/** sbt output:
*
* > greet
* Hello world!
* > greetReverse
* Hello dlrow!
* > greet
* Hello world!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment