Status | Stardoc |
---|---|
Stardoc |
rules_twirl
compiles Twirl templates to Scala, so they can be used with bazelbuild/rules_scala
and higherkindness/rules_scala
.
Simple Core API: twirl_templates
For more information about Twirl templates, see the Play Twirl documentation.
Create a file called at the top of your repository named WORKSPACE
and add the following snippet to it.
# update version as needed
rules_twirl_version = "TODO"
http_archive(
name = "rules_twirl",
sha256 = "TODO",
strip_prefix = "rules_twirl-{}".format(rules_twirl_version),
type = "zip",
url = "https://github.com/lucidsoftware/rules_twirl/archive/{}.zip".format(rules_twirl_version),
)
# rules_jvm_external
rules_jvm_external_version = "6.2"
http_archive(
name = "rules_jvm_external",
sha256 = "aa39ecd47e16d5870eba817fe71476802bc371fe2724a2ddee565992df55f4af",
strip_prefix = "rules_jvm_external-{}".format(rules_jvm_external_version),
type = "zip",
url = "https://github.com/bazelbuild/rules_jvm_external/archive/{}.zip".format(rules_jvm_external_version),
)
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
rules_jvm_external_deps()
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
rules_jvm_external_setup()
load(
"@rules_twirl//:workspace.bzl",
"twirl_compiler_cli_2_13_repositories",
"twirl_compiler_cli_3_repositories",
)
twirl_compiler_cli_3_repositories()
load("@twirl_compiler_cli_3//:defs.bzl", twirl_compiler_cli_3_pinned_maven_install = "pinned_maven_install")
twirl_compiler_cli_3_pinned_maven_install()
twirl_compiler_cli_2_13_repositories()
load("@twirl_compiler_cli_2_13//:defs.bzl", twirl_compiler_cli_2_13_pinned_maven_install = "pinned_maven_install")
twirl_compiler_cli_2_13_pinned_maven_install()
# Twirl compiler
load("@rules_twirl//twirl-toolchain:register-toolchains.bzl", "twirl_register_toolchains")
twirl_register_toolchains(default_toolchain_name = "twirl-3")
This installs rules_twirl
to your WORKSPACE
and sets up toolchains for a Scala 2.13 and Scala 3 version of the Twirl compiler with Scala 3 being the default. To change the default to Scala 2.13, set default_toolchain_name = "twirl-2-13"
If you want to use a custom Twirl compiler, you can set up a custom toolchain in a BUILD.bazel file as follows:
load("@rules_twirl//twirl-toolchain:create-toolchain.bzl", "create_twirl_toolchain")
create_twirl_toolchain(
name = "twirl-custom",
twirl_compiler = "<label of your custom Twirl templates compiler>",
)
Then change the twirl_register_toolchains
in the WORKSPACE
file to use your custom toolchain:
twirl_register_toolchains(
default_toolchain_name = "twirl-custom",
toolchains = ["<label of your custom Twirl templates compiler>"]
)
You can find the available versions of the Twirl Compiler CLI on maven: https://mvnrepository.com/artifact/com.lucidchart/twirl-compiler-cli.
The twirl_templates
rule compiles Twirl templates to a source jar that can be used with the rules_scala
rules. For example,
twirl_templates(
name = "twirl-templates",
source_directory = "app",
srcs = glob(["app/**/*.scala.html"])
+ glob(["app/**/*.scala.xml"])
+ glob(["app/**/*.scala.js"])
+ glob(["app/**/*.scala.txt"]),
)
scala_binary(
name = "foo-service",
srcs = glob(["app/**/*.scala"]) + [":twirl-templates"],
main_class = "foo.server.RunServer",
deps = [...],
)
To override the default Twirl compiler for a single target, you can change the twirl_toolchain_name
attribute on the twirl_routes
target. That attribute can be set to the name of any twirl_toolchain
registered with twirl_register_toolchains
(and created using create_twirl_toolchain
). By default twirl-3
and twirl-2-13
are valid values.
For example:
twirl_templates(
name = "twirl-templates",
source_directory = "app",
srcs = glob(["app/**/*.scala.html"])
+ glob(["app/**/*.scala.xml"])
+ glob(["app/**/*.scala.js"])
+ glob(["app/**/*.scala.txt"]),
twirl_toolchain_name = "twirl-2-13",
)
See the Stardoc documentation for the full list of options for twirl_templates
.
twirl_templates
can be used with the rules_play_routes
to run a Play Framework Service. For example
twirl_templates(
name = "twirl-templates",
source_directory = "app",
include_play_imports = True,
srcs = glob(["app/**/*.scala.html"])
+ glob(["app/**/*.scala.xml"])
+ glob(["app/**/*.scala.js"])
+ glob(["app/**/*.scala.txt"]),
additional_imports = [...],
)
play_routes(
name = "play-routes",
srcs = ["conf/routes"] + glob(["conf/*.routes"]),
include_play_imports = True,
generate_reverse_router = True,
routes_imports = [...],
)
scala_binary(
name = "foo-service",
srcs = glob(["app/**/*.scala"]) + [":twirl-templates", ":play-routes"],
visibility = ["//visibility:public"],
main_class = "play.core.server.ProdServerStart",
resources = ["conf/logback.xml"] + glob(["conf/resources/**/*"]),
resource_strip_prefix = native.package_name(),
classpath_resources = ["conf/application.conf"],
jvm_flags = [
"-Dhttp.port=9000",
"-Dapplication.name=foo-service",
],
deps = [...],
)
This project consists of the Twirl Bazel rules and a command line Twirl compiler. The command line compiler can be built with
bazel build //twirl-compiler
It can be run with
bazel run //twirl-compiler
All tests can be run using
test/run_all_tests.sh
They can also be run using
bazel test //test/...
We use rules_jvm_external to import third party dependencies.
To make changes to the dependencies, simply update maven_install
in the appropriate workspace.bzl
file (workspace.bzl
for the main rules_twirl
implementation or test_workspace.bzl
for the tests), and then update the dependencies json file used by rules_jvm_external
by running the following script:
scripts/gen-deps.sh
Never modify the dependencies json file directly.
Before pushing your changes, make sure you update the documentation by running the following script:
scripts/gen-docs.sh
Failure to do so will result in CI failing.
To release a new version to Maven Central:
- Push a tag with this syntax:
P1.P2.P3
whereP1.P2.P3
is the Twirl version, e.g.,2.0.7
- Once the build completes (including the publish step), find the staging repo in Sonatype (assuming you're signed in and have access to the project)
- Verify all the artifacts are on the staging Repository, and then close it through the Sonatype GUI
- Once Sonatype's pre-release checks on the repository complete, release it through the Sonatype GUI
- Verify the artifact's present in Maven Central (it can take multiple hours for everything to sync)