Skip to content

Commit

Permalink
Update to recent rules_node (#131)
Browse files Browse the repository at this point in the history
* Update to rules_node using pkg_tar.files attribute
* Update example
* Update README
  • Loading branch information
pcj authored Sep 24, 2017
1 parent d9523f3 commit 183ab18
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 70 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ all: build test
build: external_proto_library_build
$(BAZEL_BUILD) \
//examples/extra_args:person_tar \
//examples/helloworld/node:client \
//examples/helloworld/node:server \
//examples/helloworld/go/client \
//examples/helloworld/go/server \
//examples/helloworld/grpc_gateway:swagger \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ of this project are to:
| [Go (gogo)](gogo) | [gogo_proto_compile](gogo#gogo_proto_compile) | [gogo_proto_library](gogo#gogo_proto_library) | [fb8a35](https://github.com/gogo/protobuf/commit/fb8a359905af6e2b6517cccda0ba25915322ee88) |
| [gRPC gateway](grpc_gateway) | [grpc_gateway_proto_compile](grpc_gateway#grpc_gateway_proto_compile)<br/>[grpc_gateway_swagger_compile](grpc_gateway#grpc_gateway_swagger_compile) | [grpc_gateway_proto_library](grpc_gateway#grpc_gateway_proto_library)<br/>[grpc_gateway_binary](grpc_gateway#grpc_gateway_binary) | [v1.2.2+ (f2862b)](https://github.com/grpc-ecosystem/grpc-gateway/commit/f2862b476edcef83412c7af8687c9cd8e4097c0f) |
| [Java](java) | [java_proto_compile](java#java_proto_compile) | [java_proto_library](java#java_proto_library) | [v1.6.1](https://github.com/grpc/grpc-java/releases/tag/v1.6.1) |
| [Node](node) | [node_proto_compile](js#node_proto_compile) | [node_proto_library](js#node_proto_library) | [1.0.0](https://www.npmjs.com/package/grpc) |
| [Node](node) | [node_proto_compile](node#node_proto_compile) | [node_proto_library](node#node_proto_library) | [1.6.0](https://www.npmjs.com/package/grpc) |
| [Objective-C](objc) | [objc_proto_compile](objc#objc_proto_compile) | [objc_proto_library](objc#objc_proto_library) <sup>4</sup> | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Python](python) | [py_proto_compile](python#py_proto_compile) | [py_proto_library](python#py_proto_library) | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Ruby](ruby) | [ruby_proto_compile](ruby#ruby_proto_compile) | | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
Expand Down
13 changes: 10 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ csharp_repositories()
# node_proto_library support requires rules_node
# ================================================================


github_archive(
name = "org_pubref_rules_node",
commit = "f6fff71fe8b1bee8d3a22e50eca0f76427ab939e",
# This commit is *not* on master but rather https://github.com/pubref/rules_node/pull/35.
commit = "2b4b38dcb76eba98419c932c98bd22fe6e322c9f",
org = "pubref",
repo = "rules_node",
sha256 = "94c22db354edc9c21541c713f8d8ace381c8fb7b2c232a2b623c393abe9cb8e6",
sha256 = "64e30037b931fb01c5c5f2e38477bdd234ba95b51138153c13bec8d156982978",
)

load("@org_pubref_rules_node//node:rules.bzl", "node_repositories")
load("@org_pubref_rules_node//node:rules.bzl", "node_repositories", "yarn_modules")

node_repositories()

yarn_modules(
name = "yarn_modules",
package_json = "//node:package.json",
)

# ================================================================
# Python GRPC support requires rules_python
Expand All @@ -92,6 +98,7 @@ load("@pip_grpcio//:requirements.bzl", pip_grpcio_install = "pip_install")

pip_grpcio_install()


# ================================================================
# Specific Languages Support
# ================================================================
Expand Down
13 changes: 2 additions & 11 deletions examples/helloworld/node/BUILD
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
load("@org_pubref_rules_node//node:rules.bzl", "node_binary")

# Server works, but not the the client (for reasons I dont yet
# understand)

node_binary(
name = "client",
main = "greeter_client.js",
modules = [
"@npm_protobuf_stack//:modules",
"@npm_grpc//:modules",
],
deps = [
"//examples/helloworld/proto:node",
"@yarn_modules//:_all_",
],
)

node_binary(
name = "server",
main = "greeter_server.js",
modules = [
"@npm_protobuf_stack//:modules",
"@npm_grpc//:modules",
],
deps = [
"//examples/helloworld/proto:node",
"@yarn_modules//:_all_",
],
)
32 changes: 12 additions & 20 deletions examples/helloworld/node/greeter_client.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
const fs = require('fs')
const path = require('path')
const jspb = require('google-protobuf');
const grpc = require('grpc');
const messages = require('examples-helloworld-proto-node');
const services_path = path.dirname(require.resolve('examples-helloworld-proto-node'));
const services = require(services_path + '/helloworld_grpc_pb');
const messages = require('examples/helloworld/node/api_proto_js').api_pb;
const services = require('examples/helloworld/node/api_proto_js').api_grpc_pb;

function main() {
console.log("Running main");
var cred = grpc.credentials.createInsecure()
console.log(cred.constructor.classname)
console.log("grpc.credentials: " + cred)
var client = new services.GreeterClient('localhost:50051', cred);
var client = new services.GreeterClient(
'localhost:50051',
grpc.credentials.createInsecure());
var request = new messages.HelloRequest();
var user;
if (process.argv.length >= 3) {
user = process.argv[2];
} else {
user = 'world';
}

request.setName(user);
client.sayHello(request, function(err, response) {
console.log('Greeting:', response.getMessage());
if (err) {
console.error('Error:', err);
} else {
console.log('Greeting:', response.getMessage());
}
});


// var user = "Foo";
// var request = new messages.HelloRequest();
// request.setName(user);
// console.log("Hello " + request.getName());

}

main();
11 changes: 4 additions & 7 deletions examples/helloworld/node/greeter_server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const path = require('path')
const messages = require('examples-helloworld-proto-node');
const services_path = path.dirname(require.resolve('examples-helloworld-proto-node'));
const services = require(services_path + '/helloworld_grpc_pb');

var grpc = require('grpc');
const grpc = require('grpc');
const messages = require('examples/helloworld/node/api_proto_js').api_pb;
const services = require('examples/helloworld/node/api_proto_js').api_grpc_pb;

/**
* Implements the SayHello RPC method.
Expand All @@ -20,7 +17,7 @@ function sayHello(call, callback) {
*/
function main() {
var server = new grpc.Server();
server.addService(services.GreeterService, {sayHello: sayHello});
server.addService(services.GreeterService, { sayHello: sayHello });
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
}
Expand Down
50 changes: 45 additions & 5 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
| ---: | :--- |
| [node_proto_repositories](#node_proto_repositories) | Load workspace dependencies. |
| [node_proto_compile](#node_proto_compile) | Generate node js protobuf source files. |

> NOTE: These rules are currenty *not* compatible with the bazel sandbox.
> To use them you'll have to use `--spawn_strategy=standalone`.
| [node_proto_library](#node_proto_library) | Generate a node module packaged with protobuf sources. |

## node\_proto\_repositories

Enable node support by loading the dependencies in your workspace.

> IMPORTANT: This should occur after loading
> [rules_node](https://github.com/pubref/rules_node).
> This should occur after loading
> [rules_node](https://github.com/pubref/rules_node). See the
> `WORKSPACE` file for the appropriate commit to use from that repo.
```python
load("@org_pubref_rules_protobuf//node:rules.bzl", "node_proto_repositories")
Expand Down Expand Up @@ -43,3 +42,44 @@ Target //:protos up-to-date:
bazel-genfiles/message_pb.js
bazel-genfiles/message_grpc_pb.js
```


## node\_proto\_library

Load the requisite external node modules via the `yarn_modules` repository rule in your `WORKSPACE`:

```python
# WORKSPACE
load("@org_pubref_rules_node//node:rules.bzl", "yarn_modules")

yarn_modules(
name = "yarn_modules",
deps = {
"google-protobuf": "3.4.0",
"grpc": "1.6.0"
},
)
```

Then, in your `BUILD` file:

```python
load("@org_pubref_rules_node//node:rules.bzl", "node_binary")
load("//node:rules.bzl", "node_proto_library")

node_proto_library(
name = "api",
protos = ["api.proto"],
verbose = 0,
with_grpc = True,
)

node_binary(
name = "server",
main = "server.js",
deps = [
":api",
"@yarn_modules//:_all_",
],
)
```
8 changes: 8 additions & 0 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rules_protobuf",
"version": "1.0.0",
"dependencies": {
"google-protobuf": "3.4.0",
"grpc": "1.6.0"
}
}
87 changes: 64 additions & 23 deletions node/rules.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@org_pubref_rules_node//node:rules.bzl", "node_library", "npm_repository")
load("@org_pubref_rules_node//node:rules.bzl", "node_module")
load("//cpp:rules.bzl", "cpp_proto_repositories")
load("//node:deps.bzl", "DEPS")

Expand All @@ -11,8 +11,8 @@ def node_proto_repositories(
omit_cpp_repositories = False,
lang_deps = DEPS,
lang_requires = [
"npm_protobuf_stack",
"npm_grpc",
# "npm_protobuf_stack",
# "npm_grpc",
],
**kwargs):

Expand All @@ -27,14 +27,64 @@ def node_proto_repositories(
for dep in rem:
rule = dep.pop("rule")
if "npm_repository" == rule:
npm_repository(**dep)
fail("Unknown loading rule %s for %s" % (rule, dep))
#npm_repository(**dep)
else:
fail("Unknown loading rule %s for %s" % (rule, dep))


def _get_js_variable_name(file):
name = file.basename.rstrip(".js")
# Deal with special characters here?
return name


def _node_proto_module_impl(ctx):
compilation = ctx.attr.compilation.proto_compile_result
index_js = ctx.new_file("%s/index.js" % ctx.label.name)

exports = {}

for unit in compilation.transitive_units:
for file in unit.outputs:
if file.path.endswith("_pb.js"):
name = _get_js_variable_name(file)
exports[name] = file.short_path
elif file.path.endswith("_grpc_pb.js"):
name = _get_js_variable_name(file)
exports[name] = file.short_path

content = []
content.append("module.exports = {")
for name, path in exports.items():
content.append(" '%s': require('./%s')," % (name, path))
content.append("}")

ctx.file_action(
output = index_js,
content = "\n".join(content)
)

return struct(
files = depset([index_js]),
)


_node_proto_module = rule(
implementation = _node_proto_module_impl,
attrs = {
"compilation": attr.label(
providers = ["proto_compile_result"],
mandatory = True,
)
}
)


def node_proto_compile(langs = [str(Label("//node"))], **kwargs):
proto_compile(langs = langs, **kwargs)


def node_proto_library(
name,
langs = [str(Label("//node"))],
Expand All @@ -43,36 +93,29 @@ def node_proto_library(
inputs = [],
output_to_workspace = False,
proto_deps = [
#"@io_bazel_rules_node//closure/protobuf:jspb",
],
protoc = None,
pb_plugin = None,
pb_options = [],
proto_compile_args = {},
srcs = [],
deps = [],
deps = [
"@yarn_modules//:google-protobuf",
],
data = [],
verbose = 0,
with_grpc = False,

requiremap = {},

**kwargs):

special_pb_options = []
if requiremap:
entries = ["%s=%s" % (k, v) for k, v in requiremap.items()]
rmap = ",".join(entries)
special_pb_options += ["require_map='%s'" % rmap]

proto_compile_args += {
"name": name + ".pb",
"protos": protos,
"deps": [dep + ".pb" for dep in proto_deps],
"langs": langs,
"imports": imports,
"inputs": inputs,
"pb_options": pb_options + special_pb_options,
"pb_options": pb_options,
"output_to_workspace": output_to_workspace,
"verbose": verbose,
"with_grpc": with_grpc,
Expand All @@ -85,17 +128,15 @@ def node_proto_library(

proto_compile(**proto_compile_args)

proto_language_deps(
name = name + "_compile_deps",
langs = langs,
file_extensions = [".js"],
_node_proto_module(
name = name + "_index",
compilation = name + ".pb",
)

# How to pass along the set of files that the pb file imported?
# Hmm. Or should node_library not really exist?

node_library(
node_module(
name = name,
index = name + "_index",
layout = "workspace",
srcs = srcs + [name + ".pb"],
data = data + [dep + ".pb" for dep in proto_deps],
deps = depset(deps + proto_deps).to_list(),
Expand Down

0 comments on commit 183ab18

Please sign in to comment.