Skip to content

Commit

Permalink
protoc_gen_swagger: Add attr for single_output (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon authored and achew22 committed Jun 24, 2019
1 parent d6fec18 commit 0d29f14
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
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

0 comments on commit 0d29f14

Please sign in to comment.