Created
October 16, 2015 12:23
-
-
Save frgomes/f4ee0817081d4ad9023e to your computer and use it in GitHub Desktop.
Scala / SBT - Evaluation and task execution helpers
This file contains 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
trait EvaluationSupport { | |
import sbt._ | |
protected def fail(errorMessage: String, state: State): Nothing = { | |
state.log.error(errorMessage) | |
throw new IllegalArgumentException() | |
} | |
protected def log(implicit state: State) = state.log | |
// our version of http://stackoverflow.com/questions/25246920 | |
protected implicit class RichSettingKey[A](key: SettingKey[A]) { | |
def gimme(implicit c: Ctx): A = | |
gimme(c.pr, c.bs, c.state) | |
def gimme(pr: ProjectRef, bs: BuildStructure, state: State): A = | |
gimmeOpt(pr, bs) getOrElse { fail(s"Missing setting: ${key.key.label}", state) } | |
def gimmeOpt(implicit c: Ctx): Option[A] = | |
gimmeOpt(c.pr, c.bs) | |
def gimmeOpt(pr: ProjectRef, bs: BuildStructure): Option[A] = | |
key in pr get bs.data | |
} | |
protected implicit class RichTaskKey[A](key: TaskKey[A]) { | |
def run(implicit c: Ctx): A = | |
run(c.pr, c.bs, c.state) | |
def run(pr: ProjectRef, bs: BuildStructure, state: State): A = | |
runOpt(pr, bs, state).getOrElse { fail(s"Missing task key: ${key.key.label}", state) } | |
def runOpt(implicit c: Ctx): Option[A] = | |
runOpt(c.pr, c.bs, c.state) | |
def runOpt(pr: ProjectRef, bs: BuildStructure, state: State): Option[A] = | |
EvaluateTask(bs, key, state, pr).map(_._2) match { | |
case Some(Value(v)) => Some(v) | |
case _ => None | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment