Skip to content

Commit 734a77d

Browse files
committed
Bazel: Add lfs_archives function
This pulls out some generic stuff from the Swift static runtime into a generic location. Internal clean-up PR will follow once this is in.
1 parent b0fa3e7 commit 734a77d

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

‎misc/bazel/lfs.bzl‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ def lfs_smudge(repository_ctx, srcs, *, extract = False, stripPrefix = None, exe
2828
res = repository_ctx.download([], src.basename, sha256 = info, allow_fail = True, executable = executable)
2929
if not res.success:
3030
remote.append(src)
31-
if remote:
32-
infos = probe(remote)
33-
for src, info in zip(remote, infos):
34-
sha256, _, url = info.partition(" ")
35-
repository_ctx.report_progress("downloading remote %s" % src.basename)
36-
repository_ctx.download(url, src.basename, sha256 = sha256, executable = executable)
37-
if extract:
38-
for src in srcs:
39-
repository_ctx.report_progress("extracting %s" % src.basename)
40-
repository_ctx.extract(src.basename, stripPrefix = stripPrefix)
41-
repository_ctx.delete(src.basename)
31+
if remote:
32+
infos = probe(remote)
33+
for src, info in zip(remote, infos):
34+
sha256, _, url = info.partition(" ")
35+
repository_ctx.report_progress("downloading remote %s" % src.basename)
36+
repository_ctx.download(url, src.basename, sha256 = sha256, executable = executable)
37+
if extract:
38+
for src in srcs:
39+
repository_ctx.report_progress("extracting %s" % src.basename)
40+
repository_ctx.extract(src.basename, stripPrefix = stripPrefix)
41+
repository_ctx.delete(src.basename)
4242

43-
def _download_and_extract_lfs(repository_ctx):
43+
def _download_and_extract_lfs_archive(repository_ctx):
4444
attr = repository_ctx.attr
4545
src = repository_ctx.path(attr.src)
4646
if attr.build_file_content and attr.build_file:
@@ -51,6 +51,14 @@ def _download_and_extract_lfs(repository_ctx):
5151
elif attr.build_file:
5252
repository_ctx.symlink(attr.build_file, "BUILD.bazel")
5353

54+
def _download_and_extract_lfs_archives(repository_ctx):
55+
lfs_smudge(
56+
repository_ctx,
57+
[repository_ctx.path(src) for src in repository_ctx.attr.srcs],
58+
extract = True,
59+
stripPrefix = repository_ctx.attr.strip_prefix,
60+
)
61+
5462
def _download_lfs(repository_ctx):
5563
attr = repository_ctx.attr
5664
if int(bool(attr.srcs)) + int(bool(attr.dir)) != 1:
@@ -86,7 +94,7 @@ def _download_lfs(repository_ctx):
8694
lfs_archive = repository_rule(
8795
doc = "Export the contents from an on-demand LFS archive. The corresponding path should be added to be ignored " +
8896
"in `.lfsconfig`.",
89-
implementation = _download_and_extract_lfs,
97+
implementation = _download_and_extract_lfs_archive,
9098
attrs = {
9199
"src": attr.label(mandatory = True, doc = "Local path to the LFS archive to extract."),
92100
"build_file_content": attr.string(doc = "The content for the BUILD file for this repository. " +
@@ -97,6 +105,19 @@ lfs_archive = repository_rule(
97105
},
98106
)
99107

108+
lfs_archives = repository_rule(
109+
doc = "Overlay the contents from on-demand LFS archives. The corresponding paths should be added to be ignored " +
110+
"in `.lfsconfig`.",
111+
implementation = _download_and_extract_lfs_archives,
112+
attrs = {
113+
"srcs": attr.label_list(
114+
doc = "Local paths to the LFS archives to extract in order.",
115+
mandatory = True,
116+
),
117+
"strip_prefix": attr.string(default = "", doc = "A directory prefix to strip from the extracted files. "),
118+
},
119+
)
120+
100121
lfs_files = repository_rule(
101122
doc = "Export LFS files for on-demand download. Exactly one between `srcs` and `dir` must be defined. The " +
102123
"corresponding paths should be added to be ignored in `.lfsconfig`.",

0 commit comments

Comments
 (0)