From 0d29f14a9afe84adfbc8760b9fad6b394436fd3e Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Mon, 24 Jun 2019 11:19:17 -0400 Subject: [PATCH] protoc_gen_swagger: Add attr for single_output (#944) --- examples/proto/examplepb/BUILD.bazel | 8 +++- protoc-gen-swagger/defs.bzl | 68 +++++++++++++++++++++------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/examples/proto/examplepb/BUILD.bazel b/examples/proto/examplepb/BUILD.bazel index f5aa149eb8e..dd7a2d45c3c 100644 --- a/examples/proto/examplepb/BUILD.bazel +++ b/examples/proto/examplepb/BUILD.bazel @@ -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. +) diff --git a/protoc-gen-swagger/defs.bzl b/protoc-gen-swagger/defs.bzl index 936ca4791fe..4f90807f0f5 100644 --- a/protoc-gen-swagger/defs.bzl +++ b/protoc-gen-swagger/defs.bzl @@ -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, @@ -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 @@ -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, ), ), )] @@ -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,