Skip to content

Commit

Permalink
feat: Support info and version file in rules_task (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvgijssel authored Mar 1, 2024
1 parent f29a818 commit 2fc3870
Show file tree
Hide file tree
Showing 20 changed files with 353 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ query:quiet --ui_event_filters=-info --noshow_progress
# Disable the bazel symlinks
common --experimental_convenience_symlinks=ignore

common --workspace_status_command=tools/bazel/workspace_status.sh

# Print warning messages about test suite being configured as too small/big
test --test_verbose_timeout_warnings

Expand Down
5 changes: 5 additions & 0 deletions .changeset/bunq2ynab-purple-windows-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bunq2ynab": patch
---

chore(deps): Upgrade bazel_skylib and bazel_skylib
5 changes: 5 additions & 0 deletions .changeset/provisioner-soft-tables-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"provisioner": patch
---

chore(deps): Upgrade bazel_skylib and bazel_skylib
5 changes: 5 additions & 0 deletions .changeset/rules_release-thirty-kiwis-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rules_release": patch
---

chore(deps): Upgrade bazel_skylib and bazel_skylib
5 changes: 5 additions & 0 deletions .changeset/rules_task-old-apples-sneeze-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rules_task": patch
---

chore(deps): Upgrade bazel_skylib and bazel_skylib
5 changes: 5 additions & 0 deletions .changeset/rules_task-old-apples-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rules_task": minor
---

feat: Support loading version file and info file into tasks
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ bazel_dep(
# ------------------------------------ rules_skylib ------------------------------------ #
bazel_dep(
name = "bazel_skylib",
version = "1.4.2",
version = "1.5.0",
)

# ------------------------------------ aspect_bazel_lib ------------------------------------ #
bazel_dep(
name = "aspect_bazel_lib",
version = "2.0.3",
version = "2.5.0",
)

bazel_lib_toolchains = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")
Expand Down
25 changes: 1 addition & 24 deletions rules/rules_release/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_task//task:defs.bzl", "cmd")
load("//tools:defs.bzl", "publish_github_release", release = "release_changed_files")
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages()
Expand Down Expand Up @@ -48,36 +48,13 @@ filegroup(
],
)

mtree_spec(
name = "release_archive_mtree",
srcs = [
":all_files",
],
)

# Remove `../rules_release~override` from each line because otherwise
# this results in an empty archive when extracting.
genrule(
name = "release_archive_mtree_normalized",
srcs = [
":release_archive_mtree",
],
outs = [
"release_archive_mtree_normalized.spec",
],
cmd = """
sed 's|../{}/||' $< > $@
""".format(repository_name().removeprefix("@")),
)

tar(
name = "release_archive",
srcs = [
":all_files",
],
out = "rules_release.tar.gz",
compress = "gzip",
mtree = ":release_archive_mtree_normalized",
)

genrule(
Expand Down
4 changes: 2 additions & 2 deletions rules/rules_release/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ bazel_dep(name = "platforms", version = "0.0.8")
# ------------------------------------ bazel_skylib ------------------------------------ #
bazel_dep(
name = "bazel_skylib",
version = "1.4.2",
version = "1.5.0",
)

# ------------------------------------ aspect_bazel_lib ------------------------------------ #
bazel_dep(name = "aspect_bazel_lib", version = "2.0.3")
bazel_dep(name = "aspect_bazel_lib", version = "2.5.0")

# ------------------------------------ rules_java ------------------------------------ #
bazel_dep(name = "rules_java", version = "7.0.6")
Expand Down
6 changes: 3 additions & 3 deletions rules/rules_release/release/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def rules_release_bazel_dependencies():

http_archive(
name = "bazel_skylib",
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
],
)

Expand Down
25 changes: 1 addition & 24 deletions rules/rules_task/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_release//tools:defs.bzl", "publish_github_release", release = "release_changed_files")
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("//task:defs.bzl", "cmd")
load("//tools:defs.bzl", "compile_pip_requirements")

Expand All @@ -22,36 +22,13 @@ filegroup(
],
)

mtree_spec(
name = "release_archive_mtree",
srcs = [
":all_files",
],
)

# Remove `../rules_task~override` from each line because otherwise
# this results in an empty archive when extracting.
genrule(
name = "release_archive_mtree_normalized",
srcs = [
":release_archive_mtree",
],
outs = [
"release_archive_mtree_normalized.spec",
],
cmd = """
sed 's|../{}/||' $< > $@
""".format(repository_name().removeprefix("@")),
)

tar(
name = "release_archive",
srcs = [
":all_files",
],
out = "rules_task.tar.gz",
compress = "gzip",
mtree = ":release_archive_mtree_normalized",
)

genrule(
Expand Down
4 changes: 2 additions & 2 deletions rules/rules_task/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module(
version = "0.0.0",
)

bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "aspect_bazel_lib", version = "1.36.0")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.5.0")

# ------------------------------------ rules_python ------------------------------------ #
bazel_dep(name = "rules_python", version = "0.29.0")
Expand Down
88 changes: 85 additions & 3 deletions rules/rules_task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ You can use the `cmd.executable` AST node to reference other **executable** targ
task(
name = "executable",
cmds = [
"$my_executable",
"$tool",
],
env = {
"tool": cmd.executable("my_executable"),
Expand All @@ -141,8 +141,8 @@ You can use the `cmd.file` and `cmd.files` AST nodes to reference other targets.
task(
name = "file",
cmds = [
"echo $my_file",
"cat $my_file_group",
"echo $file",
"cat $files",
],
env = {
"file": cmd.file("my_file"),
Expand All @@ -151,6 +151,58 @@ task(
)
```

### Using variables from Bazel workspace_status_command / stamping

If you add the following variable to the `workspace_status_command`:

```bash
#!/bin/bash
echo "STABLE_SOME_VAR BAR"
```

Add add `stamp_stable` attribute to the `task` rule, you can use the variable in the task:

```bazel
task(
name = "stamp_stable",
cmds = [
"echo $STABLE_SOME_VAR",
],
stamp_stable = True,
)
```

This will print:

```bash
FOO
```

The same goes for volatile variables which has the benefit of not invalidating the Bazel cache if only the volatile variable changes ([docs](https://bazel.build/docs/user-manual#workspace-status)). With the following `workspace_status_command`:

```bash
#!/bin/bash
echo "SOME_OTHER_VAR BAR"
```

And the `stamp_volatile` attribute:

```bazel
task(
name = "stamp_volatile",
cmds = [
"echo $SOME_OTHER_VAR",
],
stamp_volatile = True,
)
```

This will print:

```bash
BAR
```

### More examples

For more examples, see the [tests](tests/BUILD.bazel).
Expand Down Expand Up @@ -254,6 +306,36 @@ See [Referencing outputs of other targets](#referencing-outputs-of-other-targets

See [Referencing outputs of other targets](#referencing-outputs-of-other-targets)

### `cmd.version_file`

Instead of using the `stamp_stable` and `stamp_volatile` flags, you can use the `cmd.version_file` node to reference the file containing the stable variables. Given a `workspace_status_command` like:

```bash
#!/bin/bash
echo "VOLATILE_SOME_VAR BAR"
```

And running `cat` on the the version file

```bazel
task(
name = "version_file",
cmds = [
cmd.shell("cat", cmd.version_file()),
],
)
```

This will print

```bash
export VOLATILE_SOME_VAR='BAR'
```

### `cmd.info_file`

This is the same as `cmd.version_file` but for the info file, this gives you the volatile variables.

### `cmd.executable`

See [Using executable targets](#using-executable-targets)
Expand Down
7 changes: 7 additions & 0 deletions rules/rules_task/task/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
load("@rules_python//python:defs.bzl", "py_binary")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "all_files",
srcs = glob(["**/*"]),
)

py_binary(
name = "dotenv_convert",
srcs = ["dotenv_convert.py"],
)
16 changes: 16 additions & 0 deletions rules/rules_task/task/private/cmd.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def _wrap_env(env):
env[key] = cmd.string(value)
return env

def _wrap_env_file(node):
if type(node) == "string":
node = cmd.string(node)

return node

def _wrap_shell_args(args):
result = []

Expand Down Expand Up @@ -88,6 +94,10 @@ cmd = struct(
"type": "env",
"env": _wrap_env(env),
},
env_file = lambda node: {
"type": "env_file",
"node": _wrap_env_file(node),
},
defer = lambda node: {
"type": "defer",
"node": _wrap_defer(node),
Expand All @@ -100,6 +110,12 @@ cmd = struct(
"type": "file",
"label": fq_label(label),
},
version_file = lambda: {
"type": "version_file",
},
info_file = lambda: {
"type": "info_file",
},
files = lambda label: {
"type": "files",
"label": fq_label(label),
Expand Down
25 changes: 25 additions & 0 deletions rules/rules_task/task/private/dotenv_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
import shlex

_, input_file, output_file = sys.argv

with open(input_file, "r") as file:
bazel_content = file.read()

results = []

for line in bazel_content.split("\n"):
if not line:
continue

key, value = line.split(" ", 1)
safe_value = shlex.quote(value)

results.append(f"export {key}={safe_value}")

with open(output_file, "w") as file:
file.write("\n".join(results))


print(f"input_file: {input_file}")
print(f"output_file: {output_file}")
Loading

0 comments on commit 2fc3870

Please sign in to comment.