Skip to content

Commit

Permalink
feat: mtree_spec now supports symlinks
Browse files Browse the repository at this point in the history
Changing mtree_spec so it can honor symlinks, if the target is a
directory then the symlink will be to the directory, we don't need to
expand those directories to make sure we follow the structure as
expected.
  • Loading branch information
manuelnaranjo committed Jun 18, 2024
1 parent 3330c38 commit 58ac6c0
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/private/tar.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Implementation of tar rule"

load("//lib:paths.bzl", "to_repository_relative_path")
load("//lib:paths.bzl", "relative_file", "to_repository_relative_path")

TAR_TOOLCHAIN_TYPE = "@aspect_bazel_lib//lib:tar_toolchain_type"

Expand Down Expand Up @@ -160,7 +160,7 @@ def _tar_impl(ctx):

return DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))

def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0755"):
def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "1672560000", mode = "0755", link = None):
spec = [
file,
"uid=" + uid,
Expand All @@ -169,7 +169,9 @@ def _mtree_line(file, type, content = None, uid = "0", gid = "0", time = "167256
"mode=" + mode,
"type=" + type,
]
if content:
if link:
spec.append("link=" + link)
elif content:
spec.append("content=" + content)
return " ".join(spec)

Expand Down Expand Up @@ -200,6 +202,21 @@ def _expand(file, expander, transform = to_repository_relative_path):
lines.append(_mtree_line(_vis_encode(path), "file", content = _vis_encode(e.path)))
return lines

def _expand_symlink(symlink, workspace_name = None):
source = _to_rlocation_path(symlink.target_file, workspace_name)
path = "{}/{}".format(workspace_name, symlink.path)

lines = []

segments = path.split("/")
for i in range(1, len(segments)):
parent = "/".join(segments[:i])
lines.append(_mtree_line(parent, "dir"))

link = _vis_encode(relative_file(source, path))
lines.append(_mtree_line(_vis_encode(path), "link", link = link))
return lines

def _mtree_impl(ctx):
out = ctx.outputs.out or ctx.actions.declare_file(ctx.attr.name + ".spec")

Expand Down Expand Up @@ -234,6 +251,15 @@ def _mtree_impl(ctx):
allow_closure = True,
)

content.add_all(
s.default_runfiles.symlinks,
expand_directories = False,
uniquify = True,
format_each = "{}/%s".format(runfiles_dir),
map_each = lambda f, e: _expand_symlink(symlink = f, workspace_name = workspace_name),
allow_closure = True,
)

ctx.actions.write(out, content = content)

return DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))
Expand Down

0 comments on commit 58ac6c0

Please sign in to comment.