11# rules_file
22
3- Bazel rules for basic file operations, like creating directories, and formatting.
3+ Bazel rules for basic file operations, such as creating directories and formatting.
44
5- ## Example
5+ ## Directory
66
7- To use buildifier as a formatter,
7+ Create a directory from files:
8+
9+ ``` bzl
10+ load(" @rules_file//file:rules.bzl" , " directory" )
11+
12+ directory(
13+ name = " example" ,
14+ srcs = glob([" **/*.txt" ]),
15+ )
16+ ```
17+
18+ Create a directory from a tarball:
19+
20+ ``` bzl
21+ load(" @rules_file//file:rules.bzl" , " untar" )
22+
23+ untar(
24+ name = " example" ,
25+ src = " example.tar" ,
26+ )
27+ ```
28+
29+ ## Package-less Files
30+
31+ Access files without regard to package structure. This can be helpful for formatting or Bazel integration tests.
32+
33+ ### Workspace Example
34+
35+ Create a new repository containing all workspace files.
836
937** WORKSPACE.bazel**
1038
1139``` bzl
12- workspace(name = " example" )
40+ files(
41+ name = " files"
42+ build = " BUILD.file.bazel" ,
43+ root_file = " //:WORKSPACE.bazel" ,
44+ )
45+ ```
46+
47+ ** BAZEL.bazel**
48+
49+ ```
50+ load("@rules_file//file:rules.bzl", "bazelrc_deleted_packages")
51+
52+ bazelrc_deleted_packages(
53+ name = "gen_bazelrc",
54+ output = "deleted.bazelrc",
55+ packages = ["@files//:packages"],
56+ )
57+ ```
58+
59+ ** files.bazel**
60+
61+ ``` bzl
62+ # note: files is the symlink to the workspace
63+ filegroup(
64+ name = " example" ,
65+ srcs = glob([" files/**/*.txt" ]),
66+ visibility = [" //visibility:public" ],
67+ )
68+ ```
69+
70+ Generate deleted.bazelrc:
71+
72+ ```
73+ bazel run :gen_bazelrc
74+ ```
75+
76+ ** .bazelrc**
77+
78+ ```
79+ import %workspace%/deleted.bazelrc
80+ ```
81+
82+ Now ` @files//:example ` is all \* .txt files in the workspace.
83+
84+ ### Bazel Integration Test Example
85+
86+ Use files in the test directory as data for a Bazel integration test.
87+
88+ ** BAZEL.bazel**
89+
90+ ```
91+ load("@rules_file//file:rules.bzl", "bazelrc_deleted_packages", "find_packages")
92+
93+ filegroup(
94+ name = "test",
95+ srcs = glob(["test/**/*.bazel" "test/**/*.txt"]),
96+ )
97+
98+ find_packages(
99+ name = "test_packages",
100+ roots = ["test"],
101+ )
102+
103+ bazelrc_deleted_packages(
104+ name = "gen_bazelrc",
105+ output = "deleted.bazelrc",
106+ packages = [":test_packages"],
107+ )
108+ ```
109+
110+ Generate ` deleted.bazelrc ` by running:
111+
112+ ```
113+ bazel run :gen_bazelrc
114+ ```
13115
14- load(" @rules_file//file:workspace.bzl" , " files" )
116+ ** .bazelrc**
117+
118+ ```
119+ import %workspace%/deleted.bazelrc
120+ ```
121+
122+ ## Generate
123+
124+ In some cases, it is necessary to version control build products in the workspace (bootstrapping, working with other tools).
125+
126+ These rules build the outputs, and copy them to the workspace or check for differences.
127+
128+ ** BUILD.bazel**
129+
130+ ``` bzl
131+ load(" @rules_file//file:rules.bzl" , " bazelrc_deleted_packages" )
132+ load(" @rules_file//file:rules.bzl" , " generate" , " generate_test" )
133+
134+ genrule(
135+ name = " example" ,
136+ cmd = " echo GENERATED! > $@" ,
137+ outs = [" out/example.txt" ],
138+ )
139+
140+ generate(
141+ name = " example_gen" ,
142+ srcs = " example.txt" ,
143+ data = [" out/example.txt" ],
144+ data_strip_prefix = " out" ,
145+ )
146+
147+ generate_test(
148+ name = " example_diff" ,
149+ generate = " :example_gen" ,
150+ )
151+
152+ bazelrc_deleted_packages(
153+ name = " gen_bazelrc" ,
154+ output = " deleted.bazelrc" ,
155+ packages = [" @files//:packages" ],
156+ )
157+ ```
158+
159+
160+
161+ To overwrite the workspace file:
162+
163+ ``` bzl
164+ bazel run :example_gen
165+ ```
166+
167+ To check for differences (e.g. in CI):
168+
169+ ``` bzl
170+ bazel test :example_diff
171+ ```
172+
173+ ## Format
174+
175+ Formatting is a particular case of the checked-in build products pattern.
176+
177+ The code formatting is a regular Bazel action. The formatted result can be using to overwrite workspace files, or to check for differences.
178+
179+ This repository has rules for buildifier, black, and gofmt. It is also used for [ prettier] ( https://github.com/rivethealth/rules_javascript ) .
180+
181+ ### Buildifier Example
182+
183+ ** WORKSPACE.bazel**
184+
185+ ``` bzl
186+ BUILDTOOLS_VERSION = " 3.5.0"
187+
188+ http_archive(
189+ name = " com_github_bazelbuild_buildtools" ,
190+ sha256 = " f5b666935a827bc2b6e2ca86ea56c796d47f2821c2ff30452d270e51c2a49708" ,
191+ strip_prefix = " buildtools-%s " % BUILDTOOLS_VERSION ,
192+ url = " https://github.com/bazelbuild/buildtools/archive/%s .zip" % BUILDTOOLS_VERSION ,
193+ )
194+
195+ load(" @com_github_bazelbuild_buildtools//buildifier:deps.bzl" , " buildifier_dependencies" )
196+
197+ buildifier_dependencies()
15198
16199files(
17- name = " example_files "
200+ name = " files "
18201 build = " BUILD.file.bazel" ,
19- root_file = " @example// WORKSPACE.bazel" ,
202+ root_file = " //: WORKSPACE.bazel" ,
20203)
21204```
22205
@@ -31,23 +214,21 @@ buildifier(
31214)
32215
33216format (
34- name = " format " ,
35- srcs = [" @example_files //:buildifier_files" ],
217+ name = " buildifier_format " ,
218+ srcs = [" @files //:buildifier_files" ],
36219 formatter = " :buildifier" ,
37220 strip_prefix = " files" ,
38221)
39222
40223generate_test(
41- name = " test " ,
224+ name = " buildifier_diff " ,
42225 generate = " :format" ,
43226)
44227```
45228
46- ** BUILD.file .bazel**
229+ ** files .bazel**
47230
48231``` bzl
49- package(default_visibility = [" //visibility:public" ])
50-
51232filegroup(
52233 name = " buildifier_files" ,
53234 srcs = glob(
@@ -57,23 +238,31 @@ filegroup(
57238 " files/**/BUILD" ,
58239 " files/**/WORKSPACE" ,
59240 ],
60- [" **/bazel-*/**" ],
61241 ),
242+ visibility = [" //visibility:public" ],
62243)
63244```
64245
65- Then run
246+ Generate deleted.bazelrc:
247+
248+ ```
249+ bazel run :gen_bazelrc
250+ ```
251+
252+ ** .bazelrc**
253+
254+ ```
255+ import %workspace%/deleted.bazelrc
256+ ```
257+
258+ To format:
259+
260+ ``` sh
261+ bazel run :buildifier_format
262+ ```
263+
264+ To check format:
66265
67266``` sh
68- packages=" $( \
69- find .-name BUILD -o -name BUILD.bazel \
70- | sed -e ' s:/[^/]*$::' -e ' s:^\./::' -e ' s:^:@example_files//files/:' -e ' s:/.$::' \
71- | tr ' \n' , \
72- | sed ' s/,$//' \
73- ) "
74-
75- # format
76- bazel run " --deleted_packages=$packages " //:format
77- # test
78- bazel run " --deleted_packages=$packages " //:test
267+ bazel run :buildifier_diff
79268```
0 commit comments