summaryrefslogtreecommitdiff
path: root/core/src/main/scala/forge/Main.scala
blob: bb1d5d69fc545a536b1eaf506eb6f2194c9ee8e9 (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
package forge

import ammonite.ops._
import ammonite.util.{Name, Res}
import forge.define.Task
import forge.discover.{Discovered, NestedEntry}
import forge.eval.Evaluator
import forge.util.OSet
import play.api.libs.json.Format


object Main {
  def main(args: Array[String]): Unit = {

    val List(buildFile, selector0, rest @_*) = args.toList
    pprint.log((buildFile, selector0, rest))
    val selector = selector0.split('.').toList
    ammonite.Main().instantiateInterpreter() match{
      case Left(problems) => pprint.log(problems)
      case Right(interp) =>
        val result = ammonite.main.Scripts.runScript(pwd, Path(buildFile, pwd), interp, Nil)

        if (!result.isSuccess) println(result)
        else{

          val (obj, discovered) = result.asInstanceOf[Res.Success[(Any, Discovered[Any])]].s
          val consistencyErrors = Discovered.consistencyCheck(obj, discovered)
          pprint.log(consistencyErrors)
          if (consistencyErrors.nonEmpty) println("Failed Discovered.consistencyCheck: " + consistencyErrors)
          else {
            val mapping = Discovered.mapping(obj)(discovered)
            val workspacePath = pwd / 'out
            val evaluator = new Evaluator(workspacePath, mapping)
            val mainRoutes = discovered.mains.map(x => (x.path :+ x.entryPoint.name, Left(x)))
            val targetRoutes = discovered.targets.map(x => x._1 -> Right(x))
            val allRoutes = (mainRoutes ++ targetRoutes).toMap[
              Seq[String],
              Either[NestedEntry[Any, _], (Seq[String], Format[_], Any => Task[_])]
              ]
            allRoutes.get(selector) match{
              case Some(Left(nestedEntryPoint)) =>
                nestedEntryPoint.invoke(
                  obj,
                  ammonite.main.Scripts.groupArgs(rest.toList)
                ) match{
                  case error: forge.discover.Router.Result.Error =>
                    println("Failed to evaluate main method: " + error)
                  case forge.discover.Router.Result.Success(target) =>
                    println("Found target! " + target)
                    val evaluated = evaluator.evaluate(OSet(target))
                    pprint.log(evaluated)
                }

              case None => println("Unknown selector: " + selector)
              case Some(Right((_, _, targetFunc))) =>
                val target = targetFunc(obj)
                val evaluated = evaluator.evaluate(OSet(target))
                pprint.log(evaluated)
            }
          }

        }
    }
  }

}