Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc_gen_swagger: Add attr for allow_merge #944

Merged
merged 9 commits into from
Jun 24, 2019
8 changes: 7 additions & 1 deletion examples/proto/examplepb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ go_library(
)

protoc_gen_swagger(
name = "expamplepb_protoc_gen_swagger",
name = "examplepb_protoc_gen_swagger",
proto = ":examplepb_proto",
)

protoc_gen_swagger(
name = "examplepb_protoc_gen_swagger_merged",
proto = ":examplepb_proto",
single_output = True, # Outputs a single swagger.json file.
)
68 changes: 52 additions & 16 deletions protoc-gen-swagger/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,38 @@ def _collect_includes(gen_dir, srcs):

return includes

def _run_proto_gen_swagger(ctx, direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger, grpc_api_configuration):
def _run_proto_gen_swagger(ctx, direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger, grpc_api_configuration, single_output):
swagger_files = []
for proto in direct_proto_srcs:
swagger_file = actions.declare_file(
"%s.swagger.json" % proto.basename[:-len(".proto")],
sibling = proto,
)

inputs = direct_proto_srcs + transitive_proto_srcs
tools = [protoc_gen_swagger]
inputs = direct_proto_srcs + transitive_proto_srcs
tools = [protoc_gen_swagger]

options = ["logtostderr=true", "allow_repeated_fields_in_body=true"]
if grpc_api_configuration:
options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
inputs.append(grpc_api_configuration)
options = ["logtostderr=true", "allow_repeated_fields_in_body=true"]
if grpc_api_configuration:
options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
inputs.append(grpc_api_configuration)

includes = _collect_includes(ctx.genfiles_dir.path, direct_proto_srcs + transitive_proto_srcs)

if single_output:
swagger_file = actions.declare_file(
"%s.swagger.json" % ctx.attr.name,
sibling = direct_proto_srcs[0],
)
output_dir = ctx.bin_dir.path
if proto.owner.workspace_root:
output_dir = "/".join([output_dir, proto.owner.workspace_root])
if direct_proto_srcs[0].owner.workspace_root:
output_dir = "/".join([output_dir, direct_proto_srcs[0].owner.workspace_root])

output_dir = "/".join([output_dir, direct_proto_srcs[0].dirname])

includes = _collect_includes(ctx.genfiles_dir.path, direct_proto_srcs + transitive_proto_srcs)
options.append("allow_merge=true")
options.append("merge_file_name=%s" % ctx.attr.name)

args = actions.args()
args.add("--plugin=%s" % protoc_gen_swagger.path)
args.add("--swagger_out=%s:%s" % (",".join(options), output_dir))
args.add_all(["-I%s" % include for include in includes])
args.add(proto.path)
args.add_all([src.path for src in direct_proto_srcs])

actions.run(
executable = protoc,
Expand All @@ -67,6 +72,32 @@ def _run_proto_gen_swagger(ctx, direct_proto_srcs, transitive_proto_srcs, action
)

swagger_files.append(swagger_file)
else:
for proto in direct_proto_srcs:
swagger_file = actions.declare_file(
"%s.swagger.json" % proto.basename[:-len(".proto")],
sibling = proto,
)

output_dir = ctx.bin_dir.path
if proto.owner.workspace_root:
output_dir = "/".join([output_dir, proto.owner.workspace_root])

args = actions.args()
args.add("--plugin=%s" % protoc_gen_swagger.path)
args.add("--swagger_out=%s:%s" % (",".join(options), output_dir))
args.add_all(["-I%s" % include for include in includes])
args.add(proto.path)

actions.run(
executable = protoc,
inputs = inputs,
tools = tools,
outputs = [swagger_file],
arguments = [args],
)

swagger_files.append(swagger_file)

return swagger_files

Expand All @@ -84,6 +115,7 @@ def _proto_gen_swagger_impl(ctx):
protoc = ctx.executable._protoc,
protoc_gen_swagger = ctx.executable._protoc_gen_swagger,
grpc_api_configuration = grpc_api_configuration,
single_output = ctx.attr.single_output,
),
),
)]
Expand All @@ -99,6 +131,10 @@ protoc_gen_swagger = rule(
allow_single_file = True,
mandatory = False,
),
"single_output": attr.bool(
default = False,
mandatory = False,
),
"_protoc": attr.label(
default = "@com_google_protobuf//:protoc",
executable = True,
Expand Down