Skip to content

Commit

Permalink
Fetch status.proto from remote, simplify JS gRPC codegen (digital-ass…
Browse files Browse the repository at this point in the history
…et#285)

* Fetch status.proto from remote, simplify JS gRPC codegen

Fetch the `status.proto` file (part of the standard gRPC distribution)
from a distribution channel. _Moreover_, use the recently introduced
`proto_gen` rule to simplify how the gRPC code for the Node.js bindings
are generated (and remove the need to have `google/rpc/status.proto`
locally in the repository.

* Add plugin_runfiles option to proto_gen

This allows use to add additional files to the bazel sandbox so that
plugins can refer to them. This will subsequently be used by the
protoc-gen-doc plugin.

Also, pass the plugin options via --name_opt parameter.

* Add missing status.proto dependency /language-support/java and /ledger

* Build proto docs using the proto_gen rule

To make this work, I had to turn on the bazel build flag
`--protocopt=--include_source_info` because we cannot turn enable this
flag only for specific build rules.

* Make /ledger-api/grpc-definitions:docs public again

* Revert to the old style of passing plugin arguments to --name_out=options:path

* Suppress output of unzipping

* Fix link for google.rpc.Status in proto-docs
  • Loading branch information
stefanobaghino-da authored Apr 9, 2019
1 parent 685904d commit 7467925
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 692 deletions.
12 changes: 10 additions & 2 deletions bazel_tools/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("//bazel_tools:pkg.bzl", "pkg_tar")

# taken from rules_proto:
# https://github.com/stackb/rules_proto/blob/f5d6eea6a4528bef3c1d3a44d486b51a214d61c2/compile.bzl#L369-L393
def get_plugin_runfiles(tool):
def get_plugin_runfiles(tool, plugin_runfiles):
"""Gather runfiles for a plugin.
"""
files = []
Expand All @@ -30,6 +30,10 @@ def get_plugin_runfiles(tool):
if runfiles.files:
files += runfiles.files.to_list()

if plugin_runfiles:
for target in plugin_runfiles:
files += target.files.to_list()

return files

def _proto_gen_impl(ctx):
Expand All @@ -52,7 +56,7 @@ def _proto_gen_impl(ctx):
plugin_runfiles = []
if ctx.attr.plugin_name not in ["java", "python"]:
plugins = [ctx.executable.plugin_exec]
plugin_runfiles = get_plugin_runfiles(ctx.attr.plugin_exec)
plugin_runfiles = get_plugin_runfiles(ctx.attr.plugin_exec, ctx.attr.plugin_runfiles)
args += [
"--plugin=protoc-gen-{}={}".format(ctx.attr.plugin_name, ctx.executable.plugin_exec.path)
]
Expand Down Expand Up @@ -129,6 +133,10 @@ proto_gen = rule(
executable = True
),
"plugin_options": attr.string_list(),
"plugin_runfiles": attr.label_list(
default = [],
allow_files = True,
),
"protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
cfg = "host",
Expand Down
3 changes: 2 additions & 1 deletion daml-lf/archive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ exports_files (
proto_library(
name = 'daml_lf_proto',
srcs = [':daml-lf-archive-srcs'],
proto_source_root = "daml-lf/archive"
proto_source_root = "daml-lf/archive",
visibility = ["//:__subpackages__"],
)

proto_gen(
Expand Down
8 changes: 8 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ java_import(
""",
)

if "com_github_googleapis_googleapis" not in native.existing_rules():
http_archive(
name = "com_github_googleapis_googleapis",
strip_prefix = "googleapis-6c48ab5aef47dc14e02e2dc718d232a28067129d",
urls = ["https://github.com/googleapis/googleapis/archive/6c48ab5aef47dc14e02e2dc718d232a28067129d.tar.gz"],
sha256 = "70d7be6ad49b4424313aad118c8622aab1c5fdd5a529d4215d3884ff89264a71",
)

native.bind(
name = "guava",
actual = "@com_google_guava_guava//jar",
Expand Down
1 change: 1 addition & 0 deletions language-support/java/bindings-rxjava/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ java_library(
"//3rdparty/jvm/org/checkerframework:checker",
"//3rdparty/jvm/io/grpc:grpc_netty",
"//3rdparty/jvm/io/grpc:grpc_core",
"//3rdparty/jvm/io/grpc:grpc_protobuf",
"//3rdparty/jvm/io/grpc:grpc_stub",
"//3rdparty/jvm/com/google/protobuf:protobuf_java",
"//3rdparty/jvm/io/netty:netty_handler",
Expand Down
6 changes: 4 additions & 2 deletions language-support/java/bindings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ proto_gen(
'@com_google_protobuf//:duration_proto',
'@com_google_protobuf//:wrappers_proto',
'@com_google_protobuf//:any_proto',
'@com_google_protobuf//:descriptor_proto'
'@com_google_protobuf//:descriptor_proto',
'@com_github_googleapis_googleapis//google/rpc:status_proto',
],
plugin_name = "java",
visibility = [
Expand All @@ -32,7 +33,8 @@ proto_gen(
'@com_google_protobuf//:duration_proto',
'@com_google_protobuf//:wrappers_proto',
'@com_google_protobuf//:any_proto',
'@com_google_protobuf//:descriptor_proto'
'@com_google_protobuf//:descriptor_proto',
'@com_github_googleapis_googleapis//google/rpc:status_proto',
],
plugin_name = "java-grpc",
plugin_exec = "@io_grpc_grpc_java//compiler:grpc_java_plugin",
Expand Down
18 changes: 18 additions & 0 deletions language-support/js/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ nodejs_binary(
visibility = ["//language-support/js:__subpackages__"]
)

nodejs_binary(
name = "grpc_tools_node_protoc_js",
data = [
"@npm//grpc-tools",
],
entry_point = "grpc-tools/bin/protoc_plugin.js",
visibility = ["//language-support/js:__subpackages__"]
)

nodejs_binary(
name = "grpc_tools_node_protoc_ts",
data = [
"@npm//grpc_tools_node_protoc_ts",
],
entry_point = "grpc_tools_node_protoc_ts/bin/protoc-gen-ts",
visibility = ["//language-support/js:__subpackages__"]
)

nodejs_binary(
name = "tsc",
data = [
Expand Down
124 changes: 117 additions & 7 deletions language-support/js/daml-grpc/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,104 @@
load("@build_bazel_rules_typescript//:defs.bzl", "ts_config")
load(":defs.bzl", "nodejs_proto_compile")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
load("//bazel_tools:proto.bzl", "proto_gen")

nodejs_proto_compile(
name = "daml-grpc-srcs"
proto_gen(
name = "ledger-api-nodejs",
srcs = ["//ledger-api/grpc-definitions:protos"],
deps = [
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_github_googleapis_googleapis//google/rpc:status_proto",
],
plugin_name = "js",
plugin_options = ["import_style=commonjs", "binary"],
plugin_exec = "//language-support/js:grpc_tools_node_protoc_js",
)

proto_gen(
name = "ledger-api-nodejs-grpc",
srcs = ["//ledger-api/grpc-definitions:protos"],
deps = [
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_github_googleapis_googleapis//google/rpc:status_proto",
],
plugin_name = "grpc",
plugin_exec = "//language-support/js:grpc_tools_node_protoc_js",
)

proto_gen(
name = "ledger-api-d.ts",
srcs = ["//ledger-api/grpc-definitions:protos"],
deps = [
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_github_googleapis_googleapis//google/rpc:status_proto",
],
plugin_name = "ts",
plugin_exec = "//language-support/js:grpc_tools_node_protoc_ts",
)

proto_gen(
name = "daml_lf_proto-nodejs",
srcs = ["//daml-lf/archive:daml_lf_proto"],
plugin_name = "js",
plugin_options = ["import_style=commonjs", "binary"],
plugin_exec = "//language-support/js:grpc_tools_node_protoc_js",
)

proto_gen(
name = "daml_lf_proto-d.ts",
srcs = ["//daml-lf/archive:daml_lf_proto"],
plugin_name = "ts",
plugin_exec = "//language-support/js:grpc_tools_node_protoc_ts",
)

proto_gen(
name = "com_google_protobuf-nodejs",
srcs = [
"@com_github_googleapis_googleapis//google/rpc:status_proto",
],
deps = [
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
],
plugin_name = "js",
plugin_options = ["import_style=commonjs", "binary"],
plugin_exec = "//language-support/js:grpc_tools_node_protoc_js",
)

proto_gen(
name = "com_google_protobuf-d.ts",
srcs = [
"@com_github_googleapis_googleapis//google/rpc:status_proto",
],
deps = [
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
],
plugin_name = "ts",
plugin_exec = "//language-support/js:grpc_tools_node_protoc_ts",
)

ts_config(
Expand All @@ -18,7 +114,13 @@ genrule(
"index.ts",
"lf/index.ts",
"testing/index.ts",
":daml-grpc-srcs",
":ledger-api-nodejs",
":ledger-api-nodejs-grpc",
":ledger-api-d.ts",
":daml_lf_proto-nodejs",
":daml_lf_proto-d.ts",
":com_google_protobuf-nodejs",
":com_google_protobuf-d.ts",
"package.json",
":tsconfig",
"@npm//@types/google-protobuf",
Expand All @@ -30,11 +132,19 @@ genrule(
cmd = """
set -e
ln -s external/npm/node_modules
cp -R $(@D)/src/* language-support/js/daml-grpc/
unzip -q $(location ledger-api-nodejs) -d language-support/js/daml-grpc/
unzip -q $(location ledger-api-nodejs-grpc) -d language-support/js/daml-grpc/
unzip -q $(location ledger-api-d.ts) -d language-support/js/daml-grpc/
unzip -q $(location daml_lf_proto-nodejs) -d language-support/js/daml-grpc/
unzip -q $(location daml_lf_proto-d.ts) -d language-support/js/daml-grpc/
unzip -q $(location com_google_protobuf-nodejs) -d language-support/js/daml-grpc/
unzip -q $(location com_google_protobuf-d.ts) -d language-support/js/daml-grpc/
$(execpath //language-support/js:tsc) --build $(location tsconfig)
mkdir -p package/dist
cp -R language-support/js/daml-grpc/dist package
cp -RL $(@D)/src/* package/dist
cp -RL language-support/js/daml-grpc/com package
cp -RL language-support/js/daml-grpc/da package
cp -RL language-support/js/daml-grpc/google package
cp -RL language-support/js/daml-grpc/grpc package
cp $(location package.json) package
tar czf $@ package
""",
Expand Down
Loading

0 comments on commit 7467925

Please sign in to comment.