mirror of https://github.com/sbt/sbt.git
Merge pull request #1922 from dwijnand/removable
Add -= & --= for settings & tasks, dual of += & ++=.
This commit is contained in:
commit
082fab5d4b
|
|
@ -0,0 +1,20 @@
|
|||
package sbt
|
||||
|
||||
import scala.annotation.implicitNotFound
|
||||
|
||||
object Remove {
|
||||
@implicitNotFound(msg = "No implicit for Remove.Value[${A}, ${B}] found,\n so ${B} cannot be removed from ${A}")
|
||||
trait Value[A, B] extends Any {
|
||||
def removeValue(a: A, b: B): A
|
||||
}
|
||||
@implicitNotFound(msg = "No implicit for Remove.Values[${A}, ${B}] found,\n so ${B} cannot be removed from ${A}")
|
||||
trait Values[A, -B] extends Any {
|
||||
def removeValues(a: A, b: B): A
|
||||
}
|
||||
sealed trait Sequence[A, -B, T] extends Value[A, T] with Values[A, B]
|
||||
|
||||
implicit def removeSeq[T, V <: T]: Sequence[Seq[T], Seq[V], V] = new Sequence[Seq[T], Seq[V], V] {
|
||||
def removeValue(a: Seq[T], b: V): Seq[T] = a filterNot b.==
|
||||
def removeValues(a: Seq[T], b: Seq[V]): Seq[T] = a diff b
|
||||
}
|
||||
}
|
||||
|
|
@ -42,13 +42,19 @@ sealed abstract class SettingKey[T] extends ScopedTaskable[T] with KeyedInitiali
|
|||
final def ++=[U](vs: U)(implicit a: Append.Values[T, U]): Setting[T] = macro std.TaskMacro.settingAppendNImpl[T, U]
|
||||
final def <+=[V](v: Initialize[V])(implicit a: Append.Value[T, V]): Setting[T] = macro std.TaskMacro.settingAppend1Position[T, V]
|
||||
final def <++=[V](vs: Initialize[V])(implicit a: Append.Values[T, V]): Setting[T] = macro std.TaskMacro.settingAppendNPosition[T, V]
|
||||
final def -=[U](v: U)(implicit r: Remove.Value[T, U]): Setting[T] = macro std.TaskMacro.settingRemove1Impl[T, U]
|
||||
final def --=[U](vs: U)(implicit r: Remove.Values[T, U]): Setting[T] = macro std.TaskMacro.settingRemoveNImpl[T, U]
|
||||
final def ~=(f: T => T): Setting[T] = macro std.TaskMacro.settingTransformPosition[T]
|
||||
final def transform(f: T => T, source: SourcePosition): Setting[T] = set(scopedKey(f), source)
|
||||
|
||||
final def append1[V](v: Initialize[V], source: SourcePosition)(implicit a: Append.Value[T, V]): Setting[T] = make(v, source)(a.appendValue)
|
||||
final def appendN[V](vs: Initialize[V], source: SourcePosition)(implicit a: Append.Values[T, V]): Setting[T] = make(vs, source)(a.appendValues)
|
||||
|
||||
protected[this] def make[S](other: Initialize[S], source: SourcePosition)(f: (T, S) => T): Setting[T] = this.set((this, other)(f), source)
|
||||
final def remove1[V](v: Initialize[V], source: SourcePosition)(implicit r: Remove.Value[T, V]): Setting[T] = make(v, source)(r.removeValue)
|
||||
final def removeN[V](vs: Initialize[V], source: SourcePosition)(implicit r: Remove.Values[T, V]): Setting[T] = make(vs, source)(r.removeValues)
|
||||
|
||||
final def transform(f: T => T, source: SourcePosition): Setting[T] = set(scopedKey(f), source)
|
||||
|
||||
protected[this] def make[S](other: Initialize[S], source: SourcePosition)(f: (T, S) => T): Setting[T] = set((this, other)(f), source)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -67,10 +73,15 @@ sealed abstract class TaskKey[T] extends ScopedTaskable[T] with KeyedInitialize[
|
|||
def ++=[U](vs: U)(implicit a: Append.Values[T, U]): Setting[Task[T]] = macro std.TaskMacro.taskAppendNImpl[T, U]
|
||||
def <+=[V](v: Initialize[Task[V]])(implicit a: Append.Value[T, V]): Setting[Task[T]] = macro std.TaskMacro.taskAppend1Position[T, V]
|
||||
def <++=[V](vs: Initialize[Task[V]])(implicit a: Append.Values[T, V]): Setting[Task[T]] = macro std.TaskMacro.taskAppendNPosition[T, V]
|
||||
final def -=[U](v: U)(implicit r: Remove.Value[T, U]): Setting[Task[T]] = macro std.TaskMacro.taskRemove1Impl[T, U]
|
||||
final def --=[U](vs: U)(implicit r: Remove.Values[T, U]): Setting[Task[T]] = macro std.TaskMacro.taskRemoveNImpl[T, U]
|
||||
|
||||
def append1[V](v: Initialize[Task[V]], source: SourcePosition)(implicit a: Append.Value[T, V]): Setting[Task[T]] = make(v, source)(a.appendValue)
|
||||
def appendN[V](vs: Initialize[Task[V]], source: SourcePosition)(implicit a: Append.Values[T, V]): Setting[Task[T]] = make(vs, source)(a.appendValues)
|
||||
|
||||
final def remove1[V](v: Initialize[Task[V]], source: SourcePosition)(implicit r: Remove.Value[T, V]): Setting[Task[T]] = make(v, source)(r.removeValue)
|
||||
final def removeN[V](vs: Initialize[Task[V]], source: SourcePosition)(implicit r: Remove.Values[T, V]): Setting[Task[T]] = make(vs, source)(r.removeValues)
|
||||
|
||||
private[this] def make[S](other: Initialize[Task[S]], source: SourcePosition)(f: (T, S) => T): Setting[Task[T]] =
|
||||
set((this, other) { (a, b) => (a, b) map f.tupled }, source)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@ object TaskMacro {
|
|||
final val AssignInitName = "set"
|
||||
final val Append1InitName = "append1"
|
||||
final val AppendNInitName = "appendN"
|
||||
final val Remove1InitName = "remove1"
|
||||
final val RemoveNInitName = "removeN"
|
||||
final val TransformInitName = "transform"
|
||||
final val InputTaskCreateDynName = "createDyn"
|
||||
final val InputTaskCreateFreeName = "createFree"
|
||||
|
|
@ -136,29 +138,58 @@ object TaskMacro {
|
|||
def taskAppend1Impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(v: c.Expr[U])(a: c.Expr[Append.Value[T, U]]): c.Expr[Setting[Task[T]]] =
|
||||
{
|
||||
val init = taskMacroImpl[U](c)(v)
|
||||
val assign = appendMacroImpl(c)(init.tree, a.tree)(Append1InitName)
|
||||
c.Expr[Setting[Task[T]]](assign)
|
||||
val append = appendMacroImpl(c)(init.tree, a.tree)(Append1InitName)
|
||||
c.Expr[Setting[Task[T]]](append)
|
||||
}
|
||||
/** Implementation of += macro for settings. */
|
||||
def settingAppend1Impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(v: c.Expr[U])(a: c.Expr[Append.Value[T, U]]): c.Expr[Setting[T]] =
|
||||
{
|
||||
val init = SettingMacro.settingMacroImpl[U](c)(v)
|
||||
val assign = appendMacroImpl(c)(init.tree, a.tree)(Append1InitName)
|
||||
c.Expr[Setting[T]](assign)
|
||||
val append = appendMacroImpl(c)(init.tree, a.tree)(Append1InitName)
|
||||
c.Expr[Setting[T]](append)
|
||||
}
|
||||
/** Implementation of ++= macro for tasks. */
|
||||
def taskAppendNImpl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(vs: c.Expr[U])(a: c.Expr[Append.Values[T, U]]): c.Expr[Setting[Task[T]]] =
|
||||
{
|
||||
val init = taskMacroImpl[U](c)(vs)
|
||||
val assign = appendMacroImpl(c)(init.tree, a.tree)(AppendNInitName)
|
||||
c.Expr[Setting[Task[T]]](assign)
|
||||
val append = appendMacroImpl(c)(init.tree, a.tree)(AppendNInitName)
|
||||
c.Expr[Setting[Task[T]]](append)
|
||||
}
|
||||
/** Implementation of ++= macro for settings. */
|
||||
def settingAppendNImpl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(vs: c.Expr[U])(a: c.Expr[Append.Values[T, U]]): c.Expr[Setting[T]] =
|
||||
{
|
||||
val init = SettingMacro.settingMacroImpl[U](c)(vs)
|
||||
val assign = appendMacroImpl(c)(init.tree, a.tree)(AppendNInitName)
|
||||
c.Expr[Setting[T]](assign)
|
||||
val append = appendMacroImpl(c)(init.tree, a.tree)(AppendNInitName)
|
||||
c.Expr[Setting[T]](append)
|
||||
}
|
||||
|
||||
/** Implementation of -= macro for tasks. */
|
||||
def taskRemove1Impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(v: c.Expr[U])(r: c.Expr[Remove.Value[T, U]]): c.Expr[Setting[Task[T]]] =
|
||||
{
|
||||
val init = taskMacroImpl[U](c)(v)
|
||||
val remove = removeMacroImpl(c)(init.tree, r.tree)(Remove1InitName)
|
||||
c.Expr[Setting[Task[T]]](remove)
|
||||
}
|
||||
/** Implementation of -= macro for settings. */
|
||||
def settingRemove1Impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(v: c.Expr[U])(r: c.Expr[Remove.Value[T, U]]): c.Expr[Setting[T]] =
|
||||
{
|
||||
val init = SettingMacro.settingMacroImpl[U](c)(v)
|
||||
val remove = removeMacroImpl(c)(init.tree, r.tree)(Remove1InitName)
|
||||
c.Expr[Setting[T]](remove)
|
||||
}
|
||||
/** Implementation of --= macro for tasks. */
|
||||
def taskRemoveNImpl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(vs: c.Expr[U])(r: c.Expr[Remove.Values[T, U]]): c.Expr[Setting[Task[T]]] =
|
||||
{
|
||||
val init = taskMacroImpl[U](c)(vs)
|
||||
val remove = removeMacroImpl(c)(init.tree, r.tree)(RemoveNInitName)
|
||||
c.Expr[Setting[Task[T]]](remove)
|
||||
}
|
||||
/** Implementation of --= macro for settings. */
|
||||
def settingRemoveNImpl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: Context)(vs: c.Expr[U])(r: c.Expr[Remove.Values[T, U]]): c.Expr[Setting[T]] =
|
||||
{
|
||||
val init = SettingMacro.settingMacroImpl[U](c)(vs)
|
||||
val remove = removeMacroImpl(c)(init.tree, r.tree)(RemoveNInitName)
|
||||
c.Expr[Setting[T]](remove)
|
||||
}
|
||||
|
||||
private[this] def appendMacroImpl(c: Context)(init: c.Tree, append: c.Tree)(newName: String): c.Tree =
|
||||
|
|
@ -170,6 +201,15 @@ object TaskMacro {
|
|||
case x => ContextUtil.unexpectedTree(x)
|
||||
}
|
||||
}
|
||||
private[this] def removeMacroImpl(c: Context)(init: c.Tree, remove: c.Tree)(newName: String): c.Tree =
|
||||
{
|
||||
import c.universe.{ Apply, ApplyTag, newTermName, Select, SelectTag, TypeApply, TypeApplyTag }
|
||||
c.macroApplication match {
|
||||
case Apply(Apply(TypeApply(Select(preT, nmeT), targs), _), r) =>
|
||||
Apply(Apply(TypeApply(Select(preT, newTermName(newName).encodedName), targs), init :: sourcePosition(c).tree :: Nil), r)
|
||||
case x => ContextUtil.unexpectedTree(x)
|
||||
}
|
||||
}
|
||||
private[this] def transformMacroImpl(c: Context)(init: c.Tree)(newName: String): c.Tree =
|
||||
{
|
||||
import c.universe.{ Apply, ApplyTag, newTermName, Select, SelectTag }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
[@dwijnand]: http://github.com/dwijnand
|
||||
[1922]: https://github.com/sbt/sbt/pull/1922
|
||||
|
||||
### Fixes with compatibility implications
|
||||
|
||||
### Improvements
|
||||
|
||||
- Add `-=` and `--=` for settings and tasks, dual of `+=` and `++=`. [#1922][1922] by [@dwijnand][@dwijnand]
|
||||
|
||||
### Bug fixes
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
val intsTask = taskKey[Seq[Int]]("A seq of ints task")
|
||||
val intsSetting = settingKey[Seq[Int]]("A seq of ints setting")
|
||||
val intsFromScalaV = settingKey[Seq[Int]]("a seq of ints from scalaVersion")
|
||||
|
||||
scalaVersion := "2.11.6"
|
||||
|
||||
intsTask := Seq(1, 2, 3, 4, 5)
|
||||
intsTask -= 3
|
||||
intsTask --= Seq(1, 2)
|
||||
|
||||
intsSetting := Seq(1, 2, 3, 4, 5)
|
||||
intsSetting -= 3
|
||||
intsSetting --= Seq(1, 2)
|
||||
|
||||
intsFromScalaV := Seq(1, 2, 3, 4, 5)
|
||||
intsFromScalaV -= { if (scalaVersion.value == "2.11.6") 3 else 5 }
|
||||
intsFromScalaV --= { if (scalaVersion.value == "2.11.6") Seq(1, 2) else Seq(4) }
|
||||
|
||||
val check = taskKey[Unit]("Runs the check")
|
||||
check := {
|
||||
assert(intsTask.value == Seq(4, 5), s"intsTask should be Seq(4, 5) but is ${intsTask.value}")
|
||||
assert(intsSetting.value == Seq(4, 5), s"intsSetting should be Seq(4, 5) but is ${intsSetting.value}")
|
||||
assert(intsFromScalaV.value == Seq(4, 5), s"intsFromScalaV should be Seq(4, 5) but is ${intsFromScalaV.value}")
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
> check
|
||||
Loading…
Reference in New Issue