Skip to content

Commit 6e4ccd1

Browse files
committed
Introduce @bazel/create package
This gives us an even simpler novice user on-ramp to create a Bazel/npm project
1 parent 9de8728 commit 6e4ccd1

8 files changed

Lines changed: 165 additions & 10 deletions

File tree

‎.bazelignore‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ dist
66
e2e/
77
# Examples are tested by running examples/test_example.sh outside of Bazel
88
examples/
9-
# Each package is a nested workspace
10-
packages/
9+
# Most packages are nested workspaces
10+
packages/jasmine
11+
packages/karma
12+
packages/labs
13+
packages/typescript
1114
# A subset of legacy e2e tests are nested workspaces
1215
# TODO(gregmagolan): move these to /e2e
1316
internal/e2e/bazel_workspaces_compat

‎BUILD.bazel‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ load("@build_bazel_rules_nodejs//:defs.bzl", "COMMON_REPLACEMENTS", "npm_package
44

55
package(default_visibility = ["//visibility:public"])
66

7-
exports_files(["tsconfig.json"])
7+
exports_files([
8+
"common.bazelrc",
9+
"tsconfig.json",
10+
])
811

912
bzl_library(
1013
name = "bzl",

‎common.bazelrc‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
# Common Bazel settings
2-
# These are imported into the .bazelrc files in each workspace in this repo
1+
# Common Bazel settings for JavaScript/NodeJS workspaces
2+
# This rc file is automatically discovered when Bazel is run in this workspace,
3+
# see https://docs.bazel.build/versions/master/guide.html#bazelrc
4+
#
5+
# The full list of Bazel options: https://docs.bazel.build/versions/master/command-line-reference.html
6+
7+
# Output artifacts will be placed in a directory called "dist/bin"
8+
# Other directories will be created like "dist/testlogs"
9+
# Be aware that this will still create a bazel-out symlink in
10+
# your project directory, which you must exclude from version control and your
11+
# editor's search path.
12+
build --symlink_prefix=dist/
313

414
# Print test logs for failed tests
515
test --test_output=errors
616

7-
# Enable debugging tests with --config=debug
17+
# Enable debugging tests
18+
# Add the Bazel option `--config=debug` to enable it
819
test:debug --test_arg=--node_options=--inspect-brk --test_output=streamed --test_strategy=exclusive --test_timeout=9999 --nocache_test_results
920

1021
# Turn off legacy external runfiles
1122
run --nolegacy_external_runfiles
1223
test --nolegacy_external_runfiles
1324

1425
# Turn on managed_directories
26+
# This allows Bazel to share the same node_modules directory with other tools
1527
build --experimental_allow_incremental_repository_updates
1628
query --experimental_allow_incremental_repository_updates

‎packages/create/BUILD.bazel‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
2+
3+
# Copy common bazelrc file to be included in this package
4+
genrule(
5+
name = "copy_bazelrc",
6+
srcs = ["//:common.bazelrc"],
7+
outs = ["common.bazelrc"],
8+
cmd = "cp $< $@",
9+
)
10+
11+
npm_package(
12+
name = "npm_package",
13+
srcs = [
14+
"README.md",
15+
"index.js",
16+
"package.json",
17+
],
18+
deps = [
19+
":common.bazelrc",
20+
],
21+
)

‎packages/create/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# @bazel/create
2+
3+
This package allows users to have a very simple quickstart to get up and running with Bazel via npm or yarn.
4+
5+
```bash
6+
$ yarn create @bazel
7+
# or
8+
$ npm init @bazel
9+
```
10+
11+
See https://yarnpkg.com/en/docs/cli/create and https://docs.npmjs.com/cli/init
12+
13+
# Design
14+
15+
We version @bazel/create along with the other packages in this monorepo.
16+
17+
yarn and npm will download this package globally. For this reason we take no dependencies here to minimize the risk of version conflicts in the global install space.

‎packages/create/index.js‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function main(args) {
5+
if (args.length < 1) {
6+
console.error(`Please specify the workspace directory:
7+
8+
npx @bazel/create [workspace name]
9+
npm init @bazel [workspace name]
10+
yarn create @bazel [workspace name]
11+
`);
12+
return 1;
13+
}
14+
// FIXME: the workspace name should conform with Bazel rules
15+
const [wkspDir] = args;
16+
// TODO: user might want these to differ
17+
const wkspName = wkspDir;
18+
console.log(`Creating Bazel workspace ${wkspName}...`);
19+
fs.mkdirSync(wkspDir);
20+
21+
function write(workspaceRelativePath, content) {
22+
fs.writeFileSync(
23+
path.join(wkspDir, workspaceRelativePath), content + require('os').EOL,
24+
{encoding: 'utf-8'});
25+
}
26+
27+
write('WORKSPACE', `# Bazel workspace created by @bazel/create
28+
29+
# Declares that this directory is the root of a Bazel workspace.
30+
# See https://docs.bazel.build/versions/master/build-ref.html#workspace
31+
workspace(
32+
# How this workspace would be referenced with absolute labels from another workspace
33+
name = "${wkspName}",
34+
managed_directories = {"@npm": ["node_modules"]},
35+
)
36+
37+
# Install the nodejs "bootstrap" package
38+
#
39+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
40+
http_archive(
41+
name = "build_bazel_rules_nodejs",
42+
sha256 = "abcf497e89cfc1d09132adfcd8c07526d026e162ae2cb681dcb896046417ce91",
43+
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.30.1/rules_nodejs-0.30.1.tar.gz"],
44+
)
45+
46+
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
47+
yarn_install(
48+
name = "npm",
49+
package_json = "//:package.json",
50+
yarn_lock = "//:yarn.lock",
51+
)
52+
53+
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
54+
install_bazel_dependencies()`);
55+
write('BUILD.bazel', `# Add rules here to build your software
56+
# See https://docs.bazel.build/versions/master/build-ref.html#BUILD_files`);
57+
write('.bazelignore', `node_modules`);
58+
write(
59+
'package.json',
60+
JSON.stringify(
61+
{
62+
name: wkspName,
63+
version: '0.1.0',
64+
private: true,
65+
devDependencies:
66+
{'@bazel/bazel': 'next', '@bazel/ibazel': 'latest', '@bazel/buildifier': 'latest'},
67+
scripts: {'build': 'bazel build //...', 'test': 'bazel test //...'}
68+
},
69+
null, 4));
70+
// in the published distribution, this file will appear in the same folder as this file
71+
if (fs.existsSync('common.bazelrc')) {
72+
write('.bazelrc', fs.readFileSync('common.bazelrc'))
73+
}
74+
// but running locally against sources, it's in the root of the repo two directories up
75+
if (fs.existsSync('../../common.bazelrc')) {
76+
write('.bazelrc', fs.readFileSync('../../common.bazelrc'));
77+
} else {
78+
console.error('ERROR: missing common.bazelrc file, continuing with no bazel settings...');
79+
}
80+
81+
console.log(`Successfully created new Bazel workspace at ${path.resolve(wkspDir)}
82+
Inside that directory you can run yarn build or yarn test.`)
83+
}
84+
85+
if (require.main === module) {
86+
process.exitCode = main(process.argv.slice(2));
87+
}

‎packages/create/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@bazel/create",
3+
"bin": "index.js",
4+
"license": "Apache 2.0",
5+
"version": "0.0.0-PLACEHOLDER",
6+
"keywords": [
7+
"bazel"
8+
],
9+
"description": "Create a new Bazel workspace"
10+
}

‎scripts/on-release.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ const hash = require('crypto').createHash('sha256');
1010
// RAM
1111
const sha256 = hash.update(fs.readFileSync(artifact)).digest('hex');
1212

13-
shell.sed(
14-
'-i', 'download/[0-9\.]*/rules_nodejs-[0-9\.]*.tar.gz',
15-
`download/${version}/rules_nodejs-${version}.tar.gz`, 'README.md');
16-
shell.sed('-i', 'sha256 = \"[0-9a-f]+\"', `sha256 = "${sha256}"`, 'README.md');
13+
for (const f in ['README.md', 'packages/create/index.js']) {
14+
shell.sed(
15+
'-i', 'download/[0-9\.]*/rules_nodejs-[0-9\.]*.tar.gz',
16+
`download/${version}/rules_nodejs-${version}.tar.gz`, f);
17+
shell.sed('-i', 'sha256 = \"[0-9a-f]+\"', `sha256 = "${sha256}"`, f);
18+
}
1719
shell.cp(artifact, `rules_nodejs-${version}.tar.gz`);

0 commit comments

Comments
 (0)