Skip to content

Commit

Permalink
feat(bazel): Add Bazel rule for generating .swagger.json files (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeku authored and achew22 committed Apr 25, 2018
1 parent 892952a commit 739cd2d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/proto/examplepb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@grpc_ecosystem_grpc_gateway//protoc-gen-swagger:defs.bzl", "protoc_gen_swagger")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -55,3 +56,8 @@ go_library(
embed = [":examplepb_go_proto"],
importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/proto/examplepb",
)

protoc_gen_swagger(
name = "expamplepb_protoc_gen_swagger",
proto = ":examplepb_proto",
)
1 change: 1 addition & 0 deletions protoc-gen-swagger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
go_binary(
name = "protoc-gen-swagger",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

go_test(
Expand Down
75 changes: 75 additions & 0 deletions protoc-gen-swagger/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
def _collect_includes(srcs):
includes = ["."]
for src in srcs:
include = src.dirname
if include and not include in includes:
includes += [include]

return includes

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

args = actions.args()
args.add("--plugin=%s" % protoc_gen_swagger.path)
args.add("--swagger_out=logtostderr=true:%s" % 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],
outputs = [swagger_file],
arguments = [args],
)

swagger_files.append(swagger_file)

return swagger_files

def _proto_gen_swagger_impl(ctx):
proto = ctx.attr.proto.proto

return struct(
files=depset(
_run_proto_gen_swagger(
direct_proto_srcs = proto.direct_sources,
transitive_proto_srcs = ctx.files._well_known_protos + proto.transitive_sources.to_list(),
actions = ctx.actions,
protoc = ctx.executable._protoc,
protoc_gen_swagger = ctx.executable._protoc_gen_swagger,
)
)
)

protoc_gen_swagger = rule(
attrs = {
"proto": attr.label(
allow_rules = ["proto_library"],
mandatory = True,
providers = ['proto'],
),
"_protoc": attr.label(
default = "@com_google_protobuf//:protoc",
executable = True,
cfg = "host",
),
"_well_known_protos": attr.label(
default = "@com_google_protobuf//:well_known_protos",
allow_files = True,
),
"_protoc_gen_swagger": attr.label(
default = Label("@grpc_ecosystem_grpc_gateway//protoc-gen-swagger:protoc-gen-swagger"),
executable = True,
cfg = "host",
),
},
implementation = _proto_gen_swagger_impl,
)

0 comments on commit 739cd2d

Please sign in to comment.