Skip to content

Commit

Permalink
feat(bazel): Add rule for generating .swagger.json files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeku committed Apr 24, 2018
1 parent e8bd377 commit 83b7a7b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/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:protoc-gen-swagger.bzl", "protoc_gen_swagger")

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

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

protoc_gen_swagger(
name = "expamplepb_protoc_gen_swagger",
proto_service = "a_bit_of_everything.proto",
deps = [
"//examples/sub:sub_proto",
"//examples/sub2:sub2_proto",
"//protoc-gen-swagger/options:options_proto",
"@com_github_googleapis_googleapis//google/api:api_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_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
79 changes: 79 additions & 0 deletions protoc-gen-swagger/protoc-gen-swagger.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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(proto_service, transitive_proto_srcs, actions, protoc, protoc_gen_swagger):
swagger_file = actions.declare_file(
"%s.swagger.json" % proto_service.basename[:-len(".proto")],
sibling = proto_service,
)

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/grpc_ecosystem_grpc_gateway/third_party/googleapis/")
args.add(["-I%s" % include for include in _collect_includes(transitive_proto_srcs)])
args.add(proto_service.basename)

actions.run(
executable = protoc,
inputs = transitive_proto_srcs + [protoc_gen_swagger],
outputs = [swagger_file],
arguments = [args],
)

return swagger_file

def _proto_gen_swagger_impl(ctx):
transitive_proto_srcs = depset([ctx.file.proto_service] + ctx.files._googleapis + ctx.files._well_known_protos)
for dep in ctx.attr.deps:
transitive_proto_srcs = depset(transitive=[transitive_proto_srcs, dep.proto.transitive_sources])

return struct(
files=depset([
_run_proto_gen_swagger(
ctx.file.proto_service,
transitive_proto_srcs.to_list(),
ctx.actions,
ctx.executable._protoc,
ctx.executable._protoc_gen_swagger
)
])
)

protoc_gen_swagger = rule(
implementation = _proto_gen_swagger_impl,
attrs = {
"proto_service": attr.label(
mandatory=True,
allow_single_file = True,
),
"deps": attr.label_list(
allow_rules = ["proto_library"]
),
"_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"
),
"_googleapis": attr.label(
default = Label("@grpc_ecosystem_grpc_gateway//third_party/googleapis:googleapis"),
allow_files = True,
)
},
)
6 changes: 6 additions & 0 deletions third_party/googleapis/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "googleapis",
srcs = glob(["**/*.proto"])
)

0 comments on commit 83b7a7b

Please sign in to comment.