Skip to content

Commit 93d4152

Browse files
authored
refactor: fully rely on buildifier (#472)
We can reduce the conditional logic by placing load statements with the use-site of symbols they load. buildifier will re-order the load statements for us.
1 parent 21d6d88 commit 93d4152

File tree

5 files changed

+72
-94
lines changed

5 files changed

+72
-94
lines changed

{{ .ProjectSnake }}/.aspect/bazelrc/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ write_aspect_bazelrc_presets(
99
"convenience",
1010
"correctness",
1111
"debug",
12-
{{- if .Computed.java }}
12+
{{ if .Computed.java }}
1313
"java",
14-
{{- end }}
15-
{{- if .Computed.javascript }}
14+
{{ end }}
15+
{{ if .Computed.javascript }}
1616
"javascript",
17-
{{- end }}
17+
{{ end }}
1818
"performance",
1919
],
2020
)

{{ .ProjectSnake }}/BUILD

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,16 @@
11
"""Targets in the repository root"""
2-
{{- if or .Computed.javascript .Computed.python }}
3-
{{ if .Computed.javascript }}
4-
load("@aspect_rules_js//js:defs.bzl", "js_library")
5-
load("@npm//:defs.bzl", "npm_link_all_packages")
6-
{{- end }}
7-
{{- if .Computed.python }}
8-
load("@pip//:requirements.bzl", "all_whl_requirements")
9-
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
10-
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
11-
{{- end }}
12-
{{- end }}
132

3+
# We prefer BUILD instead of BUILD.bazel
144
# gazelle:build_file_name BUILD
155

166
{{ if .Computed.javascript }}
7+
load("@aspect_rules_js//js:defs.bzl", "js_library")
8+
load("@npm//:defs.bzl", "npm_link_all_packages")
9+
1710
# TODO: remove once https://github.com/aspect-build/aspect-cli/issues/560 done
1811
# gazelle:js_npm_package_target_name pkg
19-
{{- end }}
20-
21-
{{- if .Scaffold.lint }}
22-
alias(
23-
name = "format",
24-
actual = "//tools/format",
25-
)
26-
{{- end }}
27-
{{- if .Computed.shell }}
28-
exports_files(
29-
[".shellcheckrc"],
30-
visibility = ["//:__subpackages__"],
31-
)
32-
{{- end }}
33-
{{- if .Computed.go }}
34-
load("@gazelle//:def.bzl", "gazelle")
35-
36-
# gazelle:prefix github.com/example/project
37-
gazelle(name = "gazelle")
38-
{{- end }}
39-
{{- if .Computed.python }}
40-
41-
exports_files(
42-
["pyproject.toml"],
43-
visibility = ["//:__subpackages__"],
44-
)
45-
{{- end }}
46-
{{- if .Computed.javascript }}
47-
4812
npm_link_all_packages(name = "node_modules")
49-
{{- if .Scaffold.lint }}
13+
{{ if .Scaffold.lint }}
5014

5115
js_library(
5216
name = "eslintrc",
@@ -64,9 +28,39 @@ js_library(
6428
deps = [],
6529
visibility = ["//tools/format:__pkg__"],
6630
)
67-
{{- end }}
68-
{{- end }}
69-
{{- if .Computed.python }}
31+
{{ end }}
32+
{{ end }}
33+
34+
{{ if .Scaffold.lint }}
35+
alias(
36+
name = "format",
37+
actual = "//tools/format",
38+
)
39+
{{ end }}
40+
41+
{{ if .Computed.shell }}
42+
exports_files(
43+
[".shellcheckrc"],
44+
visibility = ["//:__subpackages__"],
45+
)
46+
{{ end }}
47+
48+
{{ if .Computed.go }}
49+
load("@gazelle//:def.bzl", "gazelle")
50+
51+
# gazelle:prefix github.com/example/project
52+
gazelle(name = "gazelle")
53+
{{ end }}
54+
55+
{{ if .Computed.python }}
56+
load("@pip//:requirements.bzl", "all_whl_requirements")
57+
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
58+
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
59+
60+
exports_files(
61+
["pyproject.toml"],
62+
visibility = ["//:__subpackages__"],
63+
)
7064

7165
# Set `aspect configure` to produce aspect_rules_py targets rather than rules_python
7266
# aspect:map_kind py_binary py_binary @aspect_rules_py//py:defs.bzl
@@ -96,4 +90,4 @@ gazelle_python_manifest(
9690
pip_repository_name = "pip",
9791
)
9892

99-
{{- end }}
93+
{{ end }}

{{ .ProjectSnake }}/tools/BUILD

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,28 @@ included `.envrc` automates this for you.
1212
"""
1313

1414
load("@bazel_env.bzl", "bazel_env")
15-
{{ if .Computed.javascript }}
16-
load("@npm//:defs.bzl", "npm_link_all_packages")
17-
{{ end }}
18-
{{ if and .Scaffold.codegen .Computed.python }}
19-
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
20-
{{ end }}
2115
load("@multitool//:tools.bzl", MULTITOOL_TOOLS = "TOOLS")
2216

2317
package(default_visibility = ["//visibility:public"])
2418

25-
{{ if .Computed.go }}
26-
alias(name = "go", actual = "@rules_go//go")
27-
{{ end }}
28-
2919
{{ if and .Scaffold.codegen .Computed.python }}
20+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
21+
3022
py_console_script_binary(
3123
name = "copier",
3224
pkg = "@pip//copier",
3325
)
3426
{{ end }}
3527

3628
{{ if .Computed.javascript }}
29+
load("@npm//:defs.bzl", "npm_link_all_packages")
30+
31+
npm_link_all_packages(name = "node_modules")
32+
3733
{{ if .Scaffold.codegen }}
3834
load("@npm//tools:yo/package_json.bzl", yo_bin = "bin")
3935
yo_bin.yo_binary(name = "yo")
4036
{{ end }}
41-
42-
npm_link_all_packages(name = "node_modules")
43-
4437
{{ end }}
4538

4639
bazel_env(
@@ -87,8 +80,5 @@ bazel_env(
8780
{{ if .Computed.python }}
8881
"python": "$(PYTHON3)",
8982
{{ end }}
90-
} | {
91-
t: "@multitool//tools/" + t
92-
for t in MULTITOOL_TOOLS
93-
},
83+
} | MULTITOOL_TOOLS,
9484
)

{{ .ProjectSnake }}/tools/format/BUILD

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ we don't want to trigger eager fetches of these for builds that don't want to ru
55
"""
66

77
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
8-
{{- if .Computed.javascript }}
9-
load("@npm//:prettier/package_json.bzl", prettier = "bin")
10-
{{- end }}
118

129
package(default_visibility = ["//:__subpackages__"])
1310

14-
{{ if .Computed.javascript -}}
11+
{{ if .Computed.javascript }}
12+
load("@npm//:prettier/package_json.bzl", prettier = "bin")
13+
1514
prettier.prettier_binary(
1615
name = "prettier",
1716
# Allow the binary to be run outside bazel
@@ -28,24 +27,24 @@ prettier.prettier_binary(
2827
"--log-level=warn",
2928
],
3029
)
30+
{{ end }}
3131

32-
{{ end -}}
3332
format_multirun(
3433
name = "format",
35-
{{- if .Computed.cpp}}
34+
{{ if .Computed.cpp}}
3635
cc = "@llvm_toolchain_llvm//:bin/clang-format",
37-
{{- end }}
38-
{{- if .Computed.go }}
36+
{{ end }}
37+
{{ if .Computed.go }}
3938
go = "@aspect_rules_lint//format:gofumpt",
40-
{{- end }}
41-
{{- if .Computed.javascript }}
39+
{{ end }}
40+
{{ if .Computed.javascript }}
4241
javascript = ":prettier",
43-
{{- end }}
44-
{{- if .Computed.python }}
42+
{{ end }}
43+
{{ if .Computed.python }}
4544
python = "@aspect_rules_lint//format:ruff",
46-
{{- end }}
47-
{{- if .Computed.rust }}
45+
{{ end }}
46+
{{ if .Computed.rust }}
4847
rust = "@rules_rust//tools/rustfmt:upstream_rustfmt",
49-
{{- end }}
48+
{{ end }}
5049
starlark = "@buildifier_prebuilt//:buildifier",
5150
)

{{ .ProjectSnake }}/tools/lint/BUILD

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,17 @@ This is in its own package because it has so many loading-time symbols,
44
we don't want to trigger eager fetches of these for builds which aren't running linters.
55
"""
66

7-
{{ if .Computed.cpp }}
8-
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
9-
{{ end }}
10-
{{ if .Computed.javascript }}
11-
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin")
12-
{{ end }}
13-
{{ if .Computed.java }}
14-
load("@rules_java//java:defs.bzl", "java_binary")
15-
{{ end }}
16-
{{ if .Computed.go }}
17-
load("@rules_go//go:def.bzl", "nogo", "TOOLS_NOGO")
18-
{{ end }}
19-
207
package(default_visibility = ["//:__subpackages__"])
218

229
{{ if .Computed.javascript }}
10+
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin")
11+
2312
eslint_bin.eslint_binary(name = "eslint")
2413
{{ end }}
2514

2615
{{ if .Computed.java }}
16+
load("@rules_java//java:defs.bzl", "java_binary")
17+
2718
java_binary(
2819
name = "pmd",
2920
main_class = "net.sourceforge.pmd.PMD",
@@ -32,6 +23,8 @@ java_binary(
3223
{{ end }}
3324

3425
{{ if .Computed.cpp}}
26+
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
27+
3528
native_binary(
3629
name = "clang_tidy",
3730
src = select(
@@ -51,6 +44,8 @@ native_binary(
5144
{{ end }}
5245

5346
{{ if .Computed.go }}
47+
load("@rules_go//go:def.bzl", "nogo", "TOOLS_NOGO")
48+
5449
nogo(
5550
name = "nogo",
5651
deps = TOOLS_NOGO,

0 commit comments

Comments
 (0)