Skip to content

Commit

Permalink
Add install_tools option to yarn_modules rule (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj authored Feb 12, 2018
1 parent cbff612 commit e383505
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
14 changes: 12 additions & 2 deletions node/internal/yarn_modules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ def _yarn_modules_impl(ctx):
execute(ctx, ["cp", parse_yarn_lock_js, "internal/parse_yarn_lock.js"])
execute(ctx, ["cp", yarn_js, "yarn.js"])

install_path = [node.dirname]
for tool in ctx.attr.install_tools:
tool_path = ctx.which(tool)
if not tool_path:
fail("Required install tool '%s' is not in the PATH" % tool, "install_tools")
install_path.append(tool_path.dirname)
install_path.append("$PATH")

# Build node_modules via 'yarn install'
execute(ctx, [node, yarn_js, "install"], quiet = True, environment = {
# postinstall scripts may need to find the node binary
"PATH": "%s:$PATH" % ctx.path(node).dirname,
"PATH": ":".join(install_path),
})

# Run the script and save the stdout to our BUILD file(s)
Expand Down Expand Up @@ -93,6 +100,9 @@ yarn_modules = repository_rule(
default = Label("@yarn//:bin/yarn.js"),
single_file = True,
),
# If specififed, augment the PATH environment variable with these
# tools during 'yarn install'.
"install_tools": attr.string_list(),
"package_json": attr.label(
mandatory = False,
allow_files = FileType(["package.json"]),
Expand Down
19 changes: 19 additions & 0 deletions tests/node-ref/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])

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

node_binary(
name = "example",
main = "example.js",
deps = [
"@yarn_modules//:_all_",
],
)

sh_test(
name = "example_test",
size = "small",
srcs = ["example_test.sh"],
data = [":example"],
)

9 changes: 9 additions & 0 deletions tests/node-ref/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ref Example

This example demonstrates the `install_tools` option.

In this case, `yarn install` of the `ref` package triggers `node-gyp`,
which in this case uses tools in `/bin` and `/usr/bin`. We normally
take these for granted as being on the path but bazel runs the yarn
install script in the bare environment, so we need to specify these
explicitly to build up the appropriate `PATH`.
22 changes: 22 additions & 0 deletions tests/node-ref/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local_repository(
name = "org_pubref_rules_node",
path = "../..",
)

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

node_repositories()

yarn_modules(
name = "yarn_modules",
deps = {
"ref": "1.3.5",
},
# yarn install of 'ref' triggers node-gyp. This will in turn
# require 'sh' and 'dirname'. Explicitly make these tools
# available during the yarn install step.
install_tools = [
"sh", # /bin
"dirname", # #/usr/bin
],
)
9 changes: 9 additions & 0 deletions tests/node-ref/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const ref = require("ref");

const buf = new Buffer(4)

buf.writeInt32LE(12345, 0)
buf.type = ref.types.int

console.log(buf.deref()) // ← 12345

7 changes: 7 additions & 0 deletions tests/node-ref/example_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set -e

if (./example &) | grep -q '12345'; then
echo "PASS"
else
exit 1
fi

0 comments on commit e383505

Please sign in to comment.