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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
package mill.define
import ammonite.main.Router.Overrides
import mill.define.Applicative.Applyable
import mill.eval.{PathRef, Result}
import mill.util.EnclosingClass
import sourcecode.Compat.Context
import upickle.default.{ReadWriter => RW, Reader => R, Writer => W}
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
/**
* Models a single node in the Mill build graph, with a list of inputs and a
* single output of type [[T]].
*
* Generally not instantiated manually, but instead constructed via the
* [[Target.apply]] & similar macros.
*/
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: mill.util.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 asWorker: Option[Worker[T]] = None
def self = this
}
trait NamedTask[+T] extends Task[T]{
def ctx: mill.define.Ctx
def label = ctx.segment match{case Segment.Label(v) => v}
override def toString = ctx.segments.render
}
trait Target[+T] extends NamedTask[T]{
override def asTarget = Some(this)
def readWrite: RW[_]
}
object Target extends TargetGenerated with Applicative.Applyer[Task, Task, Result, mill.util.Ctx] {
implicit def apply[T](t: T)
(implicit r: R[T],
w: W[T],
ctx: mill.define.Ctx): 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]],
ctx: c.Expr[mill.define.Ctx]): c.Expr[Target[T]] = {
import c.universe._
val lhs = Applicative.impl0[Task, T, mill.util.Ctx](c)(reify(Result.Success(t.splice)).tree)
mill.moduledefs.Cacher.impl0[TargetImpl[T]](c)(
reify(
new TargetImpl[T](lhs.splice, ctx.splice, RW(w.splice.write, r.splice.read))
)
)
}
implicit def apply[T](t: Result[T])
(implicit r: R[T],
w: W[T],
ctx: mill.define.Ctx): 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]],
ctx: c.Expr[mill.define.Ctx]): c.Expr[Target[T]] = {
import c.universe._
mill.moduledefs.Cacher.impl0[Target[T]](c)(
reify(
new TargetImpl[T](
Applicative.impl0[Task, T, mill.util.Ctx](c)(t.tree).splice,
ctx.splice,
RW(w.splice.write, r.splice.read)
)
)
)
}
def apply[T](t: Task[T])
(implicit r: R[T],
w: W[T],
ctx: mill.define.Ctx): 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]],
ctx: c.Expr[mill.define.Ctx]): c.Expr[Target[T]] = {
import c.universe._
mill.moduledefs.Cacher.impl0[Target[T]](c)(
reify(
new TargetImpl[T](t.splice, ctx.splice, RW(w.splice.write, r.splice.read))
)
)
}
def sources(values: Result[ammonite.ops.Path]*)
(implicit ctx: mill.define.Ctx): Sources = macro sourcesImpl1
def sourcesImpl1(c: Context)
(values: c.Expr[Result[ammonite.ops.Path]]*)
(ctx: c.Expr[mill.define.Ctx]): c.Expr[Sources] = {
import c.universe._
val wrapped =
for (value <- values.toList)
yield Applicative.impl0[Task, PathRef, mill.util.Ctx](c)(
reify(value.splice.map(PathRef(_))).tree
).tree
mill.moduledefs.Cacher.impl0[Sources](c)(
reify(
new Sources(
Task.sequence(c.Expr[List[Task[PathRef]]](q"scala.List(..$wrapped)").splice),
ctx.splice
)
)
)
}
def sources(values: Result[Seq[PathRef]])
(implicit ctx: mill.define.Ctx): Sources = macro sourcesImpl2
def sourcesImpl2(c: Context)
(values: c.Expr[Result[Seq[PathRef]]])
(ctx: c.Expr[mill.define.Ctx]): c.Expr[Sources] = {
import c.universe._
mill.moduledefs.Cacher.impl0[Sources](c)(
reify(
new Sources(
Applicative.impl0[Task, Seq[PathRef], mill.util.Ctx](c)(values.tree).splice,
ctx.splice
)
)
)
}
def input[T](value: Result[T])
(implicit r: R[T],
w: W[T],
ctx: mill.define.Ctx): Input[T] = macro inputImpl[T]
def inputImpl[T: c.WeakTypeTag](c: Context)
(value: c.Expr[T])
(r: c.Expr[R[T]],
w: c.Expr[W[T]],
ctx: c.Expr[mill.define.Ctx]): c.Expr[Input[T]] = {
import c.universe._
mill.moduledefs.Cacher.impl0[Input[T]](c)(
reify(
new Input[T](
Applicative.impl[Task, T, mill.util.Ctx](c)(value).splice,
ctx.splice,
RW(w.splice.write, r.splice.read)
)
)
)
}
def command[T](t: Task[T])
(implicit ctx: mill.define.Ctx,
w: W[T],
cls: EnclosingClass,
overrides: Overrides): Command[T] = {
new Command(t, ctx, w, cls.value, overrides.value)
}
def command[T](t: Result[T])
(implicit w: W[T],
ctx: mill.define.Ctx,
cls: EnclosingClass,
overrides: Overrides): Command[T] = macro commandImpl[T]
def commandImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[T])
(w: c.Expr[W[T]],
ctx: c.Expr[mill.define.Ctx],
cls: c.Expr[EnclosingClass],
overrides: c.Expr[Overrides]): c.Expr[Command[T]] = {
import c.universe._
reify(
new Command[T](
Applicative.impl[Task, T, mill.util.Ctx](c)(t).splice,
ctx.splice,
w.splice,
cls.splice.value,
overrides.splice.value
)
)
}
def worker[T](t: Task[T])
(implicit ctx: mill.define.Ctx): Worker[T] = new Worker(t, ctx)
def worker[T](t: Result[T])
(implicit ctx: mill.define.Ctx): Worker[T] = macro workerImpl[T]
def workerImpl[T: c.WeakTypeTag](c: Context)
(t: c.Expr[T])
(ctx: c.Expr[mill.define.Ctx]): c.Expr[Worker[T]] = {
import c.universe._
reify(
new Worker[T](Applicative.impl[Task, T, mill.util.Ctx](c)(t).splice, ctx.splice)
)
}
def task[T](t: Result[T]): Task[T] = macro Applicative.impl[Task, T, mill.util.Ctx]
def persistent[T](t: Result[T])(implicit r: R[T],
w: W[T],
ctx: mill.define.Ctx): Persistent[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]],
ctx: c.Expr[mill.define.Ctx]): c.Expr[Persistent[T]] = {
import c.universe._
mill.moduledefs.Cacher.impl0[Persistent[T]](c)(
reify(
new Persistent[T](
Applicative.impl[Task, T, mill.util.Ctx](c)(t).splice,
ctx.splice,
RW(w.splice.write, r.splice.read)
)
)
)
}
type TT[+X] = Task[X]
def makeT[X](inputs0: Seq[TT[_]], evaluate0: mill.util.Ctx => Result[X]) = new Task[X] {
val inputs = inputs0
def evaluate(x: mill.util.Ctx) = evaluate0(x)
}
def underlying[A](v: Task[A]) = v
def mapCtx[A, B](t: Task[A])(f: (A, mill.util.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)"
}
}
abstract class NamedTaskImpl[+T](ctx0: mill.define.Ctx, t: Task[T]) extends NamedTask[T]{
def evaluate(args: mill.util.Ctx) = args[T](0)
val ctx = ctx0.copy(segments = ctx0.segments ++ Seq(ctx0.segment))
val inputs = Seq(t)
}
class TargetImpl[+T](t: Task[T],
ctx0: mill.define.Ctx,
val readWrite: RW[_]) extends NamedTaskImpl[T](ctx0, t) with Target[T] {
}
class Command[+T](t: Task[T],
ctx0: mill.define.Ctx,
val writer: W[_],
val cls: Class[_],
val overrides: Int) extends NamedTaskImpl[T](ctx0, t) {
override def asCommand = Some(this)
}
class Worker[+T](t: Task[T], ctx0: mill.define.Ctx) extends NamedTaskImpl[T](ctx0, t) {
override def flushDest = false
override def asWorker = Some(this)
}
class Persistent[+T](t: Task[T],
ctx0: mill.define.Ctx,
readWrite: RW[_])
extends TargetImpl[T](t, ctx0, readWrite) {
override def flushDest = false
override def asPersistent = Some(this)
}
class Input[T](t: Task[T],
ctx0: mill.define.Ctx,
val readWrite: RW[_]) extends NamedTaskImpl[T](ctx0, t) with Target[T]{
override def sideHash = util.Random.nextInt()
}
class Sources(t: Task[Seq[PathRef]],
ctx0: mill.define.Ctx) extends Input[Seq[PathRef]](
t,
ctx0,
RW(
upickle.default.SeqishW[PathRef, Seq].write,
upickle.default.SeqishR[Seq, PathRef].read
)
)
object Task {
class Task0[T](t: T) extends Task[T]{
lazy val t0 = t
val inputs = Nil
def evaluate(args: mill.util.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, mill.util.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: mill.util.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: mill.util.Ctx) = f(args(0))
val inputs = List(source)
}
class MappedDest[+T, +V](source: Task[T], f: (T, mill.util.Ctx) => Result[V]) extends Task[V]{
def evaluate(args: mill.util.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: mill.util.Ctx) = (args(0), args(1))
val inputs = List(source1, source2)
}
}
|