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
|
package forge
import java.nio.{file => jnio}
trait Target[T]{
val defCtx: DefCtx
override def toString = defCtx.staticEnclosing match{
case None => this.getClass.getSimpleName + "@" + Integer.toHexString(System.identityHashCode(this))
case Some(s) => this.getClass.getName + "@" + s
}
val inputs: Seq[Target[_]]
def evaluate(args: Args): T
def map[V](f: T => V)(implicit defCtx: DefCtx) = {
Target.Mapped(this, f, defCtx)
}
def zip[V](other: Target[V])(implicit defCtx: DefCtx) = {
Target.Zipped(this, other, defCtx)
}
def ~[V, R](other: Target[V])
(implicit s: Implicits.Sequencer[T, V, R], defCtx: DefCtx): Target[R] = {
this.zip(other).map(s.apply _ tupled)
}
}
object Target{
def test(inputs: Target[Int]*)(implicit defCtx: DefCtx) = Test(inputs, defCtx)
/**
* A dummy target that takes any number of inputs, and whose output can be
* controlled externally, so you can construct arbitrary dataflow graphs and
* test how changes propagate.
*/
case class Test(inputs: Seq[Target[Int]], defCtx: DefCtx) extends Target[Int]{
var counter = 1
def evaluate(args: Args) = counter + args.args.map(_.asInstanceOf[Int]).sum
}
def traverse[T](source: Seq[Target[T]])(implicit defCtx: DefCtx) = {
Traverse[T](source, defCtx)
}
case class Traverse[T](inputs: Seq[Target[T]], defCtx: DefCtx) extends Target[Seq[T]]{
def evaluate(args: Args) = {
for (i <- 0 until args.length)
yield args(i).asInstanceOf[T]
}
}
case class Mapped[T, V](source: Target[T], f: T => V,
defCtx: DefCtx) extends Target[V]{
def evaluate(args: Args) = f(args(0))
val inputs = List(source)
}
case class Zipped[T, V](source1: Target[T],
source2: Target[V],
defCtx: DefCtx) extends Target[(T, V)]{
def evaluate(args: Args) = (args(0), args(0))
val inputs = List(source1, source1)
}
def path(path: jnio.Path)(implicit defCtx: DefCtx) = Path(path, defCtx)
case class Path(path: jnio.Path, defCtx: DefCtx) extends Target[jnio.Path]{
def evaluate(args: Args) = path
val inputs = Nil
}
case class Subprocess(inputs: Seq[Target[_]],
command: Args => Seq[String],
defCtx: DefCtx) extends Target[Subprocess.Result] {
def evaluate(args: Args) = {
jnio.Files.createDirectories(args.dest)
import ammonite.ops._
implicit val path = ammonite.ops.Path(args.dest, pwd)
val output = %%(command(args))
assert(output.exitCode == 0)
Subprocess.Result(output, args.dest)
}
}
object Subprocess{
case class Result(result: ammonite.ops.CommandResult, dest: jnio.Path)
}
}
|