Skip to content

Commit

Permalink
Add support for the grpc_api_configuration option in the bazel rule.
Browse files Browse the repository at this point in the history
PR grpc-ecosystem#521 added support for grpc service configurations. Add an option to let
users specify this config in the bazel rule.

Also add a simple doc blob to the rule.
  • Loading branch information
ensonic committed Apr 30, 2018
1 parent 87a1b0c commit 50a301c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions protoc-gen-swagger/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Generated an open-api spec for a grpc api spec.
Reads the the api spec in protobuf format and generate an open-api spec.
Optionally applies settings from the grpc-service configuration.
"""
def _collect_includes(srcs):
includes = ["."]
for src in srcs:
Expand All @@ -7,17 +12,21 @@ def _collect_includes(srcs):

return includes

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

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

args = actions.args()
args.add("--plugin=%s" % protoc_gen_swagger.path)
args.add("--swagger_out=logtostderr=true:%s" % swagger_file.dirname)
args.add("--swagger_out=%s:%s" % (",".join(options), swagger_file.dirname))
args.add("-Iexternal/com_google_protobuf/src")
args.add("-Iexternal/com_github_googleapis_googleapis")
args.add(["-I%s" % include for include in _collect_includes(direct_proto_srcs + transitive_proto_srcs)])
Expand All @@ -36,6 +45,9 @@ def _run_proto_gen_swagger(direct_proto_srcs, transitive_proto_srcs, actions, pr

def _proto_gen_swagger_impl(ctx):
proto = ctx.attr.proto.proto
grpc_api_configuration = None
if ctx.file.grpc_api_configuration:
grpc_api_configuration = ctx.file.grpc_api_configuration.path

return struct(
files=depset(
Expand All @@ -45,6 +57,7 @@ def _proto_gen_swagger_impl(ctx):
actions = ctx.actions,
protoc = ctx.executable._protoc,
protoc_gen_swagger = ctx.executable._protoc_gen_swagger,
grpc_api_configuration = grpc_api_configuration
)
)
)
Expand All @@ -56,6 +69,10 @@ protoc_gen_swagger = rule(
mandatory = True,
providers = ['proto'],
),
"grpc_api_configuration": attr.label(
allow_single_file=True,
mandatory=False
),
"_protoc": attr.label(
default = "@com_google_protobuf//:protoc",
executable = True,
Expand Down

0 comments on commit 50a301c

Please sign in to comment.