summaryrefslogtreecommitdiff
path: root/core/src/test/examples/javac/build.sc
blob: 37d7a5a666f4938e57127a6025c8667e4207c9dd (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
57
58
59
60
61
62
63
64
65
66
67
68
69
import forge.define.Task
import forge.eval.PathRef

object Foo {

  import java.io.FileOutputStream
  import java.util.jar.JarEntry

  import ammonite.ops.{ls, pwd, read}
  import forge.discover.Discovered
  import forge.util.Args

  val workspacePath = pwd / 'target / 'workspace / 'javac
  val javacSrcPath = pwd / 'src / 'test / 'examples / 'javac
  val javacDestPath = workspacePath / 'src

  val sourceRootPath = javacDestPath / 'src
  val resourceRootPath = javacDestPath / 'resources

  // sourceRoot -> allSources -> classFiles
  //                                |
  //                                v
  //           resourceRoot ---->  jar
  val sourceRoot = Task.path(sourceRootPath)
  val resourceRoot = Task.path(resourceRootPath)
  val allSources = list(sourceRoot)
  val classFiles = compileAll(allSources)
  val jar = jarUp(resourceRoot, classFiles)

  def compileAll(sources: Task[Seq[PathRef]]) = {
    new Task.Subprocess(
      Seq(sources),
      args =>
        Seq("javac") ++
          args[Seq[PathRef]](0).map(_.path.toString) ++
          Seq("-d", args.dest.toString)
    ).map(_.dest)
  }

  def list(root: Task[PathRef]): Task[Seq[PathRef]] = {
    root.map(x => ls.rec(x.path).map(PathRef(_)))
  }

  case class jarUp(roots: Task[PathRef]*) extends Task[PathRef] {

    val inputs = roots

    def evaluate(args: Args): PathRef = {

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

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

}

@main def main(): Any = Foo -> forge.Discovered[Foo.type]