Repository navigation
Expand file tree
/
Copy pathnode_install.bzl
More file actions
133 lines (119 loc) · 4.84 KB
/
Copy pathnode_install.bzl
File metadata and controls
133 lines (119 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2017 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.
"""Install NodeJS when the user runs node_repositories() from their WORKSPACE.
We fetch a specific version of Node, to ensure builds are hermetic.
We then create a repository @nodejs which provides the
node binary to other rules.
"""
def _node_impl(repository_ctx):
node = "bin/node"
npm = "bin/npm"
os_name = repository_ctx.os.name.lower()
if os_name.startswith("mac os"):
repository_ctx.download_and_extract(
[
"http://mirror.bazel.build/nodejs.org/dist/v6.10.2/node-v6.10.2-darwin-x64.tar.xz",
"https://nodejs.org/dist/v6.10.2/node-v6.10.2-darwin-x64.tar.xz",
],
stripPrefix = "node-v6.10.2-darwin-x64",
sha256 = "360b887361b2597613f18968e3fc0e920079a363d0535fc4e40532e3426fc6eb"
)
elif os_name.find("windows") != -1:
repository_ctx.download_and_extract(
[
"http://mirror.bazel.build/nodejs.org/dist/v6.10.2/node-v6.10.2-win-x64.zip",
"http://nodejs.org/dist/v6.10.2/node-v6.10.2-win-x64.zip",
],
stripPrefix = "node-v6.10.2-win-x64",
sha256 = "d778ed84685c6604192cfcf40192004e27fb11c9e65c3ce4b283d90703b4192c"
)
# The windows distribution of nodejs has the binaries in different paths
node = "node.exe"
npm = "npm.cmd"
else:
repository_ctx.download_and_extract(
[
"http://mirror.bazel.build/nodejs.org/dist/v6.10.2/node-v6.10.2-linux-x64.tar.xz",
"http://nodejs.org/dist/v6.10.2/node-v6.10.2-linux-x64.tar.xz",
],
stripPrefix = "node-v6.10.2-linux-x64",
sha256 = "b519cd616b0671ab789d2645c5c026deb7e016d73a867ab4b1b8c9ceba9c3503"
)
repository_ctx.file("BUILD.bazel", content="""#Generated by node_install.bzl
package(default_visibility = ["//visibility:public"])
exports_files([
"{0}",
"{1}",
])
alias(name = "node", actual = "{0}")
alias(name = "npm", actual = "{1}")
""".format(node, npm))
_node_repo = repository_rule(_node_impl, attrs = {})
# def _write_node_modules_impl(ctx):
# WORKAROUND for https://github.com/bazelbuild/bazel/issues/374#issuecomment-296217940
# Bazel does not allow labels to start with `@`, so when installing eg. the `@types/node`
# module from the @types scoped package, you'll get an error.
# The workaround is to move the rule up one level, from /node_modules to the project root.
# For now, users must instead write their own /BUILD file on setup.
# ctx.symlink(project_dir.get_child("node_modules"), "node_modules")
# add a BUILD file inside the user's node_modules project folder
# ctx.file("installed/BUILD", """
# filegroup(name = "node_modules", srcs = glob(["node_modules/**/*"]), visibility = ["//visibility:public"])
# """)
# _write_node_modules = repository_rule(
# _write_node_modules_impl,
# attrs = { "package_json": attr.label() },
# )
def _yarn_impl(ctx):
# Yarn is a package manager that downloads dependencies. Yarn is an improvement over the `npm` tool in
# speed and correctness. We download a specific version of Yarn to ensure a hermetic build.
ctx.file("BUILD.bazel", """
package(default_visibility = ["//visibility:public"])
sh_binary(
name = "yarn",
srcs = ["yarn.sh"],
)
""")
if ctx.os.name.lower().find("windows") != -1:
node = Label("@nodejs//:node.exe")
else:
node = Label("@nodejs//:bin/node")
ctx.file("yarn.sh", "#!/bin/bash" + "".join(["""
ROOT="{}"
NODE="{}"
SCRIPT="{}"
(cd "$ROOT"; "$NODE" "$SCRIPT" "$@")
""".format(
ctx.path(package_json).dirname,
ctx.path(node),
ctx.path("bin/yarn.js"))
for package_json in ctx.attr.package_json]), executable = True)
ctx.download_and_extract(
[
"http://mirror.bazel.build/github.com/yarnpkg/yarn/releases/download/v0.22.0/yarn-v0.22.0.tar.gz",
"https://github.com/yarnpkg/yarn/releases/download/v0.22.0/yarn-v0.22.0.tar.gz",
],
stripPrefix = "dist",
sha256 = "e295042279b644f2bc3ea3407a2b2fb417a200d35590b0ec535422d21cf19a09"
)
_yarn_repo = repository_rule(
_yarn_impl,
attrs = { "package_json": attr.label_list() },
)
load(":check_bazel_version.bzl", "check_bazel_version")
def node_repositories(package_json):
# Windows users need sh_binary wrapped as an .exe
check_bazel_version("0.5.4")
_node_repo(name = "nodejs")
_yarn_repo(name = "yarn", package_json = package_json)