Skip to content

Commit

Permalink
Reintegrate the compiler CLI into this repo, implement toolchains, an…
Browse files Browse the repository at this point in the history
…d update all the things (#55)

* Update all the things, update to annex latest, and add worker support

Heads up that there's a circular dependency between the cli repo and
this repo, so things will be broken until the new cli gets published and
then another commit to this repo is made.

* Use toolchains instead of bind

* Move the Play Routes Compiler CLI back into this repo and improve toolchains

* Drop Bazel 6 support and update Bazel 7 version

* Support path mapping and better support cancellation
  • Loading branch information
jjudd authored Oct 15, 2024
1 parent 1705c23 commit a3048c2
Show file tree
Hide file tree
Showing 64 changed files with 14,057 additions and 8,928 deletions.
20 changes: 13 additions & 7 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
startup --expand_configs_in_place

build --javabase="//:jdk"
build --java_toolchain="@bazel_tools//tools/jdk:toolchain_hostjdk8"
build --host_javabase="//:jdk"
build --host_java_toolchain="@bazel_tools//tools/jdk:toolchain_hostjdk8"
build --incompatible_java_common_parameters=false

common:ci --color=yes

build:ci --verbose_failures
build --strategy=worker,sandboxed,local
build --verbose_failures

build --incompatible_strict_action_env
build --experimental_worker_multiplex_sandboxing
build --experimental_worker_cancellation
build --worker_sandboxing

build --java_language_version="21"
build --java_runtime_version="remotejdk_21"
build --tool_java_language_version="21"
build --tool_java_runtime_version="remotejdk_21"

build:ci --sandbox_debug
build:ci --spawn_strategy=standalone
build:ci --genrule_strategy=standalone
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bazelbuild/4.2.4
bazelbuild/7.3.1
33 changes: 28 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,37 @@ jobs:
USE_BAZEL_VERSION: ${{ matrix.bazel_version }}
strategy:
matrix:
os: [ubuntu-20.04]
bazel_version: [bazelbuild/4.2.4]
os: [ubuntu-24.04]
bazel_version: [bazelbuild/7.3.1]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm install -g @bazel/bazelisk
- uses: actions/[email protected]
- uses: actions/[email protected]
- uses: bazel-contrib/[email protected]
with:
bazelisk-version: 1.20.0
- run: cat .bazelrc.ci >> .bazelrc
- run: ./scripts/format.sh check
- run: ./test/run_all_tests.sh ci
- run: ./scripts/gen-docs.sh && git diff --exit-code docs/
- run: bazel shutdown

publish:
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
needs: build-and-test
runs-on: ubuntu-24.04
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
- uses: bazel-contrib/[email protected]
with:
bazelisk-version: 1.20.0
- run: cat .bazelrc.ci >> .bazelrc
- name: Import gpg keys
run: echo "$PGP_SECRET" | base64 --decode | gpg --import
env:
PGP_SECRET: ${{ secrets.PGP_SECRET }}
- name: Publish to Maven
run: ./scripts/publish.sh
env:
MAVEN_USER: ${{ secrets.SONATYPE_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
25 changes: 0 additions & 25 deletions BUILD

This file was deleted.

27 changes: 27 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load(
"@rules_java//toolchains:default_java_toolchain.bzl",
"DEFAULT_JAVACOPTS",
"DEFAULT_TOOLCHAIN_CONFIGURATION",
"default_java_toolchain",
)

default_java_toolchain(
name = "repository_default_toolchain_21",
configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
java_runtime = "@rules_java//toolchains:remotejdk_21",
javac_supports_worker_multiplex_sandboxing = True,
# some of the default options make scala compilation fail in the test package
misc = [opt for opt in DEFAULT_JAVACOPTS if not opt.startswith("-Xep")],
source_version = "21",
target_version = "21",
)

buildifier(
name = "buildifier",
)

buildifier(
name = "buildifier_check",
mode = "check",
)
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
1,630 changes: 1,630 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

183 changes: 106 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,110 @@ Simple Core API: [play_routes](docs/stardoc/play-routes.md)
For more information about the Play Framework, see [the Play documentation](https://www.playframework.com/documentation/latest).

## Installation
Create a file at the top of your repository named `WORKSPACE` and add the following snippet to it.
Add the following to your `WORKSPACE` file:

```python
# update version as needed
rules_play_routes_version = "bfaca5f186f2c3b989c80fd00f37a53b84406b3d"
rules_play_routes_version = "TODO"
http_archive(
name = "io_bazel_rules_play_routes",
sha256 = "b0ae17af402e88da31fa41b16a6cf1d8eea53d693dd6b4c0c219d421078a2af5",
strip_prefix = "rules_play_routes-{}".format(rules_play_routes_version),
type = "zip",
url = "https://github.com/lucidsoftware/rules_play_routes/archive/{}.zip".format(rules_play_routes_version),
name = "rules_play_routes",
sha256 = "TODO",
strip_prefix = "rules_play_routes-{}".format(rules_play_routes_version),
type = "zip",
url = "https://github.com/lucidsoftware/rules_play_routes/archive/{}.zip".format(rules_play_routes_version),
)

RULES_JVM_EXTERNAL_TAG = "2.9"
# rules_jvm_external
rules_jvm_external_version = "6.2"

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

load("@io_bazel_rules_play_routes//:workspace.bzl", "play_routes_repositories")
play_routes_repositories("2.7")
load("@play_routes//:defs.bzl", play_routes_pinned_maven_install = "pinned_maven_install")
play_routes_pinned_maven_install()

bind(
name = "default-play-routes-compiler-cli",
actual = "@io_bazel_rules_play_routes//default-compiler-clis:scala_2_12_play_2_7"
)
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()

This installs `rules_play_routes` to your `WORKSPACE` and binds the default play routes compiler cli the rules will use. Update the commit as needed.
load(
"@rules_play_routes//:workspace.bzl",
"play_routes_compiler_cli_2_13_repositories",
"play_routes_compiler_cli_3_repositories",
)
play_routes_compiler_cli_3_repositories()
load("@play_routes_compiler_cli_3//:defs.bzl", play_routes_compiler_cli_3_pinned_maven_install = "pinned_maven_install")
play_routes_compiler_cli_3_pinned_maven_install()

In the above example, the play routes compiler cli for Scala 2.12 and Play 2.7 is used. However, you can specify a different compiler.
play_routes_compiler_cli_2_13_repositories()
load("@play_routes_compiler_cli_2_13//:defs.bzl", play_routes_compiler_cli_2_13_pinned_maven_install = "pinned_maven_install")
play_routes_compiler_cli_2_13_pinned_maven_install()

We provide 6 default compilers:
# Play routes compiler
load("@rules_play_routes//play-routes-toolchain:register-toolchains.bzl", "play_routes_register_toolchains")
play_routes_register_toolchains(default_toolchain_name = "play-routes-3")
```

- For Scala 2.11 + Play 2.5: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_11_play_2_5`
- For Scala 2.11 + Play 2.6: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_11_play_2_6`
- For Scala 2.11 + Play 2.7: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_11_play_2_7`
- For Scala 2.12 + Play 2.6: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_12_play_2_6`
- For Scala 2.12 + Play 2.7: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_12_play_2_7`
- For Scala 2.12 + Play 2.8: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_12_play_2_8`
- For Scala 2.13 + Play 2.8: `@io_bazel_rules_play_routes//default-compiler-clis:scala_2_13_play_2_8`
This installs `rules_play_routes` to your `WORKSPACE` and sets up toolchains for a Scala 2.13 and Scala 3 version of the Play routes compiler with Scala 3 being the default. To change the default to Scala 2.13, set `default_toolchain_name = "play-routes-2-13"`

To bind one of the default compilers, simply specify the correct Play version in the call to `play_routes_repositories` and update the bind statement:
If you want to use a custom Play routes compiler, you can set up a custom toolchain in a BUILD.bazel file as follows:
```python
play_routes_repositories(<Play Version>, <Optional Scala Version, see below>)
load("@play_routes//:defs.bzl", play_routes_pinned_maven_install = "pinned_maven_install")
play_routes_pinned_maven_install()
load("@rules_play_routes//play-routes-toolchain:create-toolchain.bzl", "create_play_routes_toolchain")

bind(
name = "default-play-routes-compiler-cli",
actual = <Default Compiler Label>
create_play_routes_toolchain(
name = "play-routes-custom",
play_routes_compiler = "<label of your custom Play routes compiler>",
)
```

Note: play_routes_respositories by default only needs to know the Play version. By default, there's no special config
for the Scala version (just make sure you bind the right compiler label).
Then change the `play_routes_register_toolchains` in the `WORKSPACE` file to use your custom toolchain:
```python
play_routes_register_toolchains(
default_toolchain_name = "play-routes-custom",
toolchains = ["<label of your custom Play routes compiler>"]
)
```

You can optionally include the Scala version, this might be needed for certain versions of Play.
You can find the available versions of the Twirl Compiler CLI on maven: https://mvnrepository.com/artifact/com.lucidchart/play-routes-compiler-cli.

## Usage
The `play_routes` rule compiles Play routes files to a source jar that can be used with the `rules_scala` rules. For example,

```python
load("@io_bazel_rules_play_routes//play-routes:play-routes.bzl", "play_routes")
load("@rules_play_routes//play-routes:play-routes.bzl", "play_routes")

play_routes(
name = "play-routes",
srcs = ["conf/routes"] + glob(["conf/*.routes"]),
include_play_imports = True,
generate_reverse_router = True,
routes_imports = [...],
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"]) + [":play-routes"],
main_class = "foo.server.RunServer",
deps = [...]
)
name = "foo-service",
srcs = glob(["app/**/*.scala"]) + [":play-routes"],
main_class = "foo.server.RunServer",
deps = [...]
)
```

### Overriding the default Play routes compiler
To override the default Play routes compiler for a single target, you can change the `play_routes_toolchain_name` attribute on the `play_routes` target. That attribute can be set to the name of any `play_routes_toolchain` registered with `play_routes_register_toolchains` (and created using `create_play_routes_toolchain`). By default `play-routes-3` and `play-routes-2-13` are valid values.

For example:
```python
play_routes(
name = "play-routes",
srcs = ["conf/routes"] + glob(["conf/*.routes"]),
include_play_imports = True,
generate_reverse_router = True,
routes_imports = [...],
play_routes_toolchain_name = "play-routes-2-13",
)
```

Expand All @@ -118,30 +137,30 @@ twirl_templates(
)

play_routes(
name = "play-routes",
srcs = ["conf/routes"] + glob(["conf/*.routes"]),
include_play_imports = True,
generate_reverse_router = True,
routes_imports = [...],
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",
# To make your static assets work:
"//public",
] + glob(["conf/resources/**/*"]),
resource_strip_prefix = native.package_name(),
classpath_resources = ["conf/application.conf"],
jvm_flags = [
"-Dhttp.port=9000",
"-Dapplication.name=foo-service",
],
deps = [...],
name = "foo-service",
srcs = glob(["app/**/*.scala"]) + [":twirl-templates", ":play-routes"],
visibility = ["//visibility:public"],
main_class = "play.core.server.ProdServerStart",
resources = [
"conf/logback.xml",
# To make your static assets work:
"//public",
] + glob(["conf/resources/**/*"]),
resource_strip_prefix = native.package_name(),
classpath_resources = ["conf/application.conf"],
jvm_flags = [
"-Dhttp.port=9000",
"-Dapplication.name=foo-service",
],
deps = [...],
)
```

Expand All @@ -155,21 +174,23 @@ filegroup(
)
```

If you want to have webjars support (https://www.playframework.com/documentation/2.8.1/AssetsOverview#WebJars),
If you want to have webjars support (https://www.playframework.com/documentation/3.0.x/AssetsOverview#WebJars),
then check out https://github.com/gergelyfabian/rules_play_utils.

## Development
### Command Line Play Routes Compiler
This project consists of the Play routes Bazel rules and a command line Play routes compiler compiler. The command line compiler can be built with
This project consists of the Play routes Bazel rules and a command line Play routes compiler. The command line compiler can be built with
```bash
bazel build //play-routes-compiler
bazel build //play-routes-compiler-cli:play-routes-compiler-cli-3
```

It can be run with
```bash
bazel run //play-routes-compiler
bazel run //play-routes-compiler-cli:play-routes-compiler-cli-3
```

There is a 2.13 verison of the routes compiler: `//play-routes-compiler-cli:play-routes-compiler-cli-2-13`

### Testing
All tests can be run using

Expand Down Expand Up @@ -197,3 +218,11 @@ Before pushing your changes, make sure you update the documentation by running t
scripts/gen-docs.sh
```
Failure to do so will result in CI failing.

## Releasing
To release a new version to Maven Central:
1. Push a tag with this syntax: `P1.P2.P3` where `P1.P2.P3` is the Play version, e.g., `3.0.4`
2. Once the build completes (including the publish step), find the [staging repo in Sonatype](https://oss.sonatype.org/#stagingRepositories) (assuming you're signed in and have access to the project)
3. Verify all the artifacts are on the staging Repository, and then close it through the Sonatype GUI
4. Once Sonatype's pre-release checks on the repository complete, release it through the Sonatype GUI
5. Verify the artifact's present in [Maven Central](https://search.maven.org/search?q=com.lucidchart) (it can take multiple hours for everything to sync)
Loading

0 comments on commit a3048c2

Please sign in to comment.