Skip to content

Commit 1161393

Browse files
authored
Merge pull request #22483 from github/jketema/lfs-archives
Bazel: Add `lfs_archives` function
2 parents 2d9252f + 7814be5 commit 1161393

1 file changed

Lines changed: 46 additions & 21 deletions

File tree

misc/bazel/lfs.bzl

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,42 @@ 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 _add_build_file(repository_ctx):
4444
attr = repository_ctx.attr
45-
src = repository_ctx.path(attr.src)
4645
if attr.build_file_content and attr.build_file:
4746
fail("You should specify only one among build_file_content and build_file for rule @%s" % repository_ctx.name)
48-
lfs_smudge(repository_ctx, [src], extract = True, stripPrefix = attr.strip_prefix)
4947
if attr.build_file_content:
5048
repository_ctx.file("BUILD.bazel", attr.build_file_content)
5149
elif attr.build_file:
5250
repository_ctx.symlink(attr.build_file, "BUILD.bazel")
5351

52+
def _download_and_extract_lfs_archive(repository_ctx):
53+
attr = repository_ctx.attr
54+
lfs_smudge(repository_ctx, [repository_ctx.path(attr.src)], extract = True, stripPrefix = attr.strip_prefix)
55+
_add_build_file(repository_ctx)
56+
57+
def _download_and_extract_lfs_archives(repository_ctx):
58+
for src in repository_ctx.attr.srcs:
59+
lfs_smudge(
60+
repository_ctx,
61+
[repository_ctx.path(src)],
62+
extract = True,
63+
stripPrefix = repository_ctx.attr.strip_prefix,
64+
)
65+
_add_build_file(repository_ctx)
66+
5467
def _download_lfs(repository_ctx):
5568
attr = repository_ctx.attr
5669
if int(bool(attr.srcs)) + int(bool(attr.dir)) != 1:
@@ -83,18 +96,30 @@ def _download_lfs(repository_ctx):
8396
'alias(name = "file", actual = "//:%s", visibility = ["//visibility:public"])\n' % name,
8497
)
8598

99+
_lfs_archive_attrs = {
100+
"build_file": attr.label(doc = "The file to use as the BUILD file for this repository. " +
101+
"Either build_file or build_file_content can be specified, but not both."),
102+
"build_file_content": attr.string(doc = "The content for the BUILD file for this repository. " +
103+
"Either build_file or build_file_content can be specified, but not both."),
104+
"strip_prefix": attr.string(default = "", doc = "A directory prefix to strip from the extracted files."),
105+
}
106+
86107
lfs_archive = repository_rule(
87108
doc = "Export the contents from an on-demand LFS archive. The corresponding path should be added to be ignored " +
88109
"in `.lfsconfig`.",
89-
implementation = _download_and_extract_lfs,
110+
implementation = _download_and_extract_lfs_archive,
90111
attrs = {
91112
"src": attr.label(mandatory = True, doc = "Local path to the LFS archive to extract."),
92-
"build_file_content": attr.string(doc = "The content for the BUILD file for this repository. " +
93-
"Either build_file or build_file_content can be specified, but not both."),
94-
"build_file": attr.label(doc = "The file to use as the BUILD file for this repository. " +
95-
"Either build_file or build_file_content can be specified, but not both."),
96-
"strip_prefix": attr.string(default = "", doc = "A directory prefix to strip from the extracted files. "),
97-
},
113+
} | _lfs_archive_attrs,
114+
)
115+
116+
lfs_archives = repository_rule(
117+
doc = "Overlay the contents from on-demand LFS archives. The corresponding paths should be added to be ignored " +
118+
"in `.lfsconfig`.",
119+
implementation = _download_and_extract_lfs_archives,
120+
attrs = {
121+
"srcs": attr.label_list(doc = "Local paths to the LFS archives to extract in order.", mandatory = True),
122+
} | _lfs_archive_attrs,
98123
)
99124

100125
lfs_files = repository_rule(

0 commit comments

Comments
 (0)