Skip to content

Commit 671acca

Browse files
committed
Initial commit.
0 parents  commit 671acca

File tree

15 files changed

+436
-0
lines changed

15 files changed

+436
-0
lines changed

.travis.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
dist: trusty
2+
sudo: required
3+
language: node_js
4+
5+
env:
6+
- V=HEAD URL=http://ci.bazel.io/job/Bazel/JAVA_VERSION=1.8,PLATFORM_NAME=linux-x86_64/lastSuccessfulBuild/artifact/output/ci/bazel--installer.sh FLAGS='--worker_verbose --strategy=Javac=worker --strategy=JsChecker=worker'
7+
- V=0.3.1 URL=https://github.com/bazelbuild/bazel/releases/download/0.3.1/bazel-0.3.1-installer-linux-x86_64.sh FLAGS=''
8+
- V=0.3.0 URL=https://github.com/bazelbuild/bazel/releases/download/0.3.0/bazel-0.3.0-installer-linux-x86_64.sh FLAGS=''
9+
10+
before_install:
11+
- wget -O install.sh $URL
12+
- chmod +x install.sh
13+
- ./install.sh --user
14+
- rm -f install.sh
15+
16+
script:
17+
- |
18+
bazel \
19+
--output_base=$HOME/.cache/bazel \
20+
--batch \
21+
--host_jvm_args=-Xmx500m \
22+
--host_jvm_args=-Xms500m \
23+
build \
24+
--verbose_failures \
25+
--sandbox_debug \
26+
--spawn_strategy=standalone \
27+
--genrule_strategy=standalone \
28+
--local_resources=400,1,1.0 \
29+
//examples/... \
30+
$FLAGS
31+
32+
notifications:
33+
email: false

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 PubRef.org
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you
4+
may not use this file except in compliance with the License. You may
5+
obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
implied. See the License for the specific language governing
13+
permissions and limitations under the License.

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# `rules_node` [![Build Status](https://travis-ci.org/pubref/rules_node.svg?branch=master)](https://travis-ci.org/pubref/rules_node)
2+
3+
# Installation
4+
5+
load `rules_node` in your `WORKSPACE` and load main repository
6+
dependencies:
7+
8+
```python
9+
git_repository(
10+
name = "org_pubref_rules_node",
11+
tag = "v0.1.0",
12+
remote = "https://github.com/pubref/rules_node.git",
13+
)
14+
15+
load("@org_pubref_rules_node//node:rules.bzl", "node_repositories")
16+
17+
node_repositories() # installs node toolchain incl. 'node' and 'npm'
18+
```
19+
20+
# Rules
21+
22+
| Rule | Description |
23+
| :--- | ---: | :---------- |
24+
| [node_repositories](#node_repositories) | Install node toolchain. |
25+
| [npm_library](#npm_library) | Declare an npm dependency. |
26+
| [node_binary](#node_binary) | Build or execute a nodejs script. |
27+
28+
# Example
29+
30+
```python
31+
load("@org_pubref_rules_node//node:rules.bzl", "node_binary", "npm_library")
32+
33+
npm_library(
34+
name = "glob",
35+
)
36+
37+
npm_library(
38+
name = "react-stack",
39+
deps = {
40+
"react": "15.3.2",
41+
"react-dom": "15.3.2",
42+
},
43+
)
44+
45+
node_binary(
46+
name = "foo",
47+
main_script = "foo.js",
48+
npm_deps = ["glob", "react-stack"],
49+
)
50+
```
51+
52+
## node_\repositories
53+
54+
WORKSPACE rule. No current options.
55+
56+
## npm_\library
57+
58+
BUILD rule. Declares a set of npm dependencies. Functionally
59+
equivalent to `npm install --global` (global being relative to the npm
60+
toolchain installed in your WORKSPACE.
61+
62+
Takes two forms:
63+
64+
1. **Single import**: uses the name of the rule (see `glob` above).
65+
66+
1. **Multiple import**: uses a string_dict declaring the
67+
`name@version` dependency. (see `react-stack` above).
68+
69+
## node_\binary
70+
71+
BUILD rule. Create an executable script that will run the file named
72+
in the `main_script` attribute.

WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
workspace(name = "org_pubref_rules_node")
2+
3+
load("//node:rules.bzl", "node_repositories", "npm_repository")
4+
node_repositories()

examples/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//node:rules.bzl", "node_binary", "npm_library")
2+
3+
npm_library(
4+
name = "glob",
5+
)
6+
7+
npm_library(
8+
name = "react-stack",
9+
deps = {
10+
"react": "15.3.2",
11+
"react-dom": "15.3.2",
12+
},
13+
)
14+
15+
node_binary(
16+
name = "foo",
17+
main_script = "foo.js",
18+
npm_deps = ["glob", "react-stack"],
19+
)

examples/bar.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function() {
2+
return "Bar!";
3+
};

examples/foo.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var process = require("process");
2+
var bar = require("./bar.js");
3+
var glob = require("glob");
4+
5+
console.log('Hello, ' + bar());
6+
console.log("filename:", __filename);
7+
console.log("dirname:", __dirname);
8+
console.log("process.versions:", process.versions);
9+
console.log("process.argv ", process.argv);
10+
console.log("require paths:", module.paths);
11+
console.log("env:", process.env);

node/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package(default_visibility = ["//visibility:public"])

node/internal/node_binary.bzl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
_js_filetype = FileType([".js"])
2+
3+
SCRIPT_TEMPLATE = """
4+
#!/usr/bin/env bash
5+
set -e
6+
export NODE_PATH={node_paths}
7+
"{node_bin}" "{script_path}" $@
8+
"""
9+
10+
11+
def _get_global_node_modules_dir(ctx):
12+
node_bin = str(ctx.file.node_tool)
13+
node_parts = node_bin.partition("[source]]")
14+
node_prefix = node_parts[0][len("Artifact:["):]
15+
node_suffix_parts = node_parts[2].split("/")
16+
node_lib = "/".join([node_prefix] + node_suffix_parts[0:-2] + ["lib", "node_modules"])
17+
return node_lib
18+
19+
20+
def node_binary_impl(ctx):
21+
inputs = []
22+
node_paths = [_get_global_node_modules_dir(ctx)]
23+
for dep in ctx.attr.npm_deps:
24+
lib = dep.npm_library
25+
inputs.append(lib.package_marker)
26+
27+
script = SCRIPT_TEMPLATE.format(
28+
node_bin = ctx.file.node_tool.short_path,
29+
script_path = ctx.file.main_script.short_path,
30+
node_paths = ":".join(node_paths),
31+
)
32+
33+
ctx.file_action(
34+
output = ctx.outputs.executable,
35+
content = script,
36+
executable = True,
37+
)
38+
39+
runfiles = [ctx.file.node_tool, ctx.file.main_script] + inputs
40+
41+
return struct(
42+
runfiles = ctx.runfiles(
43+
files = runfiles,
44+
),
45+
)
46+
47+
48+
node_binary = rule(
49+
node_binary_impl,
50+
attrs = {
51+
"main_script": attr.label(
52+
single_file = True,
53+
allow_files = _js_filetype,
54+
),
55+
"node_tool": attr.label(
56+
default = Label("//node/toolchain:node_tool"),
57+
single_file = True,
58+
allow_files = True,
59+
executable = True,
60+
cfg = "host",
61+
),
62+
# "deps": attr.label_list(
63+
# providers = ["node_library"],
64+
# ),
65+
"npm_deps": attr.label_list(
66+
providers = ["npm_library"],
67+
),
68+
},
69+
executable = True,
70+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//node:internal/node_repository.bzl", "node_repository")
2+
3+
def node_repositories():
4+
5+
node_repository(
6+
name = "nodejs_linux_amd64",
7+
version = "v6.6.0",
8+
arch = "linux-x64",
9+
sha256 = "",
10+
type = "tar.gz",
11+
)
12+
13+
node_repository(
14+
name = "nodejs_darwin_amd64",
15+
version = "v6.6.0",
16+
arch = "darwin-x64",
17+
sha256 = "c8d1fe38eb794ca46aacf6c8e90676eec7a8aeec83b4b09f57ce503509e7a19f",
18+
type = "tar.gz",
19+
)

0 commit comments

Comments
 (0)