Skip to content

Commit

Permalink
feat(bazel): re-introduce @nrwl/bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Dec 14, 2019
1 parent fb4fccb commit 2e2167d
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/angular/schematics.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"angular",
"bazel",
"cypress",
"express",
"insights",
Expand Down
1 change: 1 addition & 0 deletions docs/react/schematics.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"angular",
"bazel",
"cypress",
"express",
"insights",
Expand Down
1 change: 1 addition & 0 deletions docs/web/schematics.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"angular",
"bazel",
"cypress",
"express",
"insights",
Expand Down
53 changes: 53 additions & 0 deletions e2e/bazel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
checkFilesExist,
ensureProject,
forEachCli,
readFile,
runCLI,
uniq,
updateFile
} from './utils';

forEachCli(currentCLIName => {
describe('Bazel', () => {
const ngapp = uniq('ngapp');
const reactapp = uniq('reactapp');
const nglib = uniq('nglib');
const reactlib = uniq('reactlib');

it('should generate build files for apps', () => {
ensureProject();
runCLI(`generate @nrwl/angular:app ${ngapp}`);
runCLI('generate @nrwl/bazel:sync');
checkFilesExist(`apps/${ngapp}/BUILD.bazel`);

runCLI(`generate @nrwl/react:app ${reactapp}`);
runCLI('generate @nrwl/bazel:sync');
checkFilesExist(`apps/${reactapp}/BUILD.bazel`);
});

it('should generate build files for libs', () => {
runCLI(`generate @nrwl/angular:lib ${nglib}`);
runCLI('generate @nrwl/bazel:sync');
checkFilesExist(`libs/${nglib}/BUILD.bazel`);

runCLI(`generate @nrwl/angular:lib ${reactlib}`);
runCLI('generate @nrwl/bazel:sync');
checkFilesExist(`libs/${reactlib}/BUILD.bazel`);
});

it('should add dependencies to build files', () => {
updateFile(`apps/${ngapp}/src/main.ts`, `import '@proj/${nglib}';`);
runCLI('generate @nrwl/bazel:sync');
expect(readFile(`apps/${ngapp}/BUILD.bazel`)).toContain(
`//libs/${nglib}:${nglib}`
);

updateFile(`apps/${reactapp}/src/main.ts`, `import '@proj/${reactlib}';`);
runCLI('generate @nrwl/bazel:sync');
expect(readFile(`apps/${reactapp}/BUILD.bazel`)).toContain(
`//libs/${reactlib}:${reactlib}`
);
});
});
});
13 changes: 13 additions & 0 deletions packages/bazel/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Nx Bazel",
"version": "0.1",
"extends": [],
"schematics": {
"sync": {
"factory": "./src/schematics/sync/sync",
"schema": "./src/schematics/sync/schema.json",
"description": "Sync Build Files",
"hidden": true
}
}
}
Empty file added packages/bazel/index.ts
Empty file.
3 changes: 3 additions & 0 deletions packages/bazel/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"schematics": {}
}
54 changes: 54 additions & 0 deletions packages/bazel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@nrwl/bazel",
"version": "0.0.1",
"description": "Bazel Plugin for Nx",
"repository": {
"type": "git",
"url": "git+https://github.com/nrwl/nx.git"
},
"keywords": [
"Monorepo",
"Bazel",
"Angular",
"React",
"Web",
"Node",
"Nest",
"Jest",
"Cypress",
"CLI"
],
"main": "index.js",
"types": "index.d.ts",
"author": "Victor Savkin",
"license": "MIT",
"bugs": {
"url": "https://github.com/nrwl/nx/issues"
},
"homepage": "https://nx.dev",
"schematics": "./collection.json",
"builders": "./builders.json",
"ng-update": {
"requirements": {},
"migrations": "./migrations.json",
"packageGroup": [
"@nrwl/workspace",
"@nrwl/angular",
"@nrwl/cypress",
"@nrwl/express",
"@nrwl/jest",
"@nrwl/nest",
"@nrwl/node",
"@nrwl/react",
"@nrwl/web"
]
},
"dependencies": {
"@angular-devkit/core": "8.3.14",
"@angular-devkit/schematics": "8.3.14",
"@nrwl/cli": "*"
},
"peerDependencies": {
"@nrwl/workspace": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ==== Generated by Nx
# ==== Do not touch

load("@npm//@nrwl/cli:index.bzl", "nx", "nx_test")

filegroup(
name = "<%= project.name %>",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)

nx(
name = "build",
args = [
"build",
"<%= project.name%>",
"--outputPath=$@",
],
data = [
":<%= project.name %>",
# Dependencies
<% for (dep of dependencies) { %>"//<%= dep %>",
<% } %>
# Root Files
"//:angular.json",
"//:package.json",
"//:tsconfig.json",
# NOTE: Use @npm//node_modules causes bundling to fail due to Babel errors.
"@npm//@angular-devkit/build-angular",
"@npm//@angular/cli",
"@npm//@angular/core",
"@npm//@angular/platform-browser-dynamic",
"@npm//@angular/router",
"@npm//@babel/preset-env",
"@npm//@nrwl/workspace",
"@npm//inquirer",
],
output_dir = True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports_files(
[
<% for (rootFile of rootFiles) { %>"<%= rootFile %>",
<% } %>
],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Bazel workspace created by @bazel/create 0.38.1

# Declares that this directory is the root of a Bazel workspace.
# See https://docs.bazel.build/versions/master/build-ref.html#workspace
workspace(
# How this workspace would be referenced with absolute labels from another workspace
name = "<%= name %>",
# Map the @npm bazel workspace to the node_modules directory.
# This lets Bazel use the same node_modules as other local tooling.
managed_directories = {"@npm": ["node_modules"]},
)

# Install the nodejs "bootstrap" package
# This provides the basic tools for running and packaging nodejs programs in Bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "c612d6b76eaa17540e8b8c806e02701ed38891460f9ba3303f4424615437887a",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.1/rules_nodejs-0.42.1.tar.gz"],
)

# The yarn_install rule runs yarn anytime the package.json or yarn.lock file changes.
# It also extracts and installs any Bazel rules distributed in an npm package.
load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
# Name this npm so that Bazel Label references look like @npm//package
name = "npm",
data = ["//patches"],
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

# Install any Bazel rules which were extracted earlier by the yarn_install rule.
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()
8 changes: 8 additions & 0 deletions packages/bazel/src/schematics/sync/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsNxApp",
"title": "Nx Application Options Schema",
"type": "object",
"properties": {},
"required": []
}
94 changes: 94 additions & 0 deletions packages/bazel/src/schematics/sync/sync.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Tree } from '@angular-devkit/schematics';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { runSchematic } from '../utils/testing';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';

describe('@nrwl/bazel:sync', () => {
let tree: Tree;

beforeEach(() => {
tree = Tree.empty();
tree = createEmptyWorkspace(tree);
tree.create('tools/schematics/custom/index.ts', '');
});

describe('WORKSPACE', () => {
it('should be created', async () => {
const result = await runSchematic('sync', {}, tree);

expect(result.exists('WORKSPACE')).toEqual(true);
});

it('should name the workspace validly', async () => {
const result = await runSchematic('sync', {}, tree);

const contents = stripIndents`${result.readContent('WORKSPACE')}`;

expect(contents).toContain('name = "test_name"');
});

it('should import nodejs bazel rules', async () => {
const result = await runSchematic('sync', {}, tree);

const contents = stripIndents`${result.readContent('WORKSPACE')}`;

expect(contents).toContain(stripIndents`
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "c612d6b76eaa17540e8b8c806e02701ed38891460f9ba3303f4424615437887a",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.1/rules_nodejs-0.42.1.tar.gz"],
)
`);
});

it('should import yarn bazel rules and install', async () => {
const result = await runSchematic('sync', {}, tree);

const contents = stripIndents`${result.readContent('WORKSPACE')}`;

expect(contents).toContain(stripIndents`
load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")
yarn_install(
# Name this npm so that Bazel Label references look like @npm//package
name = "npm",
data = ["//patches"],
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)
# Install any Bazel rules which were extracted earlier by the yarn_install rule.
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
install_bazel_dependencies()
`);
});
});

describe('root BUILD.bazel', () => {
it('should be created', async () => {
const result = await runSchematic('sync', {}, tree);
expect(result.exists('BUILD.bazel')).toEqual(true);
});

it('should export root files', async () => {
const result = await runSchematic('sync', {}, tree);

const contents = stripIndents`${result.readContent('BUILD.bazel')}`;

expect(contents).toContain(stripIndents`exports_files(
[
"workspace.json",
"package.json",
"nx.json",
"tsconfig.json",
"tslint.json",
],
visibility = ["//:__subpackages__"],
)
`);
});
});
});
Loading

0 comments on commit 2e2167d

Please sign in to comment.