mirror of https://github.com/sbt/sbt.git
Def.taskIf macro
Def.taskIf accepts an if-expression or a block ending in an if-expression.
This commit is contained in:
parent
fc791cc23e
commit
c6f62293f1
|
|
@ -221,6 +221,7 @@ object Def extends Init[Scope] with TaskMacroExtra with InitializeImplicits {
|
|||
inputTaskDynMacroImpl,
|
||||
inputTaskMacroImpl,
|
||||
taskDynMacroImpl,
|
||||
taskIfMacroImpl,
|
||||
taskMacroImpl
|
||||
}
|
||||
import std._
|
||||
|
|
@ -234,6 +235,7 @@ object Def extends Init[Scope] with TaskMacroExtra with InitializeImplicits {
|
|||
def inputTask[T](t: T): Def.Initialize[InputTask[T]] = macro inputTaskMacroImpl[T]
|
||||
def inputTaskDyn[T](t: Def.Initialize[Task[T]]): Def.Initialize[InputTask[T]] =
|
||||
macro inputTaskDynMacroImpl[T]
|
||||
def taskIf[T](a: Def.Initialize[Task[T]]): Def.Initialize[Task[T]] = macro taskIfMacroImpl[T]
|
||||
|
||||
private[sbt] def selectITask[A, B](
|
||||
fab: Initialize[Task[Either[A, B]]],
|
||||
|
|
|
|||
|
|
@ -118,6 +118,21 @@ object TaskMacro {
|
|||
Instance.idTransform[c.type]
|
||||
)
|
||||
|
||||
def taskIfMacroImpl[A: c.WeakTypeTag](
|
||||
c: blackbox.Context
|
||||
)(a: c.Expr[Initialize[Task[A]]]): c.Expr[Initialize[Task[A]]] = {
|
||||
import c.universe._
|
||||
def mkIfS(cond: Tree, thenp: Tree, elsep: Tree): Tree =
|
||||
q"""Def.ifS(Def.task($cond))($thenp)($elsep)"""
|
||||
a.tree match {
|
||||
case Block(stat, If(cond, thenp, elsep)) =>
|
||||
c.Expr[Initialize[Task[A]]](mkIfS(Block(stat, cond), thenp, elsep))
|
||||
case If(cond, thenp, elsep) =>
|
||||
c.Expr[Initialize[Task[A]]](mkIfS(cond, thenp, elsep))
|
||||
case x => ContextUtil.unexpectedTree(x)
|
||||
}
|
||||
}
|
||||
|
||||
/** Implementation of := macro for settings. */
|
||||
def settingAssignMacroImpl[T: c.WeakTypeTag](
|
||||
c: blackbox.Context
|
||||
|
|
|
|||
|
|
@ -2128,9 +2128,10 @@ object Defaults extends BuildCommon {
|
|||
)
|
||||
|
||||
def dependencyResolutionTask: Def.Initialize[Task[DependencyResolution]] =
|
||||
Def.ifS(useCoursier.toTask)(Def.task { CoursierDependencyResolution(csrConfiguration.value) })(
|
||||
Def.task { IvyDependencyResolution(ivyConfiguration.value, CustomHttp.okhttpClient.value) }
|
||||
)
|
||||
Def.taskIf {
|
||||
if (useCoursier.value) Def.task { CoursierDependencyResolution(csrConfiguration.value) } else
|
||||
Def.task { IvyDependencyResolution(ivyConfiguration.value, CustomHttp.okhttpClient.value) }
|
||||
}
|
||||
}
|
||||
|
||||
object Classpaths {
|
||||
|
|
@ -2947,14 +2948,18 @@ object Classpaths {
|
|||
publishTask(config)
|
||||
|
||||
def publishTask(config: TaskKey[PublishConfiguration]): Initialize[Task[Unit]] =
|
||||
Def.ifS((publish / skip).toTask)(Def.task {
|
||||
val s = streams.value
|
||||
val ref = thisProjectRef.value
|
||||
s.log.debug(s"Skipping publish* for ${ref.project}")
|
||||
})(Def.task {
|
||||
val s = streams.value
|
||||
IvyActions.publish(ivyModule.value, config.value, s.log)
|
||||
}) tag (Tags.Publish, Tags.Network)
|
||||
Def.taskIf {
|
||||
if ((publish / skip).value)
|
||||
Def.task {
|
||||
val s = streams.value
|
||||
val ref = thisProjectRef.value
|
||||
s.log.debug(s"Skipping publish* for ${ref.project}")
|
||||
} else
|
||||
Def.task {
|
||||
val s = streams.value
|
||||
IvyActions.publish(ivyModule.value, config.value, s.log)
|
||||
}
|
||||
} tag (Tags.Publish, Tags.Network)
|
||||
|
||||
val moduleIdJsonKeyFormat: sjsonnew.JsonKeyFormat[ModuleID] =
|
||||
new sjsonnew.JsonKeyFormat[ModuleID] {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,10 @@ lazy val root = (project in file("."))
|
|||
condition := true,
|
||||
trueAction := { IO.write(output.value, s"true\n", append = true) },
|
||||
falseAction := { IO.write(output.value, s"false\n", append = true) },
|
||||
foo := (Def.ifS(condition)(trueAction)(falseAction)).value,
|
||||
foo := (Def.taskIf {
|
||||
if (condition.value) trueAction
|
||||
else falseAction
|
||||
}).value,
|
||||
TaskKey[Unit]("check") := {
|
||||
val lines = IO.read(output.value).linesIterator.toList
|
||||
assert(lines == List("true"))
|
||||
|
|
|
|||
Loading…
Reference in New Issue