Skip to content

Commit

Permalink
Allow for the passing of args and env...
Browse files Browse the repository at this point in the history
...to 'create_protoc_plugin_rule' so that users
can pass in custom arguments and environment
variables to the protoc plugin.
  • Loading branch information
rileysdev committed Dec 17, 2024
1 parent 11fdb67 commit b03bc7e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ def _protoc_plugin_rule_implementation(context):
fail("Plugin name %s does not start with protoc-gen-" % plugin_name)
plugin_short_name = plugin_name.removeprefix("protoc-gen-")

args = [
# Args passed to 'create_protoc_plugin_rule' are not allowed to start with
# '--plugin' or '--<plugin_short_name>_out'.
for arg in context.attr._args:
if arg.startswith("--plugin") or arg.startswith("--%s_out" % plugin_short_name):
fail("Argument %s starts with --plugin or --%s_out, which is not allowed" % (arg, plugin_short_name))

args = context.attr._args + [
"--plugin=%s=%s" % (plugin_name, plugin_path),
"--%s_out" % plugin_short_name,
output_directory,
Expand Down Expand Up @@ -116,23 +122,27 @@ def _protoc_plugin_rule_implementation(context):
],
command = context.executable._protoc.path + " $@",
arguments = args,
use_default_shell_env = True,
env = context.attr._env,
)

return [DefaultInfo(
files = depset(output_files),
)]

def create_protoc_plugin_rule(plugin_label, extensions = []):
def create_protoc_plugin_rule(plugin_label, extensions = [], args = [], env = {}):
return rule(
attrs = {
"_args": attr.string_list(
mandatory = False,
default = args,
),
"deps": attr.label_list(
mandatory = True,
providers = [ProtoInfo],
),
"srcs": attr.label_list(
allow_files = True,
mandatory = True,
"_env": attr.string_dict(
mandatory = False,
default = env,
),
"_extensions": attr.string_list(
default = extensions,
Expand All @@ -149,6 +159,10 @@ def create_protoc_plugin_rule(plugin_label, extensions = []):
executable = True,
allow_single_file = True,
),
"srcs": attr.label_list(
allow_files = True,
mandatory = True,
),
},
output_to_genfiles = True,
implementation = _protoc_plugin_rule_implementation,
Expand Down

0 comments on commit b03bc7e

Please sign in to comment.