Skip to content

Commit 1f1c750

Browse files
committed
Allow for the passing of args and env...
...to 'create_protoc_plugin_rule' so that users can pass in custom arguments and environment variables to the protoc plugin.
1 parent 11fdb67 commit 1f1c750

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

rules.bzl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ def _protoc_plugin_rule_implementation(context):
6565
fail("Plugin name %s does not start with protoc-gen-" % plugin_name)
6666
plugin_short_name = plugin_name.removeprefix("protoc-gen-")
6767

68-
args = [
68+
# Args passed to 'create_protoc_plugin_rule' are not allowed to start with
69+
# '--plugin' or '--<plugin_short_name>_out'.
70+
for arg in context.attr._args:
71+
if arg.startswith("--plugin") or arg.startswith("--%s_out" % plugin_short_name):
72+
fail("Argument %s starts with --plugin or --%s_out, which is not allowed" % (arg, plugin_short_name))
73+
74+
args = context.attr._args + [
6975
"--plugin=%s=%s" % (plugin_name, plugin_path),
7076
"--%s_out" % plugin_short_name,
7177
output_directory,
@@ -116,14 +122,14 @@ def _protoc_plugin_rule_implementation(context):
116122
],
117123
command = context.executable._protoc.path + " $@",
118124
arguments = args,
119-
use_default_shell_env = True,
125+
env = context.attr._env,
120126
)
121127

122128
return [DefaultInfo(
123129
files = depset(output_files),
124130
)]
125131

126-
def create_protoc_plugin_rule(plugin_label, extensions = []):
132+
def create_protoc_plugin_rule(plugin_label, extensions = [], args = [], env = {}):
127133
return rule(
128134
attrs = {
129135
"deps": attr.label_list(
@@ -134,6 +140,14 @@ def create_protoc_plugin_rule(plugin_label, extensions = []):
134140
allow_files = True,
135141
mandatory = True,
136142
),
143+
"_args": attr.string_list(
144+
mandatory = False,
145+
default = args,
146+
),
147+
"_env": attr.string_dict(
148+
mandatory = False,
149+
default = env,
150+
),
137151
"_extensions": attr.string_list(
138152
default = extensions,
139153
),

0 commit comments

Comments
 (0)