summaryrefslogtreecommitdiff
path: root/src/test/scala/forge/Main.scala
blob: 99237e32e8fdaf1af347e7e3963d2ab2e81e28fa (plain) (blame)
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
package forge
import java.io.FileOutputStream
import java.util.jar.JarEntry
import collection.JavaConverters._
import ammonite.ops._
object Main{
  val sourceRoot = Target.path(pwd / 'src / 'test / 'resources / 'example / 'src)
  val resourceRoot = Target.path(pwd / 'src / 'test / 'resources / 'example / 'resources)
  val allSources = list(sourceRoot)
  val classFiles = compileAll(allSources)
  val jar = jarUp(resourceRoot, classFiles)

  def main(args: Array[String]): Unit = {
    val mapping = Discovered.mapping(Main)
    val evaluator = new Evaluator(pwd / 'target / 'workspace / 'main, mapping)
    val res = evaluator.evaluate(OSet(jar))
    println(res.evaluated.collect(mapping))
  }
  def compileAll(sources: Target[Seq[Path]])  = {
    new Target.Subprocess(
      Seq(sources),
      args =>
        Seq("javac") ++
        args[Seq[Path]](0).map(_.toString) ++
        Seq("-d", args.dest.toString)
    ).map(_.dest)
  }

  def list(root: Target[Path]): Target[Seq[Path]] = {
    root.map(ls.rec)
  }
  case class jarUp(roots: Target[Path]*) extends Target[Path]{

    val inputs = roots
    def evaluate(args: Args): Path = {

      val output = new java.util.jar.JarOutputStream(new FileOutputStream(args.dest.toIO))
      for{
        root0 <- args.args
        root = root0.asInstanceOf[Path]

        path <- ls.rec(root)
        if path.isFile
      }{
        val relative = path.relativeTo(root)
        output.putNextEntry(new JarEntry(relative.toString))
        output.write(read.bytes(path))
      }
      output.close()
      args.dest
    }


  }

}