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

@ -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"
]
}