Skip to content

Commit beb5dce

Browse files
committed
fix(bazel): Support new e2e project layout
angular/angular-cli#13780 changes the project layout for the e2e application. It is no longer a separate project and the e2e directory is now located alongside the existing project. This commit updates Bazel scheamtics to support both old and new project layout.
1 parent 487d415 commit beb5dce

File tree

5 files changed

+97
-9
lines changed

5 files changed

+97
-9
lines changed

packages/bazel/src/builders/files/e2e/BUILD.bazel.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ts_library(
55
name = "e2e_lib",
66
testonly = 1,
77
srcs = glob(["src/**/*.ts"]),
8-
tsconfig = ":tsconfig.e2e.json",
8+
tsconfig = ":tsconfig.e2e.json" if len(glob(["tsconfig.e2e.json"])) else ":tsconfig.json",
99
deps = [
1010
"@npm//@types/jasmine",
1111
"@npm//@types/jasminewd2",

packages/bazel/src/schematics/ng-add/index.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
* @fileoverview Schematics for ng-new project that builds with Bazel.
99
*/
1010

11-
import {JsonAstObject, parseJsonAst, strings} from '@angular-devkit/core';
12-
import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, move, schematic, url} from '@angular-devkit/schematics';
11+
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
12+
import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, url} from '@angular-devkit/schematics';
1313
import {getWorkspacePath} from '@schematics/angular/utility/config';
1414
import {findPropertyInAstObject, insertPropertyInAstObjectInOrder} from '@schematics/angular/utility/json-utils';
1515
import {validateProjectName} from '@schematics/angular/utility/validation';
16+
1617
import {isJsonAstObject, removeKeyValueInAstObject, replacePropertyInAstObject} from '../utility/json-utils';
18+
import {findE2eArchitect} from '../utility/workspace-utils';
19+
1720
import {Schema} from './schema';
1821

22+
1923
/**
2024
* Packages that build under Bazel require additional dev dependencies. This
2125
* function adds those dependencies to "devDependencies" section in
@@ -99,6 +103,9 @@ function updateGitignore() {
99103
};
100104
}
101105

106+
/**
107+
* Change the architect in angular.json to use Bazel builder.
108+
*/
102109
function updateAngularJsonToUseBazelBuilder(options: Schema): Rule {
103110
return (host: Tree, context: SchematicContext) => {
104111
const {name} = options;
@@ -154,13 +161,10 @@ function updateAngularJsonToUseBazelBuilder(options: Schema): Rule {
154161
},
155162
indent);
156163

157-
const e2e = `${options.name}-e2e`;
158-
const e2eNode = findPropertyInAstObject(projects as JsonAstObject, e2e);
159-
if (e2eNode) {
160-
const architect =
161-
findPropertyInAstObject(e2eNode as JsonAstObject, 'architect') as JsonAstObject;
164+
const e2eArchitect = findE2eArchitect(workspaceJsonAst, name);
165+
if (e2eArchitect) {
162166
replacePropertyInAstObject(
163-
recorder, architect, 'e2e', {
167+
recorder, e2eArchitect, 'e2e', {
164168
builder: '@angular/bazel:build',
165169
options: {
166170
bazelCommand: 'test',

packages/bazel/src/schematics/utility/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ts_library(
66
name = "utility",
77
srcs = [
88
"json-utils.ts",
9+
"workspace-utils.ts",
910
],
1011
module_name = "@angular/bazel/src/schematics/utility",
1112
deps = [
@@ -21,6 +22,7 @@ ts_library(
2122
testonly = True,
2223
srcs = [
2324
"json-utils_spec.ts",
25+
"workspace-utils_spec.ts",
2426
],
2527
deps = [
2628
":utility",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {JsonAstNode, JsonAstObject} from '@angular-devkit/core';
10+
import {findPropertyInAstObject} from '@schematics/angular/utility/json-utils';
11+
import {isJsonAstObject} from './json-utils';
12+
13+
/**
14+
* Find the e2e architect node in the JSON ast.
15+
* The e2e application is relocated alongside the existing application.
16+
* This function supports looking up the e2e architect in both the new and old
17+
* layout.
18+
* See https://github.com/angular/angular-cli/pull/13780
19+
*/
20+
export function findE2eArchitect(ast: JsonAstObject, name: string): JsonAstObject|null {
21+
const projects = findPropertyInAstObject(ast, 'projects');
22+
if (!isJsonAstObject(projects)) {
23+
return null;
24+
}
25+
let architect: JsonAstNode|null;
26+
const e2e = findPropertyInAstObject(projects, `${name}-e2e`);
27+
if (isJsonAstObject(e2e)) {
28+
architect = findPropertyInAstObject(e2e, 'architect');
29+
} else {
30+
const project = findPropertyInAstObject(projects, name);
31+
if (!isJsonAstObject(project)) {
32+
return null;
33+
}
34+
architect = findPropertyInAstObject(project, 'architect');
35+
}
36+
if (!isJsonAstObject(architect)) {
37+
return null;
38+
}
39+
return architect;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
10+
import {isJsonAstObject} from './json-utils';
11+
import {findE2eArchitect} from './workspace-utils';
12+
13+
describe('Workspace utils', () => {
14+
describe('findE2eArchitect', () => {
15+
it('should find e2e architect in old project layout', () => {
16+
const workspace = {
17+
projects: {
18+
demo: {},
19+
'demo-e2e': {
20+
architect: {},
21+
},
22+
},
23+
};
24+
const ast = parseJsonAst(JSON.stringify(workspace));
25+
const architect = findE2eArchitect(ast as JsonAstObject, 'demo');
26+
expect(isJsonAstObject(architect)).toBe(true);
27+
});
28+
29+
it('should find e2e architect in new project layout', () => {
30+
const workspace = {
31+
projects: {
32+
demo: {
33+
architect: {},
34+
},
35+
},
36+
};
37+
const ast = parseJsonAst(JSON.stringify(workspace));
38+
const architect = findE2eArchitect(ast as JsonAstObject, 'demo');
39+
expect(isJsonAstObject(architect)).toBe(true);
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)