Skip to content
Bazel rules for compiling Twirl templates
Starlark Shell Scala Other
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
compiler-cli
docs/stardoc
scripts
test
twirl
.bazelignore
.bazelrc
.bazelrc.travis
.gitignore
.travis.yml
BUILD.bazel
LICENSE
README.md
WORKSPACE
test_workspace.bzl
twirl_install.json
twirl_test_install.json
workspace.bzl

README.md

Twirl Template Rules for Bazel

Travis Doc
Build Status Stardoc

Overview

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.

Installation

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 = "f0d0cc6951796e0f530ed1470f1b93930e6cb3b1"
http_archive(
  name = "io_bazel_rules_twirl",
  sha256 = "d52b3db5486f94bad5e0638136b23860b5a7105a35adc60094e1ad60c06c38dc",
  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_TAG = "2.9"
http_archive(
    name = "rules_jvm_external",
    sha256 = "e5b97a31a3e8feed91636f42e19b11c49487b85e5de2f387c999ea14d77c7f45",
    strip_prefix = "rules_jvm_external-{}".format(RULES_JVM_EXTERNAL_TAG),
    type = "zip",
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/{}.zip".format(RULES_JVM_EXTERNAL_TAG),
)

load("@io_bazel_rules_twirl//:workspace.bzl", "twirl_repositories")
twirl_repositories()
load("@twirl//:defs.bzl", twirl_pinned_maven_install = "pinned_maven_install")
twirl_pinned_maven_install()

This installs rules_twirl to your WORKSPACE at the specified commit. Update the commit as needed.

Usage

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 = [...]
  )
)

See the Stardoc documentation for the full list of options for twirl_templates.

Use with the Play Framework

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 = [...],
)

Development

Command Line Twirl Compiler

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

Testing

All tests can be run using

test/run_all_tests.sh

They can also be run using

bazel test //test/...

Updating Third Party Dependencies

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.

Updating Stardoc

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.

You can’t perform that action at this time.