Skip to content

Commit 111085c

Browse files
authored
support for gazelle 0.33.0 (#348)
1 parent 88e7ded commit 111085c

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

rules/proto_gazelle.bzl

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def _valid_env_variable_name(name):
4040
return False
4141
return True
4242

43+
def _rlocation_path(ctx, file):
44+
if file.short_path.startswith("../"):
45+
return file.short_path[3:]
46+
else:
47+
return ctx.workspace_name + "/" + file.short_path
48+
4349
def _gazelle_runner_impl(ctx):
4450
args = [ctx.attr.command]
4551
if ctx.attr.mode:
@@ -67,43 +73,32 @@ def _gazelle_runner_impl(ctx):
6773

6874
out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
6975
go_tool = ctx.toolchains["@io_bazel_rules_go//go:toolchain"].sdk.go
76+
repo_config = ctx.file._repo_config
7077
substitutions = {
7178
"@@ARGS@@": shell.array_literal(args),
72-
"@@GAZELLE_LABEL@@": shell.quote(str(ctx.attr.gazelle.label)),
73-
"@@GAZELLE_SHORT_PATH@@": shell.quote(ctx.executable.gazelle.short_path),
79+
"@@GAZELLE_PATH@@": shell.quote(_rlocation_path(ctx, ctx.executable.gazelle)),
7480
"@@GENERATED_MESSAGE@@": """
7581
# Generated by {label}
7682
# DO NOT EDIT
7783
""".format(label = str(ctx.label)),
78-
"@@RUNNER_LABEL@@": shell.quote(str(ctx.label)),
79-
"@@GOTOOL@@": shell.quote(go_tool.path),
84+
"@@GOTOOL@@": shell.quote(_rlocation_path(ctx, go_tool)),
8085
"@@ENV@@": env,
81-
"@@REPO_CONFIG_SHORT_PATH@@": "",
86+
"@@REPO_CONFIG_PATH@@": shell.quote(_rlocation_path(ctx, repo_config)) if repo_config else "",
8287
}
8388
ctx.actions.expand_template(
8489
template = ctx.file._template,
8590
output = out_file,
8691
substitutions = substitutions,
8792
is_executable = True,
8893
)
89-
9094
runfiles = ctx.runfiles(files = ctx.files.cfgs + ctx.files.imports + [
9195
ctx.executable.gazelle,
9296
go_tool,
93-
])
94-
runfiles = runfiles.merge_all([
97+
] + ([repo_config] if repo_config else [])).merge(
9598
ctx.attr.gazelle[DefaultInfo].default_runfiles,
96-
ctx.attr.gazelle[DefaultInfo].data_runfiles,
97-
])
98-
data_files = []
99+
)
99100
for d in ctx.attr.data:
100-
data_files = d[DefaultInfo].files.to_list()
101-
runfiles = runfiles.merge_all([
102-
d[DefaultInfo].default_runfiles,
103-
d[DefaultInfo].data_runfiles,
104-
])
105-
runfiles = ctx.runfiles(files = data_files).merge(runfiles)
106-
101+
runfiles = runfiles.merge(d[DefaultInfo].default_runfiles)
107102
return [DefaultInfo(
108103
files = depset([out_file]),
109104
runfiles = runfiles,
@@ -122,6 +117,7 @@ _gazelle_runner = rule(
122117
values = [
123118
"fix",
124119
"update",
120+
"update-repos",
125121
],
126122
default = "update",
127123
),
@@ -130,7 +126,7 @@ _gazelle_runner = rule(
130126
default = "",
131127
),
132128
"external": attr.string(
133-
values = ["", "external", "vendored", "static"],
129+
values = ["", "external", "static", "vendored"],
134130
default = "",
135131
),
136132
"build_tags": attr.string_list(),
@@ -140,6 +136,10 @@ _gazelle_runner = rule(
140136
"imports": attr.label_list(allow_files = True),
141137
"cfgs": attr.label_list(allow_files = True),
142138
"env": attr.string_dict(),
139+
"_repo_config": attr.label(
140+
default = None,
141+
allow_single_file = True,
142+
),
143143
"_template": attr.label(
144144
default = "@bazel_gazelle//internal:gazelle.bash.in",
145145
allow_single_file = True,
@@ -163,14 +163,23 @@ def proto_gazelle(name, **kwargs):
163163
fail("{}: both args and extra_args were provided".format(name))
164164
kwargs["extra_args"] = kwargs["args"]
165165
kwargs.pop("args")
166+
167+
visibility = kwargs.pop("visibility", default = None)
168+
169+
tags_set = {t: "" for t in kwargs.pop("tags", [])}
170+
tags_set["manual"] = ""
171+
tags = [k for k in tags_set.keys()]
166172
runner_name = name + "-runner"
167173
_gazelle_runner(
168174
name = runner_name,
169-
tags = ["manual"],
175+
tags = tags,
170176
**kwargs
171177
)
172178
native.sh_binary(
173179
name = name,
174180
srcs = [runner_name],
175-
tags = ["manual"],
181+
tags = tags,
182+
visibility = visibility,
183+
deps = ["@bazel_tools//tools/bash/runfiles"],
184+
data = kwargs["data"] if "data" in kwargs else [],
176185
)

0 commit comments

Comments
 (0)