play-routes-wire

An ad-hoc and tiny macro project for Play Framework with MacWire

License

License

Categories

Categories

Wire Data Data Structures
GroupId

GroupId

com.github.hiroshi-cl
ArtifactId

ArtifactId

play-routes-wire_2.12
Last Version

Last Version

2.8.1-0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

play-routes-wire
An ad-hoc and tiny macro project for Play Framework with MacWire
Project URL

Project URL

https://github.com/hiroshi-cl/play-routes-wire
Project Organization

Project Organization

hiroshi-cl
Source Code Management

Source Code Management

https://github.com/hiroshi-cl/play-routes-wire

Download play-routes-wire_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hiroshi-cl/play-routes-wire_2.12/ -->
<dependency>
    <groupId>com.github.hiroshi-cl</groupId>
    <artifactId>play-routes-wire_2.12</artifactId>
    <version>2.8.1-0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.hiroshi-cl/play-routes-wire_2.12/
implementation 'com.github.hiroshi-cl:play-routes-wire_2.12:2.8.1-0.1.0'
// https://jarcasting.com/artifacts/com.github.hiroshi-cl/play-routes-wire_2.12/
implementation ("com.github.hiroshi-cl:play-routes-wire_2.12:2.8.1-0.1.0")
'com.github.hiroshi-cl:play-routes-wire_2.12:jar:2.8.1-0.1.0'
<dependency org="com.github.hiroshi-cl" name="play-routes-wire_2.12" rev="2.8.1-0.1.0">
  <artifact name="play-routes-wire_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.hiroshi-cl', module='play-routes-wire_2.12', version='2.8.1-0.1.0')
)
libraryDependencies += "com.github.hiroshi-cl" % "play-routes-wire_2.12" % "2.8.1-0.1.0"
[com.github.hiroshi-cl/play-routes-wire_2.12 "2.8.1-0.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-compiler jar 2.12.10
org.scala-lang : scala-library jar 2.12.10
org.scala-lang : scala-reflect jar 2.12.10
com.typesafe.play : play_2.12 jar 2.8.1

provided (1)

Group / Artifact Type Version
com.softwaremill.macwire : macros_2.12 jar 2.3.3

Project Modules

There are no modules declared in this project.

play-routes-wire

This is an ad-hoc and tiny macro project for Play Framework with MacWire. This macro automatically wires all controllers for given generated routes classes.

Motivation

If you use MacWire, you have to write as below:

// define controller
class GreeterController(greetingService: GreetingService,
                                  langs: Langs,
                                  cc: ControllerComponents) extends AbstractController(cc)

// define an application loader and a component
class GreetingApplicationLoader extends ApplicationLoader {
  def load(context: Context): Application = new GreetingComponents(context).application
}

class GreetingComponents(context: Context) extends BuiltInComponentsFromContext(context)
  with ServicesModule
  with AssetsComponents
  with I18nComponents
  with play.filters.HttpFiltersComponents {

  // wire *all* controllers
  lazy val greeterController = wire[GreeterController]

  // wire a router
  lazy val router: Router = new Routes(httpErrorHandler, greeterController, assets)
}

If you use Guice, what you have to do is only to insert @Inject() as below:

// ↓ Only this!
@Inject() class GreeterController(greetingService: GreetingService,
                                  langs: Langs,
                                  cc: ControllerComponents) extends AbstractController(cc)

It is unreasonable! Can you think the case your project has hundreds of controllers?

This macro helps to wire all controllers.

Usage

Add following code to your build.sbt:

libraryDependencies += "com.github.hiroshi-cl" %% "play-routes-wire" % "2.8.1-0.1.0"
  • Make sure that all controllers are subclass of ControllerHelpers.
  • Make sure that all depending services are found in the scope.

Write as below:

class GreetingComponents(context: Context) extends BuiltInComponentsFromContext(context)
  with ServicesModule
  with AssetsComponents
  with I18nComponents
  with play.filters.HttpFiltersComponents {

  lazy val router: Router = Macro.wireRoutes[Routes](httpErrorHandler, "/")
}
  • Supporting Play versions: 2.6.x, 2.7.x, 2.8.x
  • Supporting MacWire versions: 2.3.x

Versions

Version
2.8.1-0.1.0
2.7.4-0.1.0
2.6.25-0.1.0