Skip to content

Commit 6fb66f6

Browse files
authored
feat: allow custom formatjs_cli toolchain (#24)
### TL;DR Add support for custom FormatJS CLI versions with a new example demonstrating how to use version 0.1.0. ### What changed? - Enhanced the FormatJS CLI module extension to support custom binary versions - Added a new `formatjs_repositories` attribute to the `toolchain()` tag that accepts custom URLs and SHA256 checksums - Created a new example in `examples/custom_version/` demonstrating how to use a custom FormatJS CLI version (0.1.0) - Removed version 0.1.0 from the built-in versions list to demonstrate the custom version feature - Added `.lock` files to linguist-generated in `.gitattributes` ### How to test? 1. Navigate to the custom version example: ```bash cd examples/custom_version ``` 2. Build and test the example: ```bash bazel build :messages bazel test :test_extraction bazel test :test_cli_version ``` 3. Examine the extracted messages: ```bash cat bazel-bin/messages.json ``` ### Why make this change? This change provides flexibility for users who need to: - Test unreleased or pre-release versions of FormatJS CLI - Use custom builds or patched versions - Support platforms not yet in the official releases - Pin to specific versions for compatibility reasons The new API is inspired by the `rules_nodejs` pattern, making it familiar and easy to use for developers already working with Bazel JavaScript rules.
1 parent 3a2c0c0 commit 6fb66f6

12 files changed

Lines changed: 3544 additions & 49 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ examples export-ignore
99
# Occasionally there's a need to "stamp" the release version into a file
1010
# See https://blog.aspect.build/releasing-bazel-rulesets-rust for details if you need this.
1111
# formatjs/version.bzl export-subst
12+
*.lock linguist-generated

MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/aggregate/MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Example BUILD file demonstrating extraction with custom FormatJS CLI version."""
2+
3+
load("@rules_formatjs//formatjs:defs.bzl", "formatjs_extract")
4+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
5+
6+
# Simple TypeScript/React file with FormatJS messages
7+
genrule(
8+
name = "generate_source",
9+
outs = ["App.tsx"],
10+
cmd = """cat > $@ << 'EOF'
11+
import { FormattedMessage, defineMessages } from 'react-intl';
12+
13+
const messages = defineMessages({
14+
welcome: {
15+
id: 'app.welcome',
16+
defaultMessage: 'Welcome to the custom version example!',
17+
description: 'Welcome message on homepage',
18+
},
19+
goodbye: {
20+
id: 'app.goodbye',
21+
defaultMessage: 'Goodbye!',
22+
description: 'Farewell message',
23+
},
24+
});
25+
26+
export function App() {
27+
return (
28+
<div>
29+
<h1><FormattedMessage {...messages.welcome} /></h1>
30+
<p><FormattedMessage {...messages.goodbye} /></p>
31+
<FormattedMessage
32+
id="app.inline"
33+
defaultMessage="This is an inline message"
34+
description="Inline message example"
35+
/>
36+
</div>
37+
);
38+
}
39+
EOF
40+
""",
41+
)
42+
43+
# Extract messages using the custom FormatJS CLI version (0.1.0)
44+
# This demonstrates that custom platform configurations work correctly
45+
formatjs_extract(
46+
name = "messages",
47+
srcs = [":App.tsx"],
48+
out = "messages.json",
49+
)
50+
51+
# Test that we can read the extracted messages
52+
sh_test(
53+
name = "test_extraction",
54+
srcs = ["test_extraction.sh"],
55+
data = [":messages"],
56+
)
57+
58+
# Test that verifies we're using the custom CLI version (0.1.0)
59+
# This test accesses the CLI binaries directly to check their version
60+
sh_test(
61+
name = "test_cli_version",
62+
srcs = ["test_cli_version.sh"],
63+
data = [
64+
"@formatjs_v0_1_0_darwin_arm64//:cli",
65+
"@formatjs_v0_1_0_linux_x64//:cli",
66+
],
67+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Example demonstrating custom FormatJS CLI version configuration.
2+
3+
This example shows how to use a custom version (0.1.0) that is not in the built-in
4+
FORMATJS_CLI_VERSIONS dictionary. This is useful for:
5+
- Testing unreleased versions
6+
- Using custom builds
7+
- Supporting platforms not yet officially supported
8+
"""
9+
10+
module(name = "custom_version_example")
11+
12+
bazel_dep(name = "rules_formatjs", version = "")
13+
bazel_dep(name = "rules_shell", version = "0.3.0")
14+
15+
# Use local override since we're developing rules_formatjs
16+
local_path_override(
17+
module_name = "rules_formatjs",
18+
path = "../..",
19+
)
20+
21+
# Configure custom FormatJS CLI version 0.1.0
22+
# This version was removed from the built-in versions but we can still use it
23+
formatjs_cli = use_extension("@rules_formatjs//formatjs_cli:extensions.bzl", "formatjs_cli")
24+
25+
# Single toolchain call with custom binaries (similar to rules_nodejs pattern)
26+
formatjs_cli.toolchain(
27+
name = "formatjs_v0_1_0",
28+
formatjs_repositories = {
29+
"0.1.0.darwin-arm64": [
30+
"https://github.com/formatjs/formatjs/releases/download/formatjs_cli_v0.1.0/formatjs_cli-darwin-arm64",
31+
"9b2c736b48cc65e763cf19ac7c190e527f9a8d4aa0798185e602f58becb99feb",
32+
],
33+
"0.1.0.linux-x64": [
34+
"https://github.com/formatjs/formatjs/releases/download/formatjs_cli_v0.1.0/formatjs_cli-linux-x64",
35+
"884b9a41b9f6be649ea72277ebf22af0146043466d2ab94b28a57f95ffb7da1a",
36+
],
37+
},
38+
)
39+
40+
# Repository names are: {name}_{platform} and {name}_toolchains
41+
# Examples: formatjs_v0_1_0_darwin_arm64, formatjs_v0_1_0_linux_x64, formatjs_v0_1_0_toolchains
42+
use_repo(
43+
formatjs_cli,
44+
"formatjs_v0_1_0_darwin_arm64",
45+
"formatjs_v0_1_0_linux_x64",
46+
"formatjs_v0_1_0_toolchains",
47+
)
48+
49+
# Register the custom version toolchains (0.1.0)
50+
# Note: Standard toolchains (0.1.2) are already registered by rules_formatjs root MODULE.bazel
51+
# Bazel will select the appropriate one based on platform constraints and registration order
52+
register_toolchains(
53+
"@formatjs_v0_1_0_toolchains//:all",
54+
)

0 commit comments

Comments
 (0)