summaryrefslogtreecommitdiff
path: root/contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala
blob: f46f8c7b20595c6f26b18bee431057a3dffa6474 (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
package mill
package playlib
package worker

import java.io.File

import ammonite.ops.Path
import mill.api.Result
import mill.eval.PathRef
import mill.playlib.api.{RouteCompilerType, RouteCompilerWorkerApi}
import mill.scalalib.api.CompilationResult
import play.routes.compiler.RoutesCompiler.RoutesCompilerTask
import play.routes.compiler.{InjectedRoutesGenerator, RoutesCompiler, StaticRoutesGenerator}


class RouteCompilerWorker extends RouteCompilerWorkerApi {

  override def compile(
                        file: Path,
                        additionalImports: Seq[String],
                        forwardsRouter: Boolean,
                        reverseRouter: Boolean,
                        namespaceReverseRouter: Boolean,
                        generatorType: RouteCompilerType,
                        dest: Path)
                      (implicit ctx: mill.api.Ctx): mill.api.Result[CompilationResult] = {
    val routesGenerator = generatorType match {
      case RouteCompilerType.InjectedGenerator => InjectedRoutesGenerator
      case RouteCompilerType.StaticGenerator =>
        ctx.log.error("Static generator was deprecated in 2.6.0 and will be removed in 2.7.0")
        StaticRoutesGenerator
      case _ => throw new Exception(s"Unrecognized generator type: $generatorType. Use injected or static")
    }
    ctx.log.debug(s"compiling file $file with generator $generatorType")
    val result =
      RoutesCompiler.compile(
        RoutesCompilerTask(file.toIO, additionalImports, forwardsRouter, reverseRouter,
          namespaceReverseRouter),
        generator = routesGenerator,
        generatedDir = dest.toIO
      )
    ctx.log.debug("compilation result:" + result)
    result match {
      case Right(_) =>
        val zincFile = ctx.dest / 'zinc
        Result.Success(CompilationResult(zincFile, PathRef(ctx.dest)))
      case Left(errors) =>
        val errorMsg = errors.map(error =>
          s"compilation error in ${error.source.getPath} at line ${error.line.getOrElse("?")}," +
            s" column ${error.column.getOrElse("?")}: ${error.message}".stripMargin)
          .mkString("\n")
        Result.Failure("Unable to compile play routes, " + errorMsg)
    }
  }
}


case class RoutesCompilationError(source: File, message: String, line: Option[Int], column: Option[Int])