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
|
package mill
import ammonite.interp.Interpreter
import ammonite.main.Scripts
import ammonite.ops._
import ammonite.util.{Colors, Res}
import mill.define.Task
import mill.discover._
import mill.eval.{Evaluator, Result}
import mill.util.OSet
import ammonite.main.Scripts.pathScoptRead
import ammonite.repl.Repl
import mill.define.Task.TaskModule
object Main {
def parseSelector(input: String) = {
import fastparse.all._
val segment = P( CharsWhileIn(('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9')).! ).map(
Mirror.Segment.Label
)
val crossSegment = P( "[" ~ CharsWhile(c => c != ',' && c != ']').!.rep(1, sep=",") ~ "]" ).map(
Mirror.Segment.Cross
)
val query = P( segment ~ ("." ~ segment | crossSegment).rep ~ End ).map{
case (h, rest) => h :: rest.toList
}
query.parse(input)
}
def parseArgs(selectorString: String): Either[String, List[Mirror.Segment]] = {
import fastparse.all.Parsed
if (selectorString.isEmpty) Left("Selector cannot be empty")
else parseSelector(selectorString) match {
case f: Parsed.Failure => Left(s"Parsing exception ${f.msg}")
case Parsed.Success(selector, _) => Right(selector)
}
}
def resolve[T, V](remainingSelector: List[Mirror.Segment],
hierarchy: Mirror[T, V],
obj: T,
rest: Seq[String],
remainingCrossSelectors: List[List[String]],
revSelectorsSoFar: List[Mirror.Segment]): Either[String, Task[Any]] = {
remainingSelector match{
case Mirror.Segment.Cross(_) :: Nil => Left("Selector cannot start with a [cross] segment")
case Mirror.Segment.Label(last) :: Nil =>
def target =
hierarchy.targets
.find(_.label == last)
.map(x => Right(x.run(hierarchy.node(obj, remainingCrossSelectors))))
def invokeCommand[V](mirror: Mirror[T, V], name: String) = for{
cmd <- mirror.commands.find(_.name == name)
} yield cmd.invoke(
mirror.node(obj, remainingCrossSelectors),
ammonite.main.Scripts.groupArgs(rest.toList)
) match {
case Router.Result.Success(v) => Right(v)
case _ => Left(s"Command failed $last")
}
def runDefault = for{
(label, child) <- hierarchy.children
if label == last
res <- child.node(obj, remainingCrossSelectors) match{
case taskMod: TaskModule => Some(invokeCommand(child, taskMod.defaultCommandName()))
case _ => None
}
} yield res
def command = invokeCommand(hierarchy, last)
command orElse target orElse runDefault.headOption.flatten match{
case None => Left("Cannot resolve task " + Mirror.renderSelector(
(Mirror.Segment.Label(last) :: revSelectorsSoFar).reverse)
)
case Some(either) => either
}
case head :: tail =>
val newRevSelectorsSoFar = head :: revSelectorsSoFar
head match{
case Mirror.Segment.Label(singleLabel) =>
hierarchy.children.collectFirst{
case (label, child) if label == singleLabel => child
} match{
case Some(child) => resolve(tail, child, obj, rest, remainingCrossSelectors, newRevSelectorsSoFar)
case None => Left("Cannot resolve module " + Mirror.renderSelector(newRevSelectorsSoFar.reverse))
}
case Mirror.Segment.Cross(cross) =>
val Some((crossGen, childMirror)) = hierarchy.crossChildren
val crossOptions = crossGen(hierarchy.node(obj, remainingCrossSelectors))
if (crossOptions.contains(cross)){
resolve(tail, childMirror, obj, rest, remainingCrossSelectors, newRevSelectorsSoFar)
}else{
Left("Cannot resolve cross " + Mirror.renderSelector(newRevSelectorsSoFar.reverse))
}
}
case Nil => Left("Selector cannot be empty")
}
}
def discoverMirror[T: Discovered](obj: T): Either[String, Discovered[T]] = {
val discovered = implicitly[Discovered[T]]
val consistencyErrors = Discovered.consistencyCheck(obj, discovered)
if (consistencyErrors.nonEmpty) {
Left(s"Failed Discovered.consistencyCheck: ${consistencyErrors.map(Mirror.renderSelector)}")
} else {
Right(discovered)
}
}
def evaluate(evaluator: Evaluator,
target: Task[Any],
watch: Path => Unit): Option[String] = {
val evaluated = evaluator.evaluate(OSet(target))
evaluated.transitive.foreach {
case t: define.Source => watch(t.handle.path)
case _ => // do nothing
}
val errorStr =
(for((k, fs) <- evaluated.failing.items()) yield {
val ks = k match{
case Left(t) => t.toString
case Right(t) => Mirror.renderSelector(t.segments.toList)
}
val fss = fs.map{
case Result.Exception(t) => t.toString
case Result.Failure(t) => t
}
s"$ks ${fss.mkString(", ")}"
}).mkString("\n")
evaluated.failing.keyCount match {
case 0 => None
case n => Some(s"$n targets failed\n$errorStr")
}
}
def apply[T: Discovered](args: Seq[String],
obj: T,
watch: Path => Unit,
coloredOutput: Boolean): Int = {
val log = new Logger(coloredOutput)
val Seq(selectorString, rest @_*) = args
val res = for {
sel <- parseArgs(selectorString)
disc <- discoverMirror(obj)
crossSelectors = sel.map{
case Mirror.Segment.Cross(x) => x.toList.map(_.toString)
case _ => Nil
}
target <- resolve(sel, disc.mirror, obj, rest, crossSelectors, Nil)
evaluator = new Evaluator(pwd / 'out, Discovered.mapping(obj)(disc), log.info)
_ <- evaluate(evaluator, target, watch).toLeft(())
} yield ()
res match {
case Left(err) =>
log.error(err)
1
case Right(_) => 0
}
}
case class Config(home: ammonite.ops.Path = pwd/'out/'ammonite,
colored: Option[Boolean] = None,
help: Boolean = false,
repl: Boolean = false,
watch: Boolean = false)
def main(args: Array[String]): Unit = {
val startTime = System.currentTimeMillis()
import ammonite.main.Cli.Arg
val signature = Seq(
Arg[Config, Path](
"home", Some('h'),
"The home directory of the REPL; where it looks for config and caches",
(c, v) => c.copy(home = v)
),
Arg[Config, Unit](
"help", None,
"""Print this message""".stripMargin,
(c, v) => c.copy(help = true)
),
Arg[Config, Boolean](
"color", None,
"""Enable or disable colored output; by default colors are enabled
|in both REPL and scripts if the console is interactive, and disabled
|otherwise""".stripMargin,
(c, v) => c.copy(colored = Some(v))
),
Arg[Config, Unit](
"repl", Some('r'),
"Open a build REPL",
(c, v) => c.copy(repl = true)
),
Arg[Config, Unit](
"watch", Some('w'),
"Watch and re-run your build when it changes",
(c, v) => c.copy(watch = true)
)
)
ammonite.main.Cli.groupArgs(args.toList, signature, Config()) match{
case Left(err) =>
case Right((config, leftover)) =>
if (config.help) {
val leftMargin = signature.map(ammonite.main.Cli.showArg(_).length).max + 2
System.err.println(ammonite.main.Cli.formatBlock(signature, leftMargin).mkString("\n"))
System.exit(0)
} else {
val res = new Main(config).run(leftover, startTime)
System.exit(res)
}
}
}
}
class Logger(coloredOutput: Boolean){
val colors =
if(coloredOutput) Colors.Default
else Colors.BlackWhite
def info(s: String) = System.err.println(colors.info()(s))
def error(s: String) = System.err.println(colors.error()(s))
}
class Main(config: Main.Config){
val coloredOutput = config.colored.getOrElse(ammonite.Main.isInteractive())
val log = new Logger(coloredOutput)
def watchAndWait(watched: Seq[(Path, Long)]) = {
log.info(s"Watching for changes to ${watched.length} files... (Ctrl-C to exit)")
def statAll() = watched.forall{ case (file, lastMTime) =>
Interpreter.pathSignature(file) == lastMTime
}
while(statAll()) Thread.sleep(100)
}
def handleWatchRes[T](res: Res[T], printing: Boolean) = res match {
case Res.Failure(msg) =>
log.error(msg)
false
case Res.Exception(ex, s) =>
log.error(
Repl.showException(ex, fansi.Color.Red, fansi.Attr.Reset, fansi.Color.Green)
)
false
case Res.Success(value) =>
if (printing && value != ()) println(pprint.PPrinter.BlackWhite(value))
true
case Res.Skip => true // do nothing on success, everything's already happened
case Res.Exit(_) => ???
}
def run(leftover: List[String], startTime0: Long): Int = {
var exitCode = 0
var startTime = startTime0
val loop = config.watch
do {
val watchedFiles = if (config.repl) {
val repl = ammonite.Main(
predefFile = Some(pwd / "build.sc")
).instantiateRepl(remoteLogger = None).right.get
repl.interp.initializePredef()
repl.run()
repl.interp.watchedFiles
} else {
val interp = ammonite.Main(
predefFile = Some(pwd / "build.sc")
).instantiateInterpreter()match{
case Left(x) => println(x); ???
case Right(x) => x
}
interp.initializePredef()
val syntheticPath = pwd / 'out / "run.sc"
write.over(
syntheticPath,
s"""@main def run(args: String*) = mill.Main(args, ammonite.predef.FilePredef, interp.watch, $coloredOutput)
|
|@main def idea() = mill.scalaplugin.GenIdea(ammonite.predef.FilePredef)
""".stripMargin
)
val res = ammonite.main.Scripts.runScript(
pwd,
syntheticPath,
interp,
Scripts.groupArgs(leftover)
)
res match{
case Res.Success(v: Int) => exitCode = v
case _ => exitCode = 1
}
handleWatchRes(res, false)
interp.watchedFiles
}
val delta = System.currentTimeMillis() - startTime
log.info("Finished in " + delta/1000.0 + "s")
if (loop) watchAndWait(watchedFiles)
startTime = System.currentTimeMillis()
} while(loop)
exitCode
}
}
|