diff options
Diffstat (limited to 'contrib/playlib/worker')
-rw-r--r-- | contrib/playlib/worker/2.6.0/src/RouteCompilerWorker.scala | 58 | ||||
-rw-r--r-- | contrib/playlib/worker/2.7.0/src/RouteCompilerWorker.scala | 64 |
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 |