Skip to content

Commit

Permalink
feat(builtin): create @nodejs//:npm_bin, @nodejs//:npx_bin & @nodejs/…
Browse files Browse the repository at this point in the history
…/:yarn_bin targets

* Also create @nodejs//:node_files, @nodejs//:npm_files & @nodejs//:yarn_files filegroups
* Cleanup & simplify node_repositories.bzl code
* Cleanup & simplify @nodejs//:BUILD.bazel file
  • Loading branch information
gregmagolan committed Nov 21, 2019
1 parent 9802e88 commit dc57136
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 105 deletions.
2 changes: 0 additions & 2 deletions internal/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ exports_files([
"node_repositories.bzl", # Exported to be consumed for generating skydoc.
"node_launcher.sh",
"node_loader.js",
"BUILD.nodejs_host_os_alias.tpl",
])

filegroup(
Expand All @@ -42,7 +41,6 @@ filegroup(
"*.sh",
]) + [
"BUILD.bazel",
"BUILD.nodejs_host_os_alias.tpl",
],
visibility = ["//:__pkg__"],
)
27 changes: 0 additions & 27 deletions internal/node/BUILD.nodejs_host_os_alias.tpl

This file was deleted.

157 changes: 81 additions & 76 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ See https://docs.bazel.build/versions/master/skylark/repository_rules.html

load("//internal/common:check_bazel_version.bzl", "check_bazel_version")
load("//internal/common:check_version.bzl", "check_version")
load("//internal/common:os_name.bzl", "OS_ARCH_NAMES", "is_windows_os", "os_name")
load("//internal/common:os_name.bzl", "OS_ARCH_NAMES", "os_name")
load("//third_party/github.com/bazelbuild/bazel-skylib:lib/paths.bzl", "paths")
load("//toolchains/node:node_toolchain_configure.bzl", "node_toolchain_configure")

Expand Down Expand Up @@ -341,46 +341,52 @@ def _prepare_node(repository_ctx):

# TODO: Maybe we want to encode the OS as a specific attribute rather than do it based on naming?
is_windows = "_windows_" in repository_ctx.attr.name

if repository_ctx.attr.vendored_node:
node_exec = "/".join([f for f in [
node_path = "/".join([f for f in [
"../../..",
repository_ctx.attr.vendored_node.workspace_root,
repository_ctx.attr.vendored_node.package,
repository_ctx.attr.vendored_node.name,
"bin/node" if not is_windows else "node.exe",
] if f])
node_exec_label = "@%s//%s:%s/%s" % (
node_package = "@%s//%s:%s" % (
repository_ctx.attr.vendored_node.workspace_name,
repository_ctx.attr.vendored_node.package,
repository_ctx.attr.vendored_node.name,
"bin/node" if not is_windows else "node.exe",
)
npm_script = "/".join([f for f in [
"../../..",
repository_ctx.attr.vendored_node.workspace_root,
repository_ctx.attr.vendored_node.package,
repository_ctx.attr.vendored_node.name,
"lib/node_modules/npm/bin/npm-cli.js" if not is_windows else "node_modules/npm/bin/npm-cli.js",
] if f])
else:
node_exec = ("%s/bin/node" % NODE_EXTRACT_DIR) if not is_windows else ("%s/node.exe" % NODE_EXTRACT_DIR)
npm_script = ("%s/lib/node_modules/npm/bin/npm-cli.js" % NODE_EXTRACT_DIR) if not is_windows else ("%s/node_modules/npm/bin/npm-cli.js" % NODE_EXTRACT_DIR)
node_exec_label = node_exec
node_path = NODE_EXTRACT_DIR
node_package = NODE_EXTRACT_DIR

if repository_ctx.attr.vendored_yarn:
yarn_script = "/".join([f for f in [
yarn_path = "/".join([f for f in [
"../../..",
repository_ctx.attr.vendored_yarn.workspace_root,
repository_ctx.attr.vendored_yarn.package,
repository_ctx.attr.vendored_yarn.name,
"bin/yarn.js",
] if f])
yarn_package = "@%s//%s:%s" % (
repository_ctx.attr.vendored_yarn.workspace_name,
repository_ctx.attr.vendored_yarn.package,
repository_ctx.attr.vendored_yarn.name,
)
else:
yarn_script = "%s/bin/yarn.js" % YARN_EXTRACT_DIR
node_entry = "bin/node" if not is_windows else "bin/node.cmd"
npm_node_repositories_entry = "bin/npm_node_repositories" if not is_windows else "bin/npm_node_repositories.cmd"
yarn_node_repositories_entry = "bin/yarn_node_repositories" if not is_windows else "bin/yarn_node_repositories.cmd"
yarn_path = YARN_EXTRACT_DIR
yarn_package = YARN_EXTRACT_DIR

node_exec_relative = node_exec if repository_ctx.attr.vendored_node else paths.relativize(node_exec, "bin")
node_bin = ("%s/bin/node" % node_path) if not is_windows else ("%s/node.exe" % node_path)
node_bin_label = ("%s/bin/node" % node_package) if not is_windows else ("%s/node.exe" % node_package)
node_entry = "bin/node" if not is_windows else "bin/node.cmd"
npm_bin = ("%s/bin/npm" % node_path) if not is_windows else ("%s/npm.cmd" % node_path)
npm_bin_label = ("%s/bin/npm" % node_package) if not is_windows else ("%s/npm.cmd" % node_package)
npm_script = ("%s/lib/node_modules/npm/bin/npm-cli.js" % node_path) if not is_windows else ("%s/node_modules/npm/bin/npm-cli.js" % node_path)
npx_bin = ("%s/bin/npx" % node_path) if not is_windows else ("%s/npx.cmd" % node_path)
npx_bin_label = ("%s/bin/npx" % node_package) if not is_windows else ("%s/npx.cmd" % node_package)
yarn_bin = ("%s/bin/yarn" % yarn_path) if not is_windows else ("%s/yarn.cmd" % yarn_path)
yarn_bin_label = ("%s/bin/yarn" % yarn_package) if not is_windows else ("%s/npx.yarn" % yarn_package)
yarn_script = "%s/bin/yarn.js" % yarn_path

node_bin_relative = node_bin if repository_ctx.attr.vendored_node else paths.relativize(node_bin, "bin")
npm_script_relative = npm_script if repository_ctx.attr.vendored_node else paths.relativize(npm_script, "bin")
yarn_script_relative = yarn_script if repository_ctx.attr.vendored_yarn else paths.relativize(yarn_script, "bin")

Expand Down Expand Up @@ -413,7 +419,7 @@ export PATH="$SCRIPT_DIR":$PATH
exec "$SCRIPT_DIR/{node}" {args} "$@"
""".format(
get_script_dir = GET_SCRIPT_DIR,
node = node_exec_relative,
node = node_bin_relative,
args = node_args,
))
else:
Expand All @@ -423,7 +429,7 @@ exec "$SCRIPT_DIR/{node}" {args} "$@"
SET SCRIPT_DIR=%~dp0
SET PATH=%SCRIPT_DIR%;%PATH%
CALL "%SCRIPT_DIR%\\{node}" {args} %*
""".format(node = node_exec_relative, args = node_args))
""".format(node = node_bin_relative, args = node_args))

# Shell script to set repository arguments for node used by nodejs_binary & nodejs_test launcher
repository_ctx.file("bin/node_repo_args.sh", content = """#!/usr/bin/env bash
Expand Down Expand Up @@ -585,24 +591,45 @@ if %errorlevel% neq 0 exit /b %errorlevel%
package(default_visibility = ["//visibility:public"])
exports_files([
"run_npm.sh.template",
"bin/node_repo_args.sh",{exported_node_bin}
"bin/node_repo_args.sh",{node_bin_export}{npm_bin_export}{npx_bin_export}{yarn_bin_export}
"bin/node{entry_ext}",
"bin/npm{entry_ext}",
"bin/npm_node_repositories{entry_ext}",
"bin/yarn{entry_ext}",
"bin/yarn_node_repositories{entry_ext}",
])
alias(name = "node_bin", actual = "{node_bin_actual}")
alias(name = "node", actual = "{node_actual}")
alias(name = "npm", actual = "{npm_actual}")
alias(name = "yarn", actual = "{yarn_actual}")
alias(name = "node_bin", actual = "{node_bin_label}")
alias(name = "npm_bin", actual = "{npm_bin_label}")
alias(name = "npx_bin", actual = "{npx_bin_label}")
alias(name = "yarn_bin", actual = "{yarn_bin_label}")
alias(name = "node", actual = "{node_entry}")
alias(name = "npm", actual = "{npm_entry}")
alias(name = "yarn", actual = "{yarn_entry}")
filegroup(
name = "node_files",
srcs = [":node", ":node_bin"],
)
filegroup(
name = "yarn_files",
srcs = glob(["bin/yarnpkg/**"]) + [":node_files"],
)
filegroup(
name = "npm_files",
srcs = glob(["bin/nodejs/**"]) + [":node_files"],
)
""".format(
entry_ext = ".cmd" if is_windows else "",
exported_node_bin = "" if repository_ctx.attr.vendored_node else ("\n \"%s\"," % node_exec_label),
node_bin_actual = node_exec_label,
node_actual = node_entry,
npm_actual = npm_node_repositories_entry,
yarn_actual = yarn_node_repositories_entry,
node_bin_export = "" if repository_ctx.attr.vendored_node else ("\n \"%s\"," % node_bin),
npm_bin_export = "" if repository_ctx.attr.vendored_node else ("\n \"%s\"," % npm_bin),
npx_bin_export = "" if repository_ctx.attr.vendored_node else ("\n \"%s\"," % npx_bin),
yarn_bin_export = "" if repository_ctx.attr.vendored_yarn else ("\n \"%s\"," % yarn_bin),
node_bin_label = node_bin_label,
npm_bin_label = npm_bin_label,
npx_bin_label = npx_bin_label,
yarn_bin_label = yarn_bin_label,
node_entry = node_entry,
npm_entry = "bin/npm_node_repositories" if not is_windows else "bin/npm_node_repositories.cmd",
yarn_entry = "bin/yarn_node_repositories" if not is_windows else "bin/yarn_node_repositories.cmd",
))

def _nodejs_repo_impl(repository_ctx):
Expand All @@ -619,44 +646,26 @@ node_repositories_rule = repository_rule(
)

def _nodejs_host_os_alias_impl(repository_ctx):
is_windows_host = is_windows_os(repository_ctx)
file_ending = ".cmd" if is_windows_host else ""
node_repository = "@nodejs_%s" % os_name(repository_ctx)
if repository_ctx.attr.vendored_node:
node_bin_repository = "@%s" % repository_ctx.attr.vendored_node.workspace_name
actual_node_bin = "/".join([f for f in [
repository_ctx.attr.vendored_node.package,
repository_ctx.attr.vendored_node.name,
"bin/node" if not is_windows_host else "node.exe",
] if f])
else:
node_bin_repository = node_repository
actual_node_bin = "%s/%s" % (
NODE_EXTRACT_DIR,
"node.exe" if is_windows_host else "bin/node",
)
repository_ctx.template(
"BUILD.bazel",
Label("@build_bazel_rules_nodejs//internal/node:BUILD.nodejs_host_os_alias.tpl"),
substitutions = {
"TEMPLATE__npm_node_repositories": "%s//:bin/npm_node_repositories%s" % (node_repository, file_ending),
"TEMPLATE__yarn_node_repositories": "%s//:bin/yarn_node_repositories%s" % (node_repository, file_ending),
"TEMPLATE_actual_node_bin": "%s//:%s" % (node_bin_repository, actual_node_bin),
"TEMPLATE_node_repo_args": "%s//:bin/node_repo_args.sh" % node_repository,
"TEMPLATE_npm": "%s//:bin/npm%s" % (node_repository, file_ending),
"TEMPLATE_run_npm": "%s//:run_npm.sh.template" % node_repository,
"TEMPLATE_wrapped_node_bin": "%s//:bin/node%s" % (node_repository, file_ending),
"TEMPLATE_yarn": "%s//:bin/yarn%s" % (node_repository, file_ending),
},
executable = False,
)

_nodejs_repo_host_os_alias = repository_rule(
_nodejs_host_os_alias_impl,
attrs = {
"vendored_node": attr.label(allow_single_file = True),
},
)
# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", content = """# Generated by node_repositories.bzl
package(default_visibility = ["//visibility:public"])
# aliases for exports_files
alias(name = "run_npm.sh.template", actual = "{node_repository}//:run_npm.sh.template")
alias(name = "bin/node_repo_args.sh", actual = "{node_repository}//:bin/node_repo_args.sh")
# aliases for other aliases
alias(name = "node_bin", actual = "{node_repository}//:node_bin")
alias(name = "npm_bin", actual = "{node_repository}//:npm_bin")
alias(name = "npx_bin", actual = "{node_repository}//:npx_bin")
alias(name = "yarn_bin", actual = "{node_repository}//:yarn_bin")
alias(name = "node", actual = "{node_repository}//:node")
alias(name = "npm", actual = "{node_repository}//:npm")
alias(name = "yarn", actual = "{node_repository}//:yarn")
alias(name = "node_files", actual = "{node_repository}//:node_files")
alias(name = "yarn_files", actual = "{node_repository}//:yarn_files")
alias(name = "npm_files", actual = "{node_repository}//:npm_files")
""".format(node_repository = "@nodejs_%s" % os_name(repository_ctx)))

_nodejs_repo_host_os_alias = repository_rule(_nodejs_host_os_alias_impl)

def node_repositories(**kwargs):
"""
Expand All @@ -676,16 +685,13 @@ def node_repositories(**kwargs):
minimum_bazel_version = "0.21.0",
)

vendored_node = kwargs.pop("vendored_node", None)

# This needs to be setup so toolchains can access nodejs for all different versions
for os_arch_name in OS_ARCH_NAMES:
os_name = "_".join(os_arch_name)
node_repository_name = "nodejs_%s" % os_name
_maybe(
node_repositories_rule,
name = node_repository_name,
vendored_node = vendored_node,
**kwargs
)
native.register_toolchains("@build_bazel_rules_nodejs//toolchains/node:node_%s_toolchain" % os_arch_name[0])
Expand All @@ -699,7 +705,6 @@ def node_repositories(**kwargs):
_maybe(
_nodejs_repo_host_os_alias,
name = "nodejs",
vendored_node = vendored_node,
)

def _maybe(repo_rule, name, **kwargs):
Expand Down

0 comments on commit dc57136

Please sign in to comment.