Skip to content

Instantly share code, notes, and snippets.

@andyscott
Created May 14, 2020 23:40
Show Gist options
  • Save andyscott/b386286fcffa7f4743f28bc6c9f6c5e2 to your computer and use it in GitHub Desktop.
Save andyscott/b386286fcffa7f4743f28bc6c9f6c5e2 to your computer and use it in GitHub Desktop.
load(
":common.bzl",
"FilesInfo",
)
def _strip_prefix(value, prefix):
if value.startswith(prefix):
return value[len(prefix):]
else:
return value
def _get_runfile_name(ctx, file):
"""
Given a file, this should return the "runfile name" used
with many bazel runfile helpers.
For example, it might return:
- com_stripe_zoolander/tools/build_rules/deploy/tests/runfiles/python_app
- bazel_tools/tools/python/py3wrapper.sh
"""
if file.owner.workspace_root != "":
# external repository files
return "/".join([
file.owner.workspace_name,
_strip_prefix(_strip_prefix(_strip_prefix(_strip_prefix(file.path, file.root.path), "/"), file.owner.workspace_root), "/"),
])
elif file.root.path != "":
# generated files
return "/".join([
ctx.workspace_name,
_strip_prefix(_strip_prefix(file.path, file.root.path), "/"),
])
else:
# source files
return "/".join([
ctx.workspace_name,
file.path,
])
def _deploy_runfiles_implementation(ctx):
default_info = ctx.attr.dep[DefaultInfo]
if default_info.files_to_run.executable:
runfiles_dir = "{}.runfiles".format(default_info.files_to_run.executable.path)
symlinks = depset([
(
"/{}".format(f.path),
"/{}/{}".format(runfiles_dir, _get_runfile_name(ctx, f)),
)
for f in default_info.default_runfiles.files.to_list()
])
else:
symlinks = depset()
return [
FilesInfo(
cp_files = default_info.default_runfiles.files,
mv_files = depset([]),
symlinks = symlinks,
),
]
deploy_runfiles = rule(
implementation = _deploy_runfiles_implementation,
doc = """
Expands all the runfiles symlinks for a dep in a final deploy
""",
attrs = {
"dep": attr.label(
mandatory = True,
executable = True,
cfg = "target",
doc = "the dependency for runfile expansion",
),
},
provides = [FilesInfo],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment