Skip to content

Commit

Permalink
feat: support location expansion in tar (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn authored Mar 1, 2024
1 parent 834f9c9 commit 197b2da
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
5 changes: 3 additions & 2 deletions docs/tar.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ bzl_library(
name = "tar",
srcs = ["tar.bzl"],
deps = [
"//lib:expand_template",
"//lib:utils",
"//lib/private:tar",
"@bazel_skylib//lib:types",
"@bazel_skylib//rules:write_file",
],
)

Expand Down
19 changes: 14 additions & 5 deletions lib/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TODO:
"""

load("@bazel_skylib//lib:types.bzl", "types")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//lib:expand_template.bzl", "expand_template")
load("//lib:utils.bzl", "propagate_common_rule_attributes")
load("//lib/private:tar.bzl", _tar = "tar", _tar_lib = "tar_lib")

Expand All @@ -64,7 +64,7 @@ tar_rule = _tar

tar_lib = _tar_lib

def tar(name, mtree = "auto", **kwargs):
def tar(name, mtree = "auto", stamp = 0, **kwargs):
"""Wrapper macro around [`tar_rule`](#tar_rule).
### Options for mtree
Expand Down Expand Up @@ -94,6 +94,9 @@ def tar(name, mtree = "auto", **kwargs):
Args:
name: name of resulting `tar_rule`
mtree: "auto", or an array of specification lines, or a label of a file that contains the lines.
Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables)
and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution.
stamp: should mtree attribute be stamped
**kwargs: additional named parameters to pass to `tar_rule`
"""
mtree_target = "_{}.mtree".format(name)
Expand All @@ -105,12 +108,18 @@ def tar(name, mtree = "auto", **kwargs):
**propagate_common_rule_attributes(kwargs)
)
elif types.is_list(mtree):
write_file(
expand_template(
name = mtree_target,
out = "{}.txt".format(mtree_target),
data = kwargs["srcs"],
# Ensure there's a trailing newline, as bsdtar will ignore a last line without one
content = mtree + [""],
newline = "unix",
template = ["#mtree", "{content}", ""],
substitutions = {
# expand_template only expands strings in "substitions" dict. Here
# we expand mtree and then replace the template with expanded mtree.
"{content}": "\n".join(mtree),
},
stamp = stamp,
**propagate_common_rule_attributes(kwargs)
)
else:
Expand Down
18 changes: 18 additions & 0 deletions lib/tests/tar/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,21 @@ assert_archive_contains(
"LICENSE",
],
)

# Case 10: Can reference generated files
tar(
name = "tar_location_expansion",
srcs = ["@bazel_skylib//:LICENSE"],
out = "10.tar",
mtree = [
"a uid=0 gid=0 time=1672560000 mode=0755 type=file content=$(location @bazel_skylib//:LICENSE)",
],
)

assert_tar_listing(
name = "test_tar_location_expansion",
actual = "tar_location_expansion",
expected = [
"-rwxr-xr-x 0 0 0 11358 Jan 1 2023 a",
],
)

0 comments on commit 197b2da

Please sign in to comment.