diff options
-rw-r--r-- | contrib/playlib/src/mill/playlib/RouterModule.scala | 31 | ||||
-rw-r--r-- | contrib/playlib/test/resources/hello-world/core/routes/routes (renamed from contrib/playlib/test/resources/hello-world/core/conf/routes) | 0 | ||||
-rw-r--r-- | contrib/playlib/test/resources/hello-world/core/routes/sub.routes (renamed from contrib/playlib/test/resources/hello-world/core/conf/sub.routes) | 0 | ||||
-rw-r--r-- | contrib/playlib/test/resources/invalid/core/routes/routes (renamed from contrib/playlib/test/resources/invalid/core/conf/routes) | 0 | ||||
-rw-r--r-- | contrib/playlib/test/resources/invalidsub/core/routes/routes (renamed from contrib/playlib/test/resources/invalidsub/core/conf/routes) | 0 | ||||
-rw-r--r-- | contrib/playlib/test/resources/invalidsub/core/routes/sub.routes (renamed from contrib/playlib/test/resources/invalidsub/core/conf/sub.routes) | 0 | ||||
-rw-r--r-- | contrib/playlib/test/src/mill/playlib/HelloWorldTests.scala | 11 | ||||
-rw-r--r-- | docs/pages/9 - Contrib Modules.md | 31 |
8 files changed, 39 insertions, 34 deletions
diff --git a/contrib/playlib/src/mill/playlib/RouterModule.scala b/contrib/playlib/src/mill/playlib/RouterModule.scala index 63433f3b..ea2593aa 100644 --- a/contrib/playlib/src/mill/playlib/RouterModule.scala +++ b/contrib/playlib/src/mill/playlib/RouterModule.scala @@ -7,42 +7,23 @@ import mill.playlib.api.RouteCompilerType import mill.scalalib.Lib.resolveDependencies import mill.scalalib._ import mill.scalalib.api._ -import os.Path trait RouterModule extends mill.Module with ScalaModule { - /** - * Defines the version of playframework to be used to compile this projects - * routes. - */ def playVersion: T[String] override def generatedSources = T { super.generatedSources() ++ Seq(compileRouter().classes) } - /** - * The [[PathRef]] to the main routes file. - * - * This is the default path for play projects and it should be fine but you - * can override it if needed. - */ - def routesDirectory = T.sources { - millSourcePath / "conf" - } - - private def routesFiles = T { - val files = routesDirectory() - locateFilesBy(files, _.last.endsWith(".routes")) ++ locateFilesBy(files, _.last == "routes") - } + def routes = T.sources { millSourcePath / 'routes } - private def locateFilesBy(files: Seq[PathRef], p: Path => Boolean) = { - files.flatMap(file => { - os.walk(file.path).filter(p).map(f => PathRef(f)) - }) + private def routeFiles = T { + val paths = routes().flatMap(file => os.walk(file.path)) + val routeFiles=paths.filter(_.ext=="routes") ++ paths.filter(_.last == "routes") + routeFiles.map(f=>PathRef(f)) } - /** * A [[Seq]] of additional imports to be added to the routes file. * Defaults to : @@ -88,7 +69,7 @@ trait RouterModule extends mill.Module with ScalaModule { T.ctx().log.debug(s"compiling play routes with ${playVersion()} worker") RouteCompilerWorkerModule.routeCompilerWorker().compile( toolsClasspath().map(_.path), - routesFiles().map(_.path), + routeFiles().map(_.path), routesAdditionalImport, generateForwardsRouter, generateReverseRouter, diff --git a/contrib/playlib/test/resources/hello-world/core/conf/routes b/contrib/playlib/test/resources/hello-world/core/routes/routes index f71b0cdd..f71b0cdd 100644 --- a/contrib/playlib/test/resources/hello-world/core/conf/routes +++ b/contrib/playlib/test/resources/hello-world/core/routes/routes diff --git a/contrib/playlib/test/resources/hello-world/core/conf/sub.routes b/contrib/playlib/test/resources/hello-world/core/routes/sub.routes index 4597a19b..4597a19b 100644 --- a/contrib/playlib/test/resources/hello-world/core/conf/sub.routes +++ b/contrib/playlib/test/resources/hello-world/core/routes/sub.routes diff --git a/contrib/playlib/test/resources/invalid/core/conf/routes b/contrib/playlib/test/resources/invalid/core/routes/routes index 5c946fc6..5c946fc6 100644 --- a/contrib/playlib/test/resources/invalid/core/conf/routes +++ b/contrib/playlib/test/resources/invalid/core/routes/routes diff --git a/contrib/playlib/test/resources/invalidsub/core/conf/routes b/contrib/playlib/test/resources/invalidsub/core/routes/routes index 5c946fc6..5c946fc6 100644 --- a/contrib/playlib/test/resources/invalidsub/core/conf/routes +++ b/contrib/playlib/test/resources/invalidsub/core/routes/routes diff --git a/contrib/playlib/test/resources/invalidsub/core/conf/sub.routes b/contrib/playlib/test/resources/invalidsub/core/routes/sub.routes index 9c844a4a..9c844a4a 100644 --- a/contrib/playlib/test/resources/invalidsub/core/conf/sub.routes +++ b/contrib/playlib/test/resources/invalidsub/core/routes/sub.routes diff --git a/contrib/playlib/test/src/mill/playlib/HelloWorldTests.scala b/contrib/playlib/test/src/mill/playlib/HelloWorldTests.scala index 20e8ce55..54e8a057 100644 --- a/contrib/playlib/test/src/mill/playlib/HelloWorldTests.scala +++ b/contrib/playlib/test/src/mill/playlib/HelloWorldTests.scala @@ -85,21 +85,24 @@ object HelloWorldTests extends TestSuite { assert(unchangedEvalCount == 0) } - 'compileRouter - workspaceTest(HelloWorld, resourcePath = invalidResourcePath) { eval => + "compileRouter invalid routes"- workspaceTest(HelloWorld, resourcePath = invalidResourcePath) { + eval => val eitherResult = eval.apply(HelloWorld.core.compileRouter) val Left(Failure(message, x)) = eitherResult val expectedMessage = "Unable to compile play routes\ncompilation error in " + - HelloWorld.millSourcePath.toIO.getAbsolutePath + "/core/conf/routes at line 4, column" + + HelloWorld.core.millSourcePath.toIO.getAbsolutePath + "/routes/routes at line 4, " + + "column" + " 1: HTTP Verb (GET, POST, ...), include (->), comment (#), or modifier line (+) expected" assert( message == expectedMessage ) } - 'compileRouter - workspaceTest(HelloWorld, resourcePath = invalidSubResourcePath) { eval => + "compileRouter invalid sub.routes " - workspaceTest(HelloWorld, resourcePath = invalidSubResourcePath) { + eval => val eitherResult = eval.apply(HelloWorld.core.compileRouter) val Left(Failure(message, x)) = eitherResult val expectedMessage = "Unable to compile play routes\ncompilation error in " + - HelloWorld.millSourcePath.toIO.getAbsolutePath + "/core/conf/sub.routes at line 3, column" + + HelloWorld.core.millSourcePath.toIO.getAbsolutePath + "/routes/sub.routes at line 3, column" + " 1: HTTP Verb (GET, POST, ...), include (->), comment (#), or modifier line (+) expected" assert( message == expectedMessage diff --git a/docs/pages/9 - Contrib Modules.md b/docs/pages/9 - Contrib Modules.md index dbda9845..5524c23a 100644 --- a/docs/pages/9 - Contrib Modules.md +++ b/docs/pages/9 - Contrib Modules.md @@ -253,7 +253,7 @@ object app extends RouterModule { * `def playVersion: T[String]` (mandatory) - The version of play to use to compile the routes file. * `def scalaVersion: T[String]` - The scalaVersion in use in your project. - * `def routesFile: T[PathRef]` - The path to the main routes file. (Defaults to the standard play path : `conf/routes`.) + * `def conf: Sources` - The directory which contains your route files. (Defaults to : `routes/` ) * `def routesAdditionalImport: Seq[String]` - Additional imports to use in the generated routers. (Defaults to `Seq("controllers.Assets.Asset", "play.libs.F")` * `def generateForwardsRouter: Boolean = true` - Enables the forward router generation. * `def generateReverseRouter: Boolean = true` - Enables the reverse router generation. @@ -265,9 +265,11 @@ object app extends RouterModule { The following filesystem layout is expected by default: ```text -build.sc -conf/ - routes +. +├── app +│ └── routes +│ └── routes +└── build.sc ``` `RouterModule` adds the `compileRouter` task to the module: @@ -286,7 +288,7 @@ object app extends ScalaModule with RouterModule { } ``` -To add additional imports to all of the twirl templates: +To add additional imports to all of the routes: ```scala object app extends ScalaModule with RouterModule { def playVersion = "2.7.0" @@ -294,6 +296,25 @@ object app extends ScalaModule with RouterModule { } ``` +If you want to use playframework's default of storing the routes in `conf/` you can do the +follwing: +```scala +object app extends ScalaModule with RouterModule { + def playVersion = "2.7.0" + override def routesAdditionalImport = Seq("my.additional.stuff._", "my.other.stuff._") + override def routes = T.sources{ millSourcePath / 'conf } +} +``` + +which will work with the following directory structure: +```text +. +├── app +│ └── conf +│ └── routes +└── build.sc +``` + ## Thirdparty Mill Plugins ### DGraph |