summaryrefslogtreecommitdiff
path: root/contrib/playlib
diff options
context:
space:
mode:
authorJean Helou <jhe@codamens.fr>2019-02-11 22:26:08 +0100
committerTobias Roeser <le.petit.fou@web.de>2019-02-14 17:45:16 +0100
commitdfff4e0e7f4ac136dd509fa0c8085970e738cf1f (patch)
treeb2f8fa1af556e0357b28f4e3d28819cb087f4aaa /contrib/playlib
parent0c879ca73b17769a6982d2b932ac21dcd5e1d98f (diff)
downloadmill-dfff4e0e7f4ac136dd509fa0c8085970e738cf1f.tar.gz
mill-dfff4e0e7f4ac136dd509fa0c8085970e738cf1f.tar.bz2
mill-dfff4e0e7f4ac136dd509fa0c8085970e738cf1f.zip
Adds the specialized workers
This module adds workers specialized for play 2.6.0. and 2.7.0, these modules actually depend on playframework artifacts. They are dynamically loaded from the `RoutesCompilerWorkerApi`.
Diffstat (limited to 'contrib/playlib')
-rw-r--r--contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala58
-rw-r--r--contrib/playlib/worker/2.7.0/src/RouteCompilerWorker.scala64
2 files changed, 122 insertions, 0 deletions
diff --git a/contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala b/contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala
new file mode 100644
index 00000000..f46f8c7b
--- /dev/null
+++ b/contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala
@@ -0,0 +1,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]) \ No newline at end of file
diff --git a/contrib/playlib/worker/2.7.0/src/RouteCompilerWorker.scala b/contrib/playlib/worker/2.7.0/src/RouteCompilerWorker.scala
new file mode 100644
index 00000000..99e2b493
--- /dev/null
+++ b/contrib/playlib/worker/2.7.0/src/RouteCompilerWorker.scala
@@ -0,0 +1,64 @@
+package mill
+package playlib
+package worker
+
+import java.io.File
+
+import ammonite.ops.Path
+import mill.api.{Ctx, 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, RoutesCompilationError, RoutesCompiler, RoutesGenerator}
+
+
+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] = {
+ generatorType match {
+ case RouteCompilerType.InjectedGenerator =>
+ val result = compileWithPlay(file, additionalImports, forwardsRouter, reverseRouter,
+ namespaceReverseRouter, dest, ctx, InjectedRoutesGenerator)
+ asMillResult(ctx, result)
+ case RouteCompilerType.StaticGenerator =>
+ Result.Failure("Static generator was deprecated in 2.6.0 then removed in 2.7.x, see https://www.playframework.com/documentation/2.7.x/Migration27#StaticRoutesGenerator-removed")
+ }
+ }
+
+ // the following code is duplicated between play worker versions because it depends on play types
+ // which are not guaranteed to stay the same between versions even though they are currently
+ // identical
+ private def compileWithPlay(file: Path, additionalImports: Seq[String], forwardsRouter: Boolean, reverseRouter: Boolean, namespaceReverseRouter: Boolean, dest: Path, ctx: Ctx, routesGenerator: RoutesGenerator) = {
+ ctx.log.debug(s"compiling file $file with play generator $routesGenerator")
+ val result =
+ RoutesCompiler.compile(
+ RoutesCompilerTask(file.toIO, additionalImports, forwardsRouter, reverseRouter,
+ namespaceReverseRouter),
+ generator = routesGenerator,
+ generatedDir = dest.toIO
+ )
+ ctx.log.debug(s"compilation result: $result")
+ result
+ }
+
+ private def asMillResult(ctx: Ctx, result: Either[Seq[RoutesCompilationError], Seq[File]]): Result[CompilationResult] = {
+ 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.getName} at line${error.line}, column ${error.column}: ${error.message}"""")
+ .mkString("\n")
+ Result.Failure("Unable to compile play routes" + errorMsg)
+ }
+ }
+} \ No newline at end of file