Skip to content

Commit

Permalink
feat(angular): introduce an Angular CLI builder
Browse files Browse the repository at this point in the history
This replaces the one being removed in angular/angular#37190
  • Loading branch information
Alex Eagle authored and alexeagle committed May 26, 2020
1 parent f9f3dea commit c87c83f
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
bazel-out
e2e/symlinked_node_modules_yarn/node_modules
e2e/symlinked_node_modules_npm/node_modules/
packages/angular/node_modules
1 change: 1 addition & 0 deletions .github/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ generate_codeowners(
# do not sort
owners = [
"//:OWNERS",
"//packages/angular:OWNERS",
"//packages/labs:OWNERS",
"//packages/rollup:OWNERS",
"//examples:OWNERS.examples_nestjs",
Expand Down
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Don't edit it directly

* @gregmagolan @soldair @alexeagle
/packages/angular/** @alan-agius4 @alexeagle
/packages/labs/** @mrmeku @alexeagle
/packages/rollup/** @jbedard @alexeagle
/examples/nestjs/** @zachgrayio @zMotivat0r @rayman1104 @siberex @alexeagle
6 changes: 6 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ npm_install(
package_lock_json = "//packages/node-patches:package-lock.json",
)

npm_install(
name = "angular_deps",
package_json = "//packages/angular:package.json",
package_lock_json = "//packages/angular:package-lock.json",
)

# Install all Bazel dependencies needed for npm packages that supply Bazel rules
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
'scope-enum': [
2, 'always',
[
'angular',
'builtin',
'create',
'examples',
Expand Down
23 changes: 23 additions & 0 deletions packages/angular/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "codeowners", "pkg_npm")
load("//packages/typescript:index.bzl", "ts_project")

codeowners(
teams = ["@alan-agius4"],
)

ts_project(
tsc = "@angular_deps//typescript/bin:tsc",
deps = ["@angular_deps//:node_modules"],
)

pkg_npm(
name = "npm_package",
srcs = [
"README.md",
"package.json",
"src/builders/builders.json",
"src/builders/schema.d.ts",
"src/builders/schema.json",
],
deps = ["tsconfig"],
)
29 changes: 29 additions & 0 deletions packages/angular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Angular support for Bazel

This package is a replacement for parts of the deprecated @angular/bazel package previously maintained by the Angular team.

Currently, this only provides an Angular CLI Builder, which can execute Bazel when triggered by `ng build`, `ng test`, etc.
See https://angular.io/guide/cli-builder for more info about Builders.

This builder assumes you have already created Bazel configurations (WORKSPACE and BUILD files).
There is presently no tooling to generate these automatically that's supported by either Angular team or rules_nodejs maintainers.
See the `@bazel/create` package for a quickstart to creating a Bazel workspace, or look at examples in rules_nodejs.

To use it, you would just install this package (it doesn't hook into `ng add` because it has no schematics):

```sh
$ npm install --save-dev @bazel/angular
```

Then edit your `angular.json` to invoke Bazel. For example, to have `ng build` do `bazel build //:all` you would edit the `architect` block to have:

```json
"architect": {
"build": {
"builder": "@bazel/angular:build",
"options": {
"targetLabel": "//:all",
"bazelCommand": "build"
}
}
```
121 changes: 121 additions & 0 deletions packages/angular/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions packages/angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@bazel/angular",
"description": "Run Bazel under the Angular CLI",
"license": "Apache-2.0",
"version": "0.0.0-PLACEHOLDER",
"repository": {
"type": "git",
"url": "https://github.com/bazelbuild/rules_nodejs.git",
"directory": "packages/angular"
},
"bugs": {
"url": "https://github.com/bazelbuild/rules_nodejs/issues"
},
"keywords": [
"angular",
"bazel"
],
"builders": "./src/builders/builders.json",
"dependencies": {
"@bazel/bazelisk": "^1.4.0",
"@bazel/ibazel": "^0.13.1",
"@angular-devkit/architect": "^0.901.7"
},
"devDependencies": {
"@angular-devkit/core": "^9.1.7",
"@types/node": "^14.0.5",
"typescript": "^3.9.3"
}
}
9 changes: 9 additions & 0 deletions packages/angular/src/builders/builders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"builders": {
"build": {
"implementation": "./index",
"schema": "./schema.json",
"description": "Executes Bazel on a target."
}
}
}
37 changes: 37 additions & 0 deletions packages/angular/src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {BuilderContext, BuilderOutput, createBuilder} from '@angular-devkit/architect';
import {JsonObject} from '@angular-devkit/core';
import {getNativeBinary as bazeliskBin} from '@bazel/bazelisk/bazelisk';
import {getNativeBinary as ibazelBin} from '@bazel/ibazel';
import {spawn} from 'child_process';
import {Schema} from './schema';

async function _bazelBuilder(
options: JsonObject&Schema,
context: BuilderContext,
): Promise<BuilderOutput> {
const {bazelCommand, targetLabel, watch} = options;
const binary = watch ? ibazelBin() : bazeliskBin();
if (typeof binary !== 'string') {
// this happens if no binary is located for the current platform
return {success: false};
} else {
try {
const ps = spawn(binary, [bazelCommand, targetLabel], {stdio: 'inherit'});

function shutdown() {
ps.kill('SIGTERM');
}

process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
return new Promise(resolve => {
ps.on('close', e => resolve({success: e === 0}));
});
} catch (err) {
context.logger.error(err.message);
return {success: false};
}
}
}

export default createBuilder(_bazelBuilder);
34 changes: 34 additions & 0 deletions packages/angular/src/builders/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

/**
* Options for Bazel Builder
*/
export interface Schema {
/**
* Common commands supported by Bazel.
*/
bazelCommand: BazelCommand;
/**
* Target to be executed under Bazel.
*/
targetLabel: string;
/**
* If true, watch the filesystem using ibazel.
*/
watch?: boolean;
}

/**
* Common commands supported by Bazel.
*/
export enum BazelCommand {
Build = 'build',
Run = 'run',
Test = 'test',
}
31 changes: 31 additions & 0 deletions packages/angular/src/builders/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/schema",
"title": "Bazel builder schema",
"description": "Options for Bazel Builder",
"type": "object",
"properties": {
"targetLabel": {
"type": "string",
"description": "Target to be executed under Bazel."
},
"bazelCommand": {
"type": "string",
"description": "Common commands supported by Bazel.",
"enum": [
"run",
"build",
"test"
]
},
"watch": {
"type": "boolean",
"description": "If true, watch the filesystem using ibazel.",
"default": false
}
},
"additionalProperties": false,
"required": [
"targetLabel",
"bazelCommand"
]
}
8 changes: 8 additions & 0 deletions packages/angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2018",
"module": "CommonJS",
"types": ["node"]
}
}
7 changes: 7 additions & 0 deletions packages/angular/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '@bazel/bazelisk/bazelisk' {
function getNativeBinary(): Promise<number>|string;
}

declare module '@bazel/ibazel' {
function getNativeBinary(): Promise<number>|string;
}

0 comments on commit c87c83f

Please sign in to comment.