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

feat(examples): use protobufjs with multiple proto targets #3544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 24 additions & 10 deletions examples/protobufjs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ load(":defs.bzl", "protobufjs_library")

proto_library(
name = "car_proto",
srcs = [
"car.proto",
"tire.proto",
],
srcs = ["car.proto"],
deps = [":tire_proto"],
)

proto_library(
name = "tire_proto",
srcs = ["tire.proto"],
)

proto_library(
name = "animal_proto",
srcs = ["animal.proto"],
)

protobufjs_library(
# produces outputs named after this,
# car.d.ts and car.js
name = "car",
proto = "car_proto",
# "car_and_animal.d.ts" and "car_and_animal.js".
name = "car_and_animal",
protos = [
":animal_proto",
":car_proto",
],
)

ts_project(
name = "test_lib",
testonly = True,
srcs = ["car.spec.ts"],
srcs = [
"animal.spec.ts",
"car.spec.ts",
],
tsconfig = "tsconfig.json",
deps = [
":car",
":car_and_animal",
"@npm//@types/jasmine",
"@npm//protobufjs",
],
Expand All @@ -37,7 +51,7 @@ ts_project(
],
tsconfig = "//:tsconfig.json",
deps = [
":car",
":car_and_animal",
"@npm//protobufjs",
],
)
Expand Down
8 changes: 8 additions & 0 deletions examples/protobufjs/animal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package Proto;

message Animal {
string name = 1;
bool has_fur = 2;
}
10 changes: 10 additions & 0 deletions examples/protobufjs/animal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Proto as pb} from './car_and_animal_pb';

describe('protocol buffers', () => {
it('allows creation of an object described by proto', () => {
const animal = pb.Animal.create({name: "cat", hasFur: true});

expect(animal.name).toEqual("cat");
expect(animal.hasFur).toEqual(true);
});
});
2 changes: 1 addition & 1 deletion examples/protobufjs/car.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Proto as pb} from './car_pb';
import {Proto as pb} from './car_and_animal_pb';

describe('protocol buffers', () => {
it('allows creation of an object described by proto', () => {
Expand Down
28 changes: 17 additions & 11 deletions examples/protobufjs/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,48 @@ _proto_sources = rule(
attrs = {"proto": attr.label(providers = [ProtoInfo])},
)

def protobufjs_library(name, proto, **kwargs):
def _sanitize_label(label_string):
"""Replace characters in a label so it can be used as part of a target name."""
return label_string.replace(":", "_").replace("/", "_")

def protobufjs_library(name, protos, **kwargs):
"""Minimal wrapper macro around pbjs/pbts tooling

Args:
name: name of generated js_library target, also used to name the .js/.d.ts outputs
proto: label of a single proto_library target to generate for
protos: labels of proto_library targets to generate for
**kwargs: passed through to the js_library
"""

js_out = name + "_pb.js"
ts_out = js_out.replace(".js", ".d.ts")

# Generate some target names, based on the provided name
# (so that they are unique if the macro is called several times in one package)
proto_target = "_%s_protos" % name
js_target = "_%s_pbjs" % name
ts_target = "_%s_pbts" % name

# Generate some target names, based on the provided name
# (so that they are unique if the macro is called several times in one package)
proto_targets = ["_%s_%s" % (name, _sanitize_label(proto)) for proto in protos]

# grab the transitive .proto files needed to compile the given one
_proto_sources(
name = proto_target,
proto = proto,
)
for proto, proto_target in zip(protos, proto_targets):
_proto_sources(
name = proto_target,
proto = proto,
)

# Transform .proto files to a single _pb.js file named after the macro
pbjs(
name = js_target,
data = [proto_target],
data = proto_targets,
# Arguments documented at
# https://github.com/protobufjs/protobuf.js/tree/6.8.8#pbjs-for-javascript
args = [
"--target=static-module",
"--wrap=default",
"--strict-long", # Force usage of Long type with int64 fields
"--out=$@",
"$(execpaths %s)" % proto_target,
" ".join(["$(execpaths %s)" % proto_target for proto_target in proto_targets]),
],
outs = [js_out],
)
Expand Down