Skip to content

Commit cc728fd

Browse files
authored
gazelle: compact rules use wildcard (#166)
* Use globs when generating "compact" rules with gazelle * when using compact rules, erlang_bytecode rules refer to :hdrs instead of individual files * Make globs in generated app.bzl files multiline when long * Use native.glob inside of macros in generated app.bzl files * Remove the extension restriction for hdrs attributes Otherwise globs that turn out to be empty can't be passed to the rule
1 parent 5b20b9a commit cc728fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+10059
-543
lines changed

erlang_app.bzl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ def _erlang_app(
5454
beam_files = None,
5555
hdrs = None,
5656
srcs = None,
57+
priv = None,
58+
license_files = None,
5759
test = False):
58-
if beam_files != None or hdrs != None or srcs != None:
60+
if beam_files != None or hdrs != None or srcs != None or priv != None or license_files != None:
5961
if erlc_opts != None:
60-
fail("Cannot set beam_files, hdrs or srcs AND erlc_opts")
62+
fail("Cannot set beam_files, hdrs, srcs, priv or license_files AND erlc_opts")
6163
if len(extra_hdrs) > 0:
62-
fail("Cannot set beam_files, hdrs or srcs AND extra_hdrs")
64+
fail("Cannot set beam_files, hdrs, srcs, priv or license_files AND extra_hdrs")
6365
if len(extra_srcs) > 0:
64-
fail("Cannot set beam_files, hdrs or srcs AND extra_srcs")
66+
fail("Cannot set beam_files, hdrs, srcs, priv or license_files AND extra_srcs")
67+
if len(extra_priv) > 0:
68+
fail("Cannot set beam_files, hdrs, srcs, priv or license_files AND extra_priv")
69+
if len(extra_license_files) > 0:
70+
fail("Cannot set beam_files, hdrs, srcs, priv or license_files AND extra_license_files")
6571
if len(build_deps) > 0:
6672
print("Warning: build_deps are ignored when beam_files is set")
6773
if len(runtime_deps) > 0:
@@ -138,18 +144,27 @@ def _erlang_app(
138144
else:
139145
app = "ebin/{}.app".format(app_name)
140146

147+
if priv == None:
148+
priv = native.glob(
149+
["priv/**/*"],
150+
exclude = extra_priv,
151+
) + extra_priv
152+
153+
if license_files == None:
154+
license_files = native.glob(
155+
["LICENSE*"],
156+
exclude = extra_license_files,
157+
) + extra_license_files
158+
141159
erlang_app_info(
142160
name = name,
143161
app_name = app_name,
144162
extra_apps = extra_apps,
145163
hdrs = hdrs,
146164
app = app,
147165
beam = beam_files,
148-
priv = native.glob(["priv/**/*"]) + extra_priv,
149-
license_files = native.glob(
150-
["LICENSE*"],
151-
exclude = extra_license_files,
152-
) + extra_license_files,
166+
priv = priv,
167+
license_files = license_files,
153168
srcs = srcs,
154169
deps = deps + runtime_deps,
155170
visibility = ["//visibility:public"],

erlang_app_info.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ erlang_app_info = rule(
6060
attrs = {
6161
"app_name": attr.string(mandatory = True),
6262
"extra_apps": attr.string_list(),
63-
"hdrs": attr.label_list(allow_files = [".hrl"]),
63+
"hdrs": attr.label_list(allow_files = True),
6464
"app": attr.label(allow_files = [".app"]),
6565
"beam": attr.label_list(allow_files = [".beam", ".appup"]),
6666
"priv": attr.label_list(allow_files = True),

erlang_bytecode2.bzl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,32 @@ load(
77

88
ErlcOptsInfo = _ErlcOptsInfo
99

10-
def erlang_bytecode(**kwargs):
11-
_erlang_bytecode(**kwargs)
10+
def erlang_bytecode(
11+
srcs = [],
12+
outs = None,
13+
dest = None,
14+
**kwargs):
15+
if len(srcs) == 0:
16+
fail("srcs cannot be empty")
17+
if outs != None and dest != None:
18+
fail('"outs" and "dest" cannot be set together')
19+
if outs == None:
20+
if dest == None:
21+
fail('either "outs" or "dest" must be set')
22+
outs = [
23+
dest + "/" + _beam(src)
24+
for src in srcs
25+
]
26+
27+
_erlang_bytecode(
28+
srcs = srcs,
29+
outs = outs,
30+
**kwargs
31+
)
1232

1333
def erlc_opts(**kwargs):
1434
_erlc_opts(**kwargs)
35+
36+
def _beam(p):
37+
(_, _, basename) = p.rpartition("/")
38+
return basename.removesuffix(".erl") + ".beam"

gazelle/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"erl_parser.go",
1616
"erl_parser_impl.go",
1717
"erlang_app.go",
18+
"erlang_app_builder.go",
1819
"fix.go",
1920
"generate.go",
2021
"hex_metadata_parser.go",

0 commit comments

Comments
 (0)