1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
package mill.define
import mill.define.Applicative.Applyable
import mill.eval.{PathRef, Result}
import mill.util.Ctx
import upickle.default.{Reader => R, Writer => W, ReadWriter => RW}
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
abstract class Task[+T] extends Task.Ops[T] with Applyable[Task, T]{
/**
* What other Targets does this Target depend on?
*/
val inputs: Seq[Task[_]]
/**
* Evaluate this target
*/
def evaluate(args: Ctx): Result[T]
/**
* Even if this target's inputs did not change, does it need to re-evaluate
* anyway?
*/
def sideHash: Int = 0
def flushDest: Boolean = true
def asTarget: Option[Target[T]] = None
def asCommand: Option[Command[T]] = None
def asPersistent: Option[Persistent[T]] = None
def self = this
}
trait NamedTask[+T] extends Task[T]{
def owner: Task.Module
def name: String
}
trait Target[+T] extends NamedTask[T]{
override def asTarget = Some(this)
def enclosing: String
def readWrite: RW[_]
}
object Target extends TargetGenerated with Applicative.Applyer[Task, Task, Result, Ctx] {
implicit def apply[T](t: T)
(implicit r: R[T],
w: W[T],
e: sourcecode.Enclosing,
n: sourcecode.Name,
cl: Caller[mill.define.Task.Module]): Target[T] = macro targetImpl[T]
def targetImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[T])
(r: c.Expr[R[T]],
w: c.Expr[W[T]],
e: c.Expr[sourcecode.Enclosing],
n: c.Expr[sourcecode.Name],
cl: c.Expr[Caller[mill.define.Task.Module]]): c.Expr[Target[T]] = {
import c.universe._
c.Expr[Target[T]](
mill.plugin.Cacher.wrapCached(c)(
q"new ${weakTypeOf[TargetImpl[T]]}(${Applicative.impl0[Task, T, Ctx](c)(q"mill.eval.Result.Success($t)").tree}, $e.value, $cl.value, $n.value, upickle.default.ReadWriter($w.write, $r.read))"
)
)
}
implicit def apply[T](t: Result[T])
(implicit r: R[T],
w: W[T],
e: sourcecode.Enclosing,
n: sourcecode.Name,
cl: Caller[mill.define.Task.Module]): Target[T] = macro targetResultImpl[T]
def targetResultImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[Result[T]])
(r: c.Expr[R[T]],
w: c.Expr[W[T]],
e: c.Expr[sourcecode.Enclosing],
n: c.Expr[sourcecode.Name],
cl: c.Expr[Caller[mill.define.Task.Module]]): c.Expr[Target[T]] = {
import c.universe._
c.Expr[Target[T]](
mill.plugin.Cacher.wrapCached(c)(
q"new ${weakTypeOf[TargetImpl[T]]}(${Applicative.impl0[Task, T, Ctx](c)(t.tree).tree}, $e.value, $cl.value, $n.value, upickle.default.ReadWriter($w.write, $r.read))"
)
)
}
def apply[T](t: Task[T])
(implicit r: R[T],
w: W[T],
e: sourcecode.Enclosing,
n: sourcecode.Name,
cl: Caller[mill.define.Task.Module]): Target[T] = macro targetTaskImpl[T]
def targetTaskImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[Task[T]])
(r: c.Expr[R[T]],
w: c.Expr[W[T]],
e: c.Expr[sourcecode.Enclosing],
n: c.Expr[sourcecode.Name],
cl: c.Expr[Caller[mill.define.Task.Module]]): c.Expr[Target[T]] = {
import c.universe._
c.Expr[Target[T]](
mill.plugin.Cacher.wrapCached(c)(
q"new ${weakTypeOf[TargetImpl[T]]}($t, $e.value, $cl.value, $n.value, upickle.default.ReadWriter($w.write, $r.read))"
)
)
}
def command[T](t: Result[T])
(implicit w: W[T],
n: sourcecode.Name,
cl: Caller[mill.define.Task.Module]): Command[T] = macro commandImpl[T]
def source(path: ammonite.ops.Path) = new Source(path)
def command[T](t: Task[T])
(implicit c: Caller[Task.Module],
n: sourcecode.Name,
w: W[T]): Command[T] = new Command(t, c.value, n.value, w)
def commandImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[T])
(w: c.Expr[W[T]],
n: c.Expr[sourcecode.Name],
cl: c.Expr[Caller[mill.define.Task.Module]]): c.Expr[Command[T]] = {
import c.universe._
c.Expr[Command[T]](
q"new ${weakTypeOf[Command[T]]}(${Applicative.impl[Task, T, Ctx](c)(t).tree}, $cl.value, $n.value, $w)"
)
}
def task[T](t: Result[T]): Task[T] = macro Applicative.impl[Task, T, Ctx]
def persistent[T](t: Result[T])(implicit r: R[T],
w: W[T],
e: sourcecode.Enclosing,
n: sourcecode.Name,
cl: Caller[mill.define.Task.Module]): Target[T] = macro persistentImpl[T]
def persistentImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[T])
(r: c.Expr[R[T]],
w: c.Expr[W[T]],
e: c.Expr[sourcecode.Enclosing],
n: c.Expr[sourcecode.Name],
cl: c.Expr[Caller[mill.define.Task.Module]]): c.Expr[Persistent[T]] = {
import c.universe._
c.Expr[Persistent[T]](
mill.plugin.Cacher.wrapCached(c)(
q"new ${weakTypeOf[Persistent[T]]}(${Applicative.impl[Task, T, Ctx](c)(t).tree}, $e.value, $cl.value, $n.value, upickle.default.ReadWriter($w.write, $r.read))"
)
)
}
type TT[+X] = Task[X]
def makeT[X](inputs0: Seq[TT[_]], evaluate0: Ctx => Result[X]) = new Task[X] {
val inputs = inputs0
def evaluate(x: Ctx) = evaluate0(x)
}
def underlying[A](v: Task[A]) = v
def mapCtx[A, B](t: Task[A])(f: (A, Ctx) => Result[B]) = t.mapDest(f)
def zip() = new Task.Task0(())
def zip[A](a: Task[A]) = a.map(Tuple1(_))
def zip[A, B](a: Task[A], b: Task[B]) = a.zip(b)
}
case class Caller[A](value: A)
object Caller {
def apply[T]()(implicit c: Caller[T]) = c.value
implicit def generate[T]: Caller[T] = macro impl[T]
def impl[T: c.WeakTypeTag](c: Context): c.Tree = {
import c.universe._
q"new _root_.mill.define.Caller[${weakTypeOf[T]}](this)"
}
}
class TargetImpl[+T](t: Task[T],
val enclosing: String,
val owner: Task.Module,
val name: String,
val readWrite: RW[_]) extends Target[T] {
val inputs = Seq(t)
def evaluate(args: Ctx) = args[T](0)
override def toString = enclosing + "@" + Integer.toHexString(System.identityHashCode(this))
}
class Command[+T](t: Task[T],
val owner: Task.Module,
val name: String,
val writer: W[_]) extends NamedTask[T] {
val inputs = Seq(t)
def evaluate(args: Ctx) = args[T](0)
override def asCommand = Some(this)
}
class Persistent[+T](t: Task[T],
enclosing: String,
owner: Task.Module,
name: String,
readWrite: RW[_])
extends TargetImpl[T](t, enclosing, owner, name, readWrite) {
override def flushDest = false
override def asPersistent = Some(this)
}
object Source{
implicit def apply(p: ammonite.ops.Path) = new Source(p)
}
class Source(path: ammonite.ops.Path) extends Task[PathRef]{
def handle = PathRef(path)
def evaluate(args: Ctx) = handle
override def sideHash = handle.hashCode()
val inputs = Nil
}
object Task {
trait TaskModule extends Module {
def defaultCommandName(): String
}
trait Module extends mill.plugin.Cacher[Target]{
def wrapCached[T](t: Target[T], enclosing: String): Target[T] = t
}
class Task0[T](t: T) extends Task[T]{
lazy val t0 = t
val inputs = Nil
def evaluate(args: Ctx) = t0
}
abstract class Ops[+T]{ this: Task[T] =>
def map[V](f: T => V) = new Task.Mapped(this, f)
def mapDest[V](f: (T, Ctx) => Result[V]) = new Task.MappedDest(this, f)
def filter(f: T => Boolean) = this
def withFilter(f: T => Boolean) = this
def zip[V](other: Task[V]) = new Task.Zipped(this, other)
}
def traverse[T, V](source: Seq[T])(f: T => Task[V]) = {
new Sequence[V](source.map(f))
}
def sequence[T](source: Seq[Task[T]]) = new Sequence[T](source)
class Sequence[+T](inputs0: Seq[Task[T]]) extends Task[Seq[T]]{
val inputs = inputs0
def evaluate(args: Ctx) = {
for (i <- 0 until args.length)
yield args(i).asInstanceOf[T]
}
}
class Mapped[+T, +V](source: Task[T], f: T => V) extends Task[V]{
def evaluate(args: Ctx) = f(args(0))
val inputs = List(source)
}
class MappedDest[+T, +V](source: Task[T], f: (T, Ctx) => Result[V]) extends Task[V]{
def evaluate(args: Ctx) = f(args(0), args)
val inputs = List(source)
}
class Zipped[+T, +V](source1: Task[T], source2: Task[V]) extends Task[(T, V)]{
def evaluate(args: Ctx) = (args(0), args(1))
val inputs = List(source1, source2)
}
}
|