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

Add support for the grpc_api_configuration option in the bazel rule. #632

Merged
merged 2 commits into from
May 5, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 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,25 +12,32 @@ 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,
)

inputs = direct_proto_srcs + transitive_proto_srcs + [protoc_gen_swagger]

options=["logtostderr=true"]
if grpc_api_configuration:
options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
inputs.append(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)])
args.add(proto.basename)

actions.run(
executable = protoc,
inputs = direct_proto_srcs + transitive_proto_srcs + [protoc_gen_swagger],
inputs = inputs,
outputs = [swagger_file],
arguments = [args],
)
Expand All @@ -36,6 +48,8 @@ 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm unnecessary line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite done?

grpc_api_configuration = ctx.file.grpc_api_configuration

return struct(
files=depset(
Expand All @@ -45,6 +59,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 +71,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