Skip to content

Commit 797d296

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 797d296

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-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,32 @@
1+
import {JsonAstNode, JsonAstObject} from '@angular-devkit/core';
2+
import {findPropertyInAstObject} from '@schematics/angular/utility/json-utils';
3+
import {isJsonAstObject} from './json-utils';
4+
5+
/**
6+
* Find the e2e architect node in the JSON ast.
7+
* The e2e application is relocated alongside the existing application.
8+
* This function supports looking up the e2e architect in both the new and old
9+
* layout.
10+
* See https://github.com/angular/angular-cli/pull/13780
11+
*/
12+
export function findE2eArchitect(ast: JsonAstObject, name: string): JsonAstObject|null {
13+
const projects = findPropertyInAstObject(ast, 'projects');
14+
if (!isJsonAstObject(projects)) {
15+
return null;
16+
}
17+
let architect: JsonAstNode|null;
18+
const e2e = findPropertyInAstObject(projects, `${name}-e2e`);
19+
if (isJsonAstObject(e2e)) {
20+
architect = findPropertyInAstObject(e2e, 'architect');
21+
} else {
22+
const project = findPropertyInAstObject(projects, name);
23+
if (!isJsonAstObject(project)) {
24+
return null;
25+
}
26+
architect = findPropertyInAstObject(project, 'architect');
27+
}
28+
if (!isJsonAstObject(architect)) {
29+
return null;
30+
}
31+
return architect;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
2+
import {isJsonAstObject} from './json-utils';
3+
import {findE2eArchitect} from './workspace-utils';
4+
5+
describe('Workspace utils', () => {
6+
describe('findE2eArchitect', () => {
7+
it('should find e2e architect in old project layout', () => {
8+
const workspace = {
9+
projects: {
10+
demo: {},
11+
'demo-e2e': {
12+
architect: {},
13+
},
14+
},
15+
};
16+
const ast = parseJsonAst(JSON.stringify(workspace));
17+
const architect = findE2eArchitect(ast as JsonAstObject, 'demo');
18+
expect(isJsonAstObject(architect)).toBe(true);
19+
});
20+
21+
it('should find e2e architect in new project layout', () => {
22+
const workspace = {
23+
projects: {
24+
demo: {
25+
architect: {},
26+
},
27+
},
28+
};
29+
const ast = parseJsonAst(JSON.stringify(workspace));
30+
const architect = findE2eArchitect(ast as JsonAstObject, 'demo');
31+
expect(isJsonAstObject(architect)).toBe(true);
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)