Skip to content

Commit

Permalink
support for gazelle 0.33.0 (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhauser authored Dec 30, 2023
1 parent 88e7ded commit 111085c
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions rules/proto_gazelle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def _valid_env_variable_name(name):
return False
return True

def _rlocation_path(ctx, file):
if file.short_path.startswith("../"):
return file.short_path[3:]
else:
return ctx.workspace_name + "/" + file.short_path

def _gazelle_runner_impl(ctx):
args = [ctx.attr.command]
if ctx.attr.mode:
Expand Down Expand Up @@ -67,43 +73,32 @@ def _gazelle_runner_impl(ctx):

out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
go_tool = ctx.toolchains["@io_bazel_rules_go//go:toolchain"].sdk.go
repo_config = ctx.file._repo_config
substitutions = {
"@@ARGS@@": shell.array_literal(args),
"@@GAZELLE_LABEL@@": shell.quote(str(ctx.attr.gazelle.label)),
"@@GAZELLE_SHORT_PATH@@": shell.quote(ctx.executable.gazelle.short_path),
"@@GAZELLE_PATH@@": shell.quote(_rlocation_path(ctx, ctx.executable.gazelle)),
"@@GENERATED_MESSAGE@@": """
# Generated by {label}
# DO NOT EDIT
""".format(label = str(ctx.label)),
"@@RUNNER_LABEL@@": shell.quote(str(ctx.label)),
"@@GOTOOL@@": shell.quote(go_tool.path),
"@@GOTOOL@@": shell.quote(_rlocation_path(ctx, go_tool)),
"@@ENV@@": env,
"@@REPO_CONFIG_SHORT_PATH@@": "",
"@@REPO_CONFIG_PATH@@": shell.quote(_rlocation_path(ctx, repo_config)) if repo_config else "",
}
ctx.actions.expand_template(
template = ctx.file._template,
output = out_file,
substitutions = substitutions,
is_executable = True,
)

runfiles = ctx.runfiles(files = ctx.files.cfgs + ctx.files.imports + [
ctx.executable.gazelle,
go_tool,
])
runfiles = runfiles.merge_all([
] + ([repo_config] if repo_config else [])).merge(
ctx.attr.gazelle[DefaultInfo].default_runfiles,
ctx.attr.gazelle[DefaultInfo].data_runfiles,
])
data_files = []
)
for d in ctx.attr.data:
data_files = d[DefaultInfo].files.to_list()
runfiles = runfiles.merge_all([
d[DefaultInfo].default_runfiles,
d[DefaultInfo].data_runfiles,
])
runfiles = ctx.runfiles(files = data_files).merge(runfiles)

runfiles = runfiles.merge(d[DefaultInfo].default_runfiles)
return [DefaultInfo(
files = depset([out_file]),
runfiles = runfiles,
Expand All @@ -122,6 +117,7 @@ _gazelle_runner = rule(
values = [
"fix",
"update",
"update-repos",
],
default = "update",
),
Expand All @@ -130,7 +126,7 @@ _gazelle_runner = rule(
default = "",
),
"external": attr.string(
values = ["", "external", "vendored", "static"],
values = ["", "external", "static", "vendored"],
default = "",
),
"build_tags": attr.string_list(),
Expand All @@ -140,6 +136,10 @@ _gazelle_runner = rule(
"imports": attr.label_list(allow_files = True),
"cfgs": attr.label_list(allow_files = True),
"env": attr.string_dict(),
"_repo_config": attr.label(
default = None,
allow_single_file = True,
),
"_template": attr.label(
default = "@bazel_gazelle//internal:gazelle.bash.in",
allow_single_file = True,
Expand All @@ -163,14 +163,23 @@ def proto_gazelle(name, **kwargs):
fail("{}: both args and extra_args were provided".format(name))
kwargs["extra_args"] = kwargs["args"]
kwargs.pop("args")

visibility = kwargs.pop("visibility", default = None)

tags_set = {t: "" for t in kwargs.pop("tags", [])}
tags_set["manual"] = ""
tags = [k for k in tags_set.keys()]
runner_name = name + "-runner"
_gazelle_runner(
name = runner_name,
tags = ["manual"],
tags = tags,
**kwargs
)
native.sh_binary(
name = name,
srcs = [runner_name],
tags = ["manual"],
tags = tags,
visibility = visibility,
deps = ["@bazel_tools//tools/bash/runfiles"],
data = kwargs["data"] if "data" in kwargs else [],
)

0 comments on commit 111085c

Please sign in to comment.