Status | Stardoc |
---|---|
Stardoc |
rules_twirl
compiles Twirl templates to
Scala, so they can be used with
bazelbuild/rules_scala
and
lucidsoftware/rules_scala
.
Simple Core API: twirl_templates
For more information about Twirl templates, see the Play Twirl documentation.
rules_twirl
isn't yet on the Bazel Central Registry, so you'll
need to pull it in via archive_override
. Be sure to replace <COMMIT>
with latest commit on
master
and <INTEGRITY>
with the hash suggested by Bazel after the dependency is first loaded.
/MODULE.bazel
bazel_dep(name = "rules_twirl")
rules_twirl_version = "<COMMIT>"
archive_override(
module_name = "rules_twirl",
integrity = "<INTEGRITY>",
strip_prefix = "rules_twirl-{}".format(rules_twirl_version),
urls = ["https://github.com/lucidsoftware/rules_twirl/archive/refs/heads/{}.zip".format(rules_twirl_version)],
)
By default, the Scala 3 version of the Twirl compiler will be used. To change the default to
Scala 2.13, add the --@rules_twirl//twirl-toolchain=twirl-2-13
flag to your .bazelrc
file.
If you want to use a custom Twirl compiler, you can set up a custom toolchain like so:
/BUILD.bazel
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, register your toolchain with Bazel and set it as the default in your .bazelrc
file:
/MODULE.bazel
register_toolchains("//:twirl-custom")
/.bazelrc
common --@rules_twirl//twirl-toolchain=twirl-custom
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 the appropriate maven.install
call in
MODULE.bazel
, 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)