2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-25 11:32:33 +00:00

feat: support location expansion in tar (#774)

This commit is contained in:
Sahin Yort 2024-03-01 14:51:47 -08:00 committed by Greg Magolan
parent 2ce45746dd
commit 907a5d42e6
No known key found for this signature in database
GPG key ID: 3ED3220F08EA5070
4 changed files with 36 additions and 8 deletions

5
docs/tar.md generated
View file

@ -100,7 +100,7 @@ Rule that executes BSD `tar`. Most users should use the [`tar`](#tar) macro, rat
## tar
<pre>
tar(<a href="#tar-name">name</a>, <a href="#tar-mtree">mtree</a>, <a href="#tar-kwargs">kwargs</a>)
tar(<a href="#tar-name">name</a>, <a href="#tar-mtree">mtree</a>, <a href="#tar-stamp">stamp</a>, <a href="#tar-kwargs">kwargs</a>)
</pre>
Wrapper macro around [`tar_rule`](#tar_rule).
@ -136,7 +136,8 @@ https://man.freebsd.org/cgi/man.cgi?mtree(8)
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="tar-name"></a>name | name of resulting <code>tar_rule</code> | none |
| <a id="tar-mtree"></a>mtree | "auto", or an array of specification lines, or a label of a file that contains the lines. | <code>"auto"</code> |
| <a id="tar-mtree"></a>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. | <code>"auto"</code> |
| <a id="tar-stamp"></a>stamp | should mtree attribute be stamped | <code>0</code> |
| <a id="tar-kwargs"></a>kwargs | additional named parameters to pass to <code>tar_rule</code> | none |

View file

@ -133,11 +133,11 @@ bzl_library(
name = "tar",
srcs = ["tar.bzl"],
deps = [
"//lib:expand_template",
"//lib:utils",
"//lib/private/docs:tar",
"@aspect_bazel_lib//lib:paths",
"@bazel_skylib//lib:types",
"@bazel_skylib//rules:write_file",
],
)

View file

@ -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")
@ -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
@ -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)
@ -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:

View file

@ -297,3 +297,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",
],
)