build(bazel): Add Bazel builders (#27141)

PR Close #27141
This commit is contained in:
Keen Yee Liau 2018-11-16 11:36:17 -08:00 committed by Miško Hevery
parent 026b7e34b3
commit a72250bace
11 changed files with 199 additions and 1 deletions

View File

@ -33,6 +33,7 @@
},
"// 1": "dependencies are used locally and by bazel",
"dependencies": {
"@angular-devkit/architect": "^0.10.6",
"@angular-devkit/core": "^7.0.4",
"@angular-devkit/schematics": "^7.0.4",
"@bazel/typescript": "0.21.0",

View File

@ -14,6 +14,7 @@ npm_package(
],
tags = ["release-with-framework"],
deps = [
"//packages/bazel/src/builders",
"//packages/bazel/src/ng_package:lib",
"//packages/bazel/src/ngc-wrapped:ngc_lib",
"//packages/bazel/src/protractor/utils",

View File

@ -12,6 +12,7 @@
},
"typings": "./src/ngc-wrapped/index.d.ts",
"dependencies": {
"@angular-devkit/architect": "^0.10.6",
"@angular-devkit/core": "^7.0.4",
"@angular-devkit/schematics": "^7.0.4",
"@bazel/typescript": "^0.21.0",
@ -28,6 +29,7 @@
"type": "git",
"url": "https://github.com/angular/angular.git"
},
"builders": "./src/builders/builders.json",
"schematics": "./src/schematics/collection.json",
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"

View File

@ -0,0 +1,30 @@
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package_assets",
srcs = [
"builders.json",
],
visibility = ["//packages/bazel:__subpackages__"],
)
load("//tools:defaults.bzl", "ts_library")
ts_library(
name = "builders",
srcs = [
"bazel.ts",
"index.ts",
"schema.d.ts",
],
data = [
"schema.json",
],
deps = [
"@ngdeps//@angular-devkit/architect",
"@ngdeps//@angular-devkit/core",
"@ngdeps//@types/node",
"@rxjs",
"@rxjs//operators",
],
)

View File

@ -0,0 +1,43 @@
/**
* @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
*/
/// <reference types='node'/>
import {spawn, spawnSync} from 'child_process';
import {Observable, Subject} from 'rxjs';
export type Executable = 'bazel' | 'ibazel';
export type Command = 'build' | 'test' | 'run' | 'coverage' | 'query';
export function runBazel(
projectDir: string, executable: Executable, command: Command, workspaceTarget: string,
flags: string[]): Observable<void> {
const doneSubject = new Subject<void>();
const buildProcess = spawn(executable, [command, workspaceTarget, ...flags], {
cwd: projectDir,
stdio: 'inherit',
shell: false,
});
buildProcess.once('close', (code: number) => {
if (code === 0) {
doneSubject.next();
} else {
doneSubject.error(`${executable} failed with code ${code}.`);
}
});
return doneSubject.asObservable();
}
export function checkInstallation(executable: Executable, projectDir: string) {
const child = spawnSync(executable, ['version'], {
cwd: projectDir,
shell: false,
});
return child.status === 0;
}

View File

@ -0,0 +1,9 @@
{
"builders": {
"build": {
"class": "./index#Builder",
"schema": "./schema.json",
"description": "Executes Bazel on a target."
}
}
}

View File

@ -0,0 +1,40 @@
/**
* @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
*
* @fileoverview Bazel bundle builder
*/
import {BuildEvent, Builder as BuilderInterface, BuilderConfiguration, BuilderContext} from '@angular-devkit/architect';
import {getSystemPath, resolve} from '@angular-devkit/core';
import {Observable, of } from 'rxjs';
import {catchError, map, tap} from 'rxjs/operators';
import {checkInstallation, runBazel} from './bazel';
import {Schema} from './schema';
export class Builder implements BuilderInterface<Schema> {
constructor(private context: BuilderContext) {}
run(builderConfig: BuilderConfiguration<Partial<Schema>>): Observable<BuildEvent> {
const projectRoot = getSystemPath(resolve(this.context.workspace.root, builderConfig.root));
const targetLabel = builderConfig.options.targetLabel;
const executable = builderConfig.options.watch ? 'ibazel' : 'bazel';
if (!checkInstallation(executable, projectRoot)) {
throw new Error(
`Could not run ${executable}. Please make sure that the ` +
`"${executable}" command is available in the $PATH.`);
}
// TODO: Support passing flags.
return runBazel(
projectRoot, executable, builderConfig.options.bazelCommand !, targetLabel !,
[] /* flags */)
.pipe(map(() => ({success: true})), catchError(() => of ({success: false})), );
}
}

24
packages/bazel/src/builders/schema.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
// THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
// CORRESPONDING JSON SCHEMA FILE, THEN RUN devkit-admin build (or bazel build ...).
// tslint:disable:no-global-tslint-disable
// tslint:disable
/**
* Options for Bazel Builder
*/
export interface Schema {
bazelCommand: BazelCommand;
/**
* Target to be executed under Bazel.
*/
targetLabel: string;
watch?: boolean;
}
export enum BazelCommand {
Build = 'build',
Run = 'run',
Test = 'test',
}

View File

@ -0,0 +1,29 @@
{
"$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",
"enum": [
"run",
"build",
"test"
]
},
"watch": {
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
"required": [
"targetLabel",
"bazelCommand"
]
}

View File

@ -1693,7 +1693,7 @@ karma-sourcemap-loader@0.3.7:
dependencies:
graceful-fs "^4.1.2"
"karma@github:alexeagle/karma#fa1a84ac881485b5657cb669e9b4e5da77b79f0a":
karma@alexeagle/karma#fa1a84ac881485b5657cb669e9b4e5da77b79f0a:
version "1.7.1"
resolved "https://codeload.github.com/alexeagle/karma/tar.gz/fa1a84ac881485b5657cb669e9b4e5da77b79f0a"
dependencies:

View File

@ -2,6 +2,14 @@
# yarn lockfile v1
"@angular-devkit/architect@^0.10.6":
version "0.10.6"
resolved "https://registry.yarnpkg.com/@angular-devkit/architect/-/architect-0.10.6.tgz#7007e7591be21eeb478951106c84c83802ca21a4"
integrity sha512-IygpkXNn946vVUFFWKWEDxRqRy888vOAUWcmkZzqPEBYkuwWt7WnLfe8Sjw4fH/+HLWEMS8RXbdSTHiiaP9qOg==
dependencies:
"@angular-devkit/core" "7.0.6"
rxjs "6.3.3"
"@angular-devkit/core@7.0.5", "@angular-devkit/core@^7.0.4":
version "7.0.5"
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-7.0.5.tgz#60866c0a2367cea44b436a359ac9be99a138e180"
@ -13,6 +21,17 @@
rxjs "6.3.3"
source-map "0.7.3"
"@angular-devkit/core@7.0.6":
version "7.0.6"
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-7.0.6.tgz#26c4cd4d271e8cd03f6e50b4ec30cbc606f3346e"
integrity sha512-RPSXUtLrpYDTqAEL0rCyDKxES76EomsPBvUUZTD6UkE2pihoh9ZIxkzhzlE+HU/xdqm28+smQYFhvvEAXFWwSQ==
dependencies:
ajv "6.5.3"
chokidar "2.0.4"
fast-json-stable-stringify "2.0.0"
rxjs "6.3.3"
source-map "0.7.3"
"@angular-devkit/schematics@7.0.5", "@angular-devkit/schematics@^7.0.4":
version "7.0.5"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-7.0.5.tgz#527bf0af5352172e92c5473a33bc07af44c77796"