-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move protobufjs to userspace protobufjs_library macro
The Labs package has this old and crufty protobufjs_ts_library rule which is not really documented. The grpc-based one was meant to replace it. Instead of providing a rule in @bazel/labs, just add an example showing how this can be done in userspace with a macro. (there's also a one-liner rule to convert ProtoInfo to DefaultInfo so that the macro can interop with `proto_library`) NOTE: this is not a breaking change because the @bazel/labs package comes with no stability guarantee. If you're using protobufjs_ts_library you can duplicate the macro from the example to keep using it. refactor: use ProtoInfo in the protobufjs example This ensures that users don't have to repeat the dependency tree
- Loading branch information
Showing
25 changed files
with
1,320 additions
and
893 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
dist | ||
bazel-out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import %workspace%/../../common.bazelrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test") | ||
load("@npm//@bazel/typescript:index.bzl", "ts_project") | ||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
load(":defs.bzl", "protobufjs_library") | ||
|
||
proto_library( | ||
name = "car_proto", | ||
srcs = [ | ||
"car.proto", | ||
"tire.proto", | ||
], | ||
) | ||
|
||
protobufjs_library( | ||
# produces outputs named after this, | ||
# car.d.ts and car.js | ||
name = "car", | ||
proto = "car_proto", | ||
) | ||
|
||
ts_project( | ||
name = "test_lib", | ||
testonly = True, | ||
srcs = ["car.spec.ts"], | ||
tsconfig = "tsconfig.json", | ||
deps = [ | ||
":car", | ||
"@npm//@types/jasmine", | ||
"@npm//protobufjs", | ||
], | ||
) | ||
|
||
ts_project( | ||
name = "app", | ||
srcs = [ | ||
"app.ts", | ||
], | ||
tsconfig = "//:tsconfig.json", | ||
deps = [ | ||
":car", | ||
"@npm//protobufjs", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
deps = ["test_lib"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# protobufjs example | ||
|
||
This shows how the [protobuf.js](https://github.com/protobufjs/protobuf.js) | ||
build tools and runtime library can be used to consume `.proto` files. | ||
|
||
Note that the example requires some "userland" code to invoke the tools, | ||
since there is no "custom rule" to invoke them under Bazel. | ||
See `defs.bzl` for the sample code you will need. | ||
|
||
Currently the example doesn't exercise the Service definitions in the proto, | ||
but we expect this is easily added. It would be a great community contribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
workspace( | ||
name = "examples_protobufjs", | ||
managed_directories = {"@npm": ["node_modules"]}, | ||
) | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "build_bazel_rules_nodejs", | ||
sha256 = "6142e9586162b179fdd570a55e50d1332e7d9c030efd853453438d607569721d", | ||
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.0.0/rules_nodejs-3.0.0.tar.gz"], | ||
) | ||
|
||
http_archive( | ||
name = "rules_proto", | ||
sha256 = "2a20fd8af3cad3fbab9fd3aec4a137621e0c31f858af213a7ae0f997723fc4a9", | ||
strip_prefix = "rules_proto-a0761ed101b939e19d83b2da5f59034bffc19c12", | ||
urls = [ | ||
"https://github.com/bazelbuild/rules_proto/archive/a0761ed101b939e19d83b2da5f59034bffc19c12.tar.gz", | ||
], | ||
) | ||
|
||
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") | ||
|
||
rules_proto_dependencies() | ||
|
||
rules_proto_toolchains() | ||
|
||
load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install") | ||
|
||
yarn_install( | ||
name = "npm", | ||
package_json = "//:package.json", | ||
yarn_lock = "//:yarn.lock", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {Proto as pb} from './car_pb'; | ||
|
||
// TODO: use a service to fetch this data from a nodejs server | ||
// documentation: https://github.com/protobufjs/protobuf.js/tree/6.8.8#using-services | ||
const car = new pb.Car(); | ||
car.make = 'Porsche'; | ||
|
||
const el: HTMLDivElement = document.createElement('div'); | ||
el.innerText = `Car from server: ${car.make}`; | ||
el.className = 'ts1'; | ||
document.body.appendChild(el); | ||
|
||
const el2: HTMLDivElement = document.createElement('div'); | ||
el2.className = 'ts2'; | ||
document.body.appendChild(el2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
syntax = "proto3"; | ||
|
||
import "tire.proto"; | ||
|
||
package Proto; | ||
|
||
message Car { | ||
string make = 1; | ||
string model = 2; | ||
int32 year = 3; | ||
Tire front_tires = 4; | ||
Tire rear_tires = 5; | ||
int64 mileage = 6; | ||
} | ||
|
||
message GetCarRequest { | ||
} | ||
message GetCarResponse { | ||
} | ||
|
||
service CarService { | ||
rpc GetCar(GetCarRequest) returns (GetCarResponse) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {Proto as pb} from './car_pb'; | ||
|
||
describe('protocol buffers', () => { | ||
it('allows creation of an object described by proto', () => { | ||
const tires = pb.Tire.create(); | ||
tires.aspectRatio = 65; | ||
tires.width = 225; | ||
tires.construction = 'R'; | ||
tires.diameter = 17; | ||
|
||
const pontiac = pb.Car.create(); | ||
pontiac.make = 'pontiac'; | ||
pontiac.frontTires = tires; | ||
|
||
expect(pontiac.make).toEqual('pontiac'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"Illustrate how to wrap pbjs and pbts from protobufjs" | ||
|
||
load("@build_bazel_rules_nodejs//:index.bzl", "js_library") | ||
|
||
# TODO switch to protobufjs-cli when its published | ||
# https://github.com/protobufjs/protobuf.js/commit/da34f43ccd51ad97017e139f137521782f5ef119 | ||
load("@npm//protobufjs:index.bzl", "pbjs", "pbts") | ||
load("@rules_proto//proto:defs.bzl", "ProtoInfo") | ||
|
||
# protobuf.js relies on these packages, but does not list them as dependencies | ||
# in its package.json. | ||
# Instead they are listed under "cliDependencies" | ||
# (see https://unpkg.com/[email protected]/package.json) | ||
# When run, the CLI attempts to run `npm install` at runtime to get them. | ||
# This fails under Bazel as it tries to access the npm cache outside of the sandbox. | ||
# Per Bazel semantics, all dependencies should be pre-declared. | ||
# Note, you'll also need to install all of these in your package.json! | ||
# (This should be fixed when we switch to protobufjs-cli) | ||
_PROTOBUFJS_CLI_DEPS = ["@npm//%s" % s for s in [ | ||
"chalk", | ||
"escodegen", | ||
"espree", | ||
"estraverse", | ||
"glob", | ||
"jsdoc", | ||
"minimist", | ||
"semver", | ||
"tmp", | ||
"uglify-js", | ||
]] | ||
|
||
def _proto_sources_impl(ctx): | ||
return DefaultInfo(files = ctx.attr.proto[ProtoInfo].transitive_sources) | ||
|
||
_proto_sources = rule( | ||
doc = """Provider Adapter from ProtoInfo to DefaultInfo. | ||
Extracts the transitive_sources from the ProtoInfo provided by the proto attr. | ||
This allows a macro to access the complete set of .proto files needed during compilation. | ||
""", | ||
implementation = _proto_sources_impl, | ||
attrs = {"proto": attr.label(providers = [ProtoInfo])}, | ||
) | ||
|
||
def protobufjs_library(name, proto, **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 | ||
**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 | ||
|
||
# grab the transitive .proto files needed to compile the given one | ||
_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] + _PROTOBUFJS_CLI_DEPS, | ||
# 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, | ||
], | ||
outs = [js_out], | ||
) | ||
|
||
# Transform the _pb.js file to a .d.ts file with TypeScript types | ||
pbts( | ||
name = ts_target, | ||
data = [js_target] + _PROTOBUFJS_CLI_DEPS, | ||
# Arguments documented at | ||
# https://github.com/protobufjs/protobuf.js/tree/6.8.8#pbts-for-typescript | ||
args = [ | ||
"--out=$@", | ||
"$(execpath %s)" % js_target, | ||
], | ||
outs = [ts_out], | ||
) | ||
|
||
# Expose the results as js_library which provides DeclarationInfo for interop with other rules | ||
js_library( | ||
name = name, | ||
srcs = [ | ||
js_target, | ||
ts_target, | ||
], | ||
**kwargs | ||
) |
17 changes: 12 additions & 5 deletions
17
packages/labs/protobufjs/package.json → examples/protobufjs/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
{ | ||
"name": "protobufjs", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"private": true, | ||
"devDependencies": { | ||
"@bazel/jasmine": "^3.0.0", | ||
"@bazel/typescript": "^3.0.0", | ||
"@types/jasmine": "2.8.2", | ||
"@types/long": "^4.0.0", | ||
"@types/node": "11.11.1", | ||
"chalk": "^2.4.1", | ||
"escodegen": "^1.9.1", | ||
"espree": "^3.5.4", | ||
"estraverse": "^4.2.0", | ||
"glob": "^7.1.2", | ||
"jasmine": "2.8.0", | ||
"jsdoc": "^3.5.5", | ||
"long": "4.0.0", | ||
"minimist": "^1.2.0", | ||
"protobufjs": "On update, other dependencies here to be updated to the same versions as in protobufjs package.json", | ||
"protobufjs": "=6.8.8", | ||
"semver": "^5.5.0", | ||
"tmp": "0.0.33", | ||
"typescript": "^3.3.1", | ||
"uglify-js": "^3.3.25" | ||
}, | ||
"scripts": { | ||
"test": "bazel test ..." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto3"; | ||
|
||
package Proto; | ||
|
||
message Tire { | ||
string type = 1; | ||
int32 width = 2; | ||
int32 aspect_ratio = 3; | ||
string construction = 4; | ||
int32 diameter = 5; | ||
int32 load_index = 6; | ||
string speed_rating = 7; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"lib": ["ES2015", "dom"], | ||
// Include the output directory in rootDirs so that generated .d.ts files | ||
// can be used for type-checking in the editor, for example the car.proto | ||
// produces a car.d.ts. | ||
"rootDirs": [ | ||
".", | ||
"bazel-out/darwin-fastbuild/bin", | ||
"bazel-out/k8-fastbuild/bin", | ||
"bazel-out/x64_windows-fastbuild/bin", | ||
"bazel-out/darwin-dbg/bin", | ||
"bazel-out/k8-dbg/bin", | ||
"bazel-out/x64_windows-dbg/bin", | ||
] | ||
}, | ||
"include": ["*.ts"] | ||
} |
Oops, something went wrong.