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,
|
inputTaskDynMacroImpl,
|
||||||
inputTaskMacroImpl,
|
inputTaskMacroImpl,
|
||||||
taskDynMacroImpl,
|
taskDynMacroImpl,
|
||||||
|
taskIfMacroImpl,
|
||||||
taskMacroImpl
|
taskMacroImpl
|
||||||
}
|
}
|
||||||
import std._
|
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 inputTask[T](t: T): Def.Initialize[InputTask[T]] = macro inputTaskMacroImpl[T]
|
||||||
def inputTaskDyn[T](t: Def.Initialize[Task[T]]): Def.Initialize[InputTask[T]] =
|
def inputTaskDyn[T](t: Def.Initialize[Task[T]]): Def.Initialize[InputTask[T]] =
|
||||||
macro inputTaskDynMacroImpl[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](
|
private[sbt] def selectITask[A, B](
|
||||||
fab: Initialize[Task[Either[A, B]]],
|
fab: Initialize[Task[Either[A, B]]],
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,21 @@ object TaskMacro {
|
||||||
Instance.idTransform[c.type]
|
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. */
|
/** Implementation of := macro for settings. */
|
||||||
def settingAssignMacroImpl[T: c.WeakTypeTag](
|
def settingAssignMacroImpl[T: c.WeakTypeTag](
|
||||||
c: blackbox.Context
|
c: blackbox.Context
|
||||||
|
|
|
||||||
|
|
@ -2128,9 +2128,10 @@ object Defaults extends BuildCommon {
|
||||||
)
|
)
|
||||||
|
|
||||||
def dependencyResolutionTask: Def.Initialize[Task[DependencyResolution]] =
|
def dependencyResolutionTask: Def.Initialize[Task[DependencyResolution]] =
|
||||||
Def.ifS(useCoursier.toTask)(Def.task { CoursierDependencyResolution(csrConfiguration.value) })(
|
Def.taskIf {
|
||||||
Def.task { IvyDependencyResolution(ivyConfiguration.value, CustomHttp.okhttpClient.value) }
|
if (useCoursier.value) Def.task { CoursierDependencyResolution(csrConfiguration.value) } else
|
||||||
)
|
Def.task { IvyDependencyResolution(ivyConfiguration.value, CustomHttp.okhttpClient.value) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object Classpaths {
|
object Classpaths {
|
||||||
|
|
@ -2947,14 +2948,18 @@ object Classpaths {
|
||||||
publishTask(config)
|
publishTask(config)
|
||||||
|
|
||||||
def publishTask(config: TaskKey[PublishConfiguration]): Initialize[Task[Unit]] =
|
def publishTask(config: TaskKey[PublishConfiguration]): Initialize[Task[Unit]] =
|
||||||
Def.ifS((publish / skip).toTask)(Def.task {
|
Def.taskIf {
|
||||||
val s = streams.value
|
if ((publish / skip).value)
|
||||||
val ref = thisProjectRef.value
|
Def.task {
|
||||||
s.log.debug(s"Skipping publish* for ${ref.project}")
|
val s = streams.value
|
||||||
})(Def.task {
|
val ref = thisProjectRef.value
|
||||||
val s = streams.value
|
s.log.debug(s"Skipping publish* for ${ref.project}")
|
||||||
IvyActions.publish(ivyModule.value, config.value, s.log)
|
} else
|
||||||
}) tag (Tags.Publish, Tags.Network)
|
Def.task {
|
||||||
|
val s = streams.value
|
||||||
|
IvyActions.publish(ivyModule.value, config.value, s.log)
|
||||||
|
}
|
||||||
|
} tag (Tags.Publish, Tags.Network)
|
||||||
|
|
||||||
val moduleIdJsonKeyFormat: sjsonnew.JsonKeyFormat[ModuleID] =
|
val moduleIdJsonKeyFormat: sjsonnew.JsonKeyFormat[ModuleID] =
|
||||||
new sjsonnew.JsonKeyFormat[ModuleID] {
|
new sjsonnew.JsonKeyFormat[ModuleID] {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,10 @@ lazy val root = (project in file("."))
|
||||||
condition := true,
|
condition := true,
|
||||||
trueAction := { IO.write(output.value, s"true\n", append = true) },
|
trueAction := { IO.write(output.value, s"true\n", append = true) },
|
||||||
falseAction := { IO.write(output.value, s"false\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") := {
|
TaskKey[Unit]("check") := {
|
||||||
val lines = IO.read(output.value).linesIterator.toList
|
val lines = IO.read(output.value).linesIterator.toList
|
||||||
assert(lines == List("true"))
|
assert(lines == List("true"))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue