Skip to content
Permalink
Browse files

feat(bazel): update the build to use the new architect api (#29720)

With this change the builder has been updated to use the latest architect API and make it compatable with the latest CLI

Fixes angular/angular-cli#14082

PR Close #29720
  • Loading branch information...
alan-agius4 authored and IgorMinar committed Apr 5, 2019
1 parent e381155 commit 902a53a4f617a6a600f49fe4f1f032c224d71fca
@@ -7,7 +7,7 @@
"e2e": "ng e2e --webdriver-update=false",
"lint": "ng lint",
"ng": "ng",
"postinstall": "webdriver-manager update --gecko=false --standalone=false $CI_CHROMEDRIVER_VERSION_ARG && yarn ivy-ngcc",
"postinstall": "webdriver-manager update --gecko=false --standalone=false $CI_CHROMEDRIVER_VERSION_ARG",
"start": "ng serve",
"pretest": "ng version",
"test": "ng test --progress=false --watch=false && yarn e2e --configuration=ci && yarn e2e --configuration=ci-production"
@@ -28,7 +28,7 @@
"zone.js": "file:../../node_modules/zone.js"
},
"devDependencies": {
"@angular-devkit/build-angular": "0.13.5",
"@angular-devkit/build-angular": "^0.800.0-beta.11",
"@angular/cli": "file:../../node_modules/@angular/cli",
"@angular/compiler-cli": "file:../../dist/packages-dist/compiler-cli",
"@angular/language-service": "file:../../dist/packages-dist/language-service",
@@ -28,7 +28,7 @@
"zone.js": "file:../../node_modules/zone.js"
},
"devDependencies": {
"@angular-devkit/build-angular": "0.13.5",
"@angular-devkit/build-angular": "^0.800.0-beta.11",
"@angular/cli": "file:../../node_modules/@angular/cli",
"@angular/compiler-cli": "file:../../dist/packages-dist/compiler-cli",
"@angular/language-service": "file:../../dist/packages-dist/language-service",
@@ -28,7 +28,7 @@
"zone.js": "file:../../node_modules/zone.js"
},
"devDependencies": {
"@angular-devkit/build-angular": "0.13.5",
"@angular-devkit/build-angular": "^0.800.0-beta.11",

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Apr 26, 2019

Member

could this be file:../../node_modules/@angular-devkit/build-angular (will require that we add @angular-devkit/build-angular to the root package.json (and the lock file) if the dep is not there yet.

"@angular/cli": "file:../../node_modules/@angular/cli",
"@angular/compiler-cli": "file:../../dist/packages-dist/compiler-cli",
"@angular/language-service": "file:../../dist/packages-dist/language-service",
@@ -110,7 +110,7 @@
"// 2": "devDependencies are not used under Bazel. Many can be removed after test.sh is deleted.",
"// 3": "when updating @bazel/bazel version you also need to update the RBE settings in .bazelrc (see https://github.com/angular/angular/pull/27935)",
"devDependencies": {
"@angular/cli": "^7.3.2",
"@angular/cli": "^8.0.0-beta.11",
"@bazel/bazel": "0.24.0",
"@bazel/buildifier": "^0.19.2",
"@bazel/ibazel": "~0.9.0",
@@ -26,11 +26,11 @@
}
},
"dependencies": {
"@angular-devkit/architect": "^0.13.4",
"@angular-devkit/core": "^7.0.4",
"@angular-devkit/schematics": "^7.3.0-rc.0",
"@angular-devkit/architect": "^0.800.0-beta.11",
"@angular-devkit/core": "^8.0.0-beta.11",
"@angular-devkit/schematics": "^8.0.0-beta.11",
"@microsoft/api-extractor": "^7.0.21",
"@schematics/angular": "^7.3.5",
"@schematics/angular": "^8.0.0-beta.11",
"@types/node": "6.0.84",
"semver": "^5.6.0",
"shelljs": "0.8.2",
@@ -8,10 +8,10 @@

/// <reference types='node'/>

import {Path, basename, dirname, getSystemPath, join} from '@angular-devkit/core';
import {resolve} from '@angular-devkit/core/node';
import {Path, dirname, getSystemPath, join, normalize} from '@angular-devkit/core';
import {Host} from '@angular-devkit/core/src/virtual-fs/host';
import {spawn} from 'child_process';
import * as path from 'path';

export type Executable = 'bazel' | 'ibazel';
export type Command = 'build' | 'test' | 'run' | 'coverage' | 'query';
@@ -20,9 +20,9 @@ export type Command = 'build' | 'test' | 'run' | 'coverage' | 'query';
* Spawn the Bazel process. Trap SINGINT to make sure Bazel process is killed.
*/
export function runBazel(
projectDir: Path, binary: Path, command: Command, workspaceTarget: string, flags: string[]) {
projectDir: Path, binary: string, command: Command, workspaceTarget: string, flags: string[]) {
return new Promise((resolve, reject) => {
const buildProcess = spawn(getSystemPath(binary), [command, workspaceTarget, ...flags], {
const buildProcess = spawn(process.argv[0], [binary, command, workspaceTarget, ...flags], {
cwd: getSystemPath(projectDir),
stdio: 'inherit',
shell: false,
@@ -39,7 +39,7 @@ export function runBazel(
if (code === 0) {
resolve();
} else {
reject(new Error(`${basename(binary)} failed with code ${code}.`));
reject(new Error(`${binary} failed with code ${code}.`));
}
});
});
@@ -49,11 +49,13 @@ export function runBazel(
* Resolves the path to `@bazel/bazel` or `@bazel/ibazel`.
*/
export function checkInstallation(name: Executable, projectDir: Path): string {
const packageName = `@bazel/${name}`;
const packageName = `@bazel/${name}/package.json`;
try {
return resolve(packageName, {
basedir: projectDir,
const bazelPath = require.resolve(packageName, {
paths: [getSystemPath(projectDir)],
});

return path.dirname(bazelPath);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error(
@@ -69,11 +71,11 @@ export function checkInstallation(name: Executable, projectDir: Path): string {
* Returns the absolute path to the template directory in `@angular/bazel`.
*/
export async function getTemplateDir(host: Host, root: Path): Promise<Path> {
const packageJson = resolve('@angular/bazel', {
basedir: root,
resolvePackageJson: true,
const packageJson = require.resolve('@angular/bazel/package.json', {
paths: [getSystemPath(root)],
});
const packageDir = dirname(packageJson as Path);

const packageDir = dirname(normalize(packageJson));
const templateDir = join(packageDir, 'src', 'builders', 'files');
if (!await host.isDirectory(templateDir).toPromise()) {
throw new Error('Could not find Bazel template directory in "@angular/bazel".');
@@ -1,7 +1,7 @@
{
"builders": {
"build": {
"class": "./index",
"implementation": "./index",
"schema": "./schema.json",
"description": "Executes Bazel on a target."
}
@@ -137,5 +137,6 @@ ts_web_test_suite(
deps = [
":rxjs_umd_modules",
":test_lib",
"@npm//karma-jasmine",
],
)
@@ -8,25 +8,24 @@
* @fileoverview Bazel builder
*/

import {BuildEvent, Builder, BuilderConfiguration, BuilderContext} from '@angular-devkit/architect';
import {Path} from '@angular-devkit/core';
import {Observable, from} from 'rxjs';
import {BuilderContext, BuilderOutput, createBuilder,} from '@angular-devkit/architect/src/index2';
import {JsonObject, normalize} from '@angular-devkit/core';
import {checkInstallation, copyBazelFiles, deleteBazelFiles, getTemplateDir, runBazel} from './bazel';
import {Schema} from './schema';
import {NodeJsSyncHost} from '@angular-devkit/core/node';

class BazelBuilder implements Builder<Schema> {
constructor(private context: BuilderContext) {}
async function _bazelBuilder(options: JsonObject & Schema, context: BuilderContext, ):
Promise<BuilderOutput> {
const root = normalize(context.workspaceRoot);
const {logger} = context;
const {bazelCommand, leaveBazelFilesOnDisk, targetLabel, watch} = options;
const executable = watch ? 'ibazel' : 'bazel';
const binary = checkInstallation(executable, root);

run(config: BuilderConfiguration<Partial<Schema>>): Observable<BuildEvent> {
const {host, logger, workspace} = this.context;
const root: Path = workspace.root;
const {bazelCommand, leaveBazelFilesOnDisk, targetLabel, watch} = config.options as Schema;
const executable = watch ? 'ibazel' : 'bazel';
const binary = checkInstallation(executable, root) as Path;

return from(Promise.resolve().then(async() => {
const host = new NodeJsSyncHost();
const templateDir = await getTemplateDir(host, root);
const bazelFiles = await copyBazelFiles(host, root, templateDir);

try {
const flags: string[] = [];
await runBazel(root, binary, bazelCommand, targetLabel, flags);
@@ -39,8 +38,6 @@ class BazelBuilder implements Builder<Schema> {
await deleteBazelFiles(host, bazelFiles); // this will never throw
}
}
}));
}
}
}

export default BazelBuilder;
export default createBuilder(_bazelBuilder);
Oops, something went wrong.

0 comments on commit 902a53a

Please sign in to comment.
You can’t perform that action at this time.