Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0da4130

Browse files
committed
Bazel: add LFS rules
1 parent c378d6a commit 0da4130

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

java/kotlin-extractor/BUILD.bazel

Whitespace-only changes.

misc/bazel/internal/BUILD.bazel

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import pathlib
5+
import subprocess
6+
import os
7+
8+
sources = [pathlib.Path(arg).resolve() for arg in sys.argv[1:]]
9+
source_dir = pathlib.Path(os.path.commonpath(src.parent for src in sources))
10+
11+
for src in sources:
12+
with open(src, 'rb') as input:
13+
lfs_pointer = subprocess.run(["git", "lfs", "clean", "--", src],
14+
stdin=input, stdout=subprocess.PIPE, check=True, cwd=source_dir).stdout
15+
with open(src.name, 'wb') as output:
16+
subprocess.run(["git", "lfs", "smudge", "--", src], input=lfs_pointer, stdout=output, check=True, cwd=source_dir)

misc/bazel/lfs.bzl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
def _smudge(repository_ctx, srcs):
2+
for src in srcs:
3+
repository_ctx.watch(src)
4+
script = Label("//misc/bazel/internal:git_lfs_smudge.py")
5+
python = repository_ctx.which("python3") or repository_ctx.which("python")
6+
if not python:
7+
fail("Neither python3 nor python executables found")
8+
res = repository_ctx.execute([python, script] + srcs, quiet = False)
9+
if res.return_code != 0:
10+
fail("git LFS smudging failed while instantiating @%s" % repository_ctx.name)
11+
12+
def _download_and_extract_lfs(repository_ctx):
13+
attr = repository_ctx.attr
14+
src = repository_ctx.path(attr.src)
15+
if attr.build_file_content and attr.build_file:
16+
fail("You should specify only one among build_file_content and build_file for rule @%s" % repository_ctx.name)
17+
_smudge(repository_ctx, [src])
18+
repository_ctx.extract(src.basename, stripPrefix = attr.strip_prefix)
19+
repository_ctx.delete(src.basename)
20+
if attr.build_file_content:
21+
repository_ctx.file("BUILD.bazel", attr.build_file_content)
22+
elif attr.build_file:
23+
repository_ctx.symlink(attr.build_file, "BUILD.bazel")
24+
25+
def _download_lfs(repository_ctx):
26+
attr = repository_ctx.attr
27+
if int(bool(attr.srcs)) + int(bool(attr.dir)) != 1:
28+
fail("Exactly one between `srcs` and `dir` must be defined for @%s" % repository_ctx.name)
29+
if attr.srcs:
30+
srcs = [repository_ctx.path(src) for src in attr.srcs]
31+
else:
32+
dir = repository_ctx.path(attr.dir)
33+
if not dir.is_dir:
34+
fail("`dir` not a directory in @%s" % repository_ctx.name)
35+
srcs = [f for f in dir.readdir() if not f.is_dir]
36+
_smudge(repository_ctx, srcs)
37+
38+
# with bzlmod the name is qualified with `~` separators, and we want the base name here
39+
name = repository_ctx.name.split("~")[-1]
40+
repository_ctx.file("BUILD.bazel", """
41+
exports_files({files})
42+
43+
filegroup(
44+
name = "{name}",
45+
srcs = {files},
46+
visibility = ["//visibility:public"],
47+
)
48+
""".format(name = name, files = repr([src.basename for src in srcs])))
49+
50+
lfs_archive = repository_rule(
51+
doc = "Export the contents from an on-demand LFS archive. The corresponding path should be added to be ignored " +
52+
"in `.lfsconfig`.",
53+
implementation = _download_and_extract_lfs,
54+
attrs = {
55+
"src": attr.label(mandatory = True, doc = "Local path to the LFS archive to extract."),
56+
"build_file_content": attr.string(doc = "The content for the BUILD file for this repository. " +
57+
"Either build_file or build_file_content can be specified, but not both."),
58+
"build_file": attr.label(doc = "The file to use as the BUILD file for this repository. " +
59+
"Either build_file or build_file_content can be specified, but not both."),
60+
"strip_prefix": attr.string(default = "", doc = "A directory prefix to strip from the extracted files. "),
61+
},
62+
)
63+
64+
lfs_files = repository_rule(
65+
doc = "Export LFS files for on-demand download. Exactly one between `srcs` and `dir` must be defined. The " +
66+
"corresponding paths should be added to be ignored in `.lfsconfig`.",
67+
implementation = _download_lfs,
68+
attrs = {
69+
"srcs": attr.label_list(doc = "Local paths to the LFS files to export."),
70+
"dir": attr.label(doc = "Local path to a directory containing LFS files to export. Only the direct contents " +
71+
"of the directory are exported"),
72+
},
73+
)

0 commit comments

Comments
 (0)