-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathbuilder-harness.ts
487 lines (417 loc) · 14.6 KB
/
builder-harness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/**
* @license
* Copyright Google LLC 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
*/
import {
BuilderContext,
BuilderHandlerFn,
BuilderInfo,
BuilderOutput,
BuilderOutputLike,
BuilderProgressReport,
BuilderRun,
ScheduleOptions,
Target,
fromAsyncIterable,
isBuilderOutput,
} from '@angular-devkit/architect';
import { WorkspaceHost } from '@angular-devkit/architect/node';
import { TestProjectHost } from '@angular-devkit/architect/testing';
import { analytics, getSystemPath, join, json, logging, normalize } from '@angular-devkit/core';
import { Observable, Subject, from as observableFrom, of as observableOf } from 'rxjs';
import { catchError, finalize, first, map, mergeMap, shareReplay } from 'rxjs/operators';
import { BuilderWatcherFactory, WatcherNotifier } from './file-watching';
export interface BuilderHarnessExecutionResult<T extends BuilderOutput = BuilderOutput> {
result?: T;
error?: Error;
logs: readonly logging.LogEntry[];
}
export interface BuilderHarnessExecutionOptions {
configuration: string;
outputLogsOnFailure: boolean;
outputLogsOnException: boolean;
useNativeFileWatching: boolean;
}
/**
* The default set of fields provided to all builders executed via the BuilderHarness.
* `root` and `sourceRoot` are required for most Angular builders to function.
* `cli.cache.enabled` set to false provides improved test isolation guarantees by disabling
* the Webpack caching.
*/
const DEFAULT_PROJECT_METADATA = {
root: '.',
sourceRoot: 'src',
cli: {
cache: {
enabled: false,
},
},
};
export class BuilderHarness<T> {
private readonly builderInfo: BuilderInfo;
private schemaRegistry = new json.schema.CoreSchemaRegistry();
private projectName = 'test';
private projectMetadata: Record<string, unknown> = DEFAULT_PROJECT_METADATA;
private targetName?: string;
private options = new Map<string | null, T>();
private builderTargets = new Map<
string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ handler: BuilderHandlerFn<any>; info: BuilderInfo; options: json.JsonObject }
>();
private watcherNotifier?: WatcherNotifier;
constructor(
private readonly builderHandler: BuilderHandlerFn<T & json.JsonObject>,
private readonly host: TestProjectHost,
builderInfo?: Partial<BuilderInfo>,
) {
// Generate default pseudo builder info for test purposes
this.builderInfo = {
builderName: builderHandler.name,
description: '',
optionSchema: true,
...builderInfo,
};
this.schemaRegistry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
}
useProject(name: string, metadata: Record<string, unknown> = {}): this {
if (!name) {
throw new Error('Project name cannot be an empty string.');
}
this.projectName = name;
this.projectMetadata = metadata;
return this;
}
useTarget(name: string, baseOptions: T): this {
if (!name) {
throw new Error('Target name cannot be an empty string.');
}
this.targetName = name;
this.options.set(null, baseOptions);
return this;
}
withConfiguration(configuration: string, options: T): this {
this.options.set(configuration, options);
return this;
}
withBuilderTarget<O extends object>(
target: string,
handler: BuilderHandlerFn<O & json.JsonObject>,
options?: O,
info?: Partial<BuilderInfo>,
): this {
this.builderTargets.set(target, {
handler,
options: options || {},
info: { builderName: handler.name, description: '', optionSchema: true, ...info },
});
return this;
}
execute(
options: Partial<BuilderHarnessExecutionOptions> = {},
): Observable<BuilderHarnessExecutionResult> {
const {
configuration,
outputLogsOnException = true,
outputLogsOnFailure = true,
useNativeFileWatching = false,
} = options;
const targetOptions = {
...this.options.get(null),
...((configuration && this.options.get(configuration)) ?? {}),
};
if (!useNativeFileWatching) {
if (this.watcherNotifier) {
throw new Error('Only one harness execution at a time is supported.');
}
this.watcherNotifier = new WatcherNotifier();
}
const contextHost: ContextHost = {
findBuilderByTarget: async (project, target) => {
this.validateProjectName(project);
if (target === this.targetName) {
return {
info: this.builderInfo,
handler: this.builderHandler as BuilderHandlerFn<json.JsonObject>,
};
}
const builderTarget = this.builderTargets.get(target);
if (builderTarget) {
return { info: builderTarget.info, handler: builderTarget.handler };
}
throw new Error('Project target does not exist.');
},
async getBuilderName(project, target) {
return (await this.findBuilderByTarget(project, target)).info.builderName;
},
getMetadata: async (project) => {
this.validateProjectName(project);
return this.projectMetadata as json.JsonObject;
},
getOptions: async (project, target, configuration) => {
this.validateProjectName(project);
if (target === this.targetName) {
return (this.options.get(configuration ?? null) ?? {}) as json.JsonObject;
} else if (configuration !== undefined) {
// Harness builder targets currently do not support configurations
return {};
} else {
return (this.builderTargets.get(target)?.options || {}) as json.JsonObject;
}
},
hasTarget: async (project, target) => {
this.validateProjectName(project);
return this.targetName === target || this.builderTargets.has(target);
},
getDefaultConfigurationName: async (_project, _target) => {
return undefined;
},
validate: async (options, builderName) => {
let schema;
if (builderName === this.builderInfo.builderName) {
schema = this.builderInfo.optionSchema;
} else {
for (const [, value] of this.builderTargets) {
if (value.info.builderName === builderName) {
schema = value.info.optionSchema;
break;
}
}
}
const validator = await this.schemaRegistry.compile(schema ?? true).toPromise();
const { data } = await validator(options).toPromise();
return data as json.JsonObject;
},
};
const context = new HarnessBuilderContext(
this.builderInfo,
getSystemPath(this.host.root()),
contextHost,
useNativeFileWatching ? undefined : this.watcherNotifier,
);
if (this.targetName !== undefined) {
context.target = {
project: this.projectName,
target: this.targetName,
configuration: configuration as string,
};
}
const logs: logging.LogEntry[] = [];
context.logger.subscribe((e) => logs.push(e));
return this.schemaRegistry.compile(this.builderInfo.optionSchema).pipe(
mergeMap((validator) => validator(targetOptions)),
map((validationResult) => validationResult.data),
mergeMap((data) =>
convertBuilderOutputToObservable(this.builderHandler(data as T & json.JsonObject, context)),
),
map((buildResult) => ({ result: buildResult, error: undefined })),
catchError((error) => {
if (outputLogsOnException) {
// eslint-disable-next-line no-console
console.error(logs.map((entry) => entry.message).join('\n'));
// eslint-disable-next-line no-console
console.error(error);
}
return observableOf({ result: undefined, error });
}),
map(({ result, error }) => {
if (outputLogsOnFailure && result?.success === false && logs.length > 0) {
// eslint-disable-next-line no-console
console.error(logs.map((entry) => entry.message).join('\n'));
}
// Capture current logs and clear for next
const currentLogs = logs.slice();
logs.length = 0;
return { result, error, logs: currentLogs };
}),
finalize(() => {
this.watcherNotifier = undefined;
for (const teardown of context.teardowns) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
teardown();
}
}),
);
}
async executeOnce(
options?: Partial<BuilderHarnessExecutionOptions>,
): Promise<BuilderHarnessExecutionResult> {
// Return the first result
return this.execute(options).pipe(first()).toPromise();
}
async appendToFile(path: string, content: string): Promise<void> {
await this.writeFile(path, this.readFile(path).concat(content));
}
async writeFile(path: string, content: string | Buffer): Promise<void> {
this.host
.scopedSync()
.write(normalize(path), typeof content === 'string' ? Buffer.from(content) : content);
this.watcherNotifier?.notify([
{ path: getSystemPath(join(this.host.root(), path)), type: 'modified' },
]);
}
async writeFiles(files: Record<string, string | Buffer>): Promise<void> {
const watchEvents = this.watcherNotifier
? ([] as { path: string; type: 'modified' | 'deleted' }[])
: undefined;
for (const [path, content] of Object.entries(files)) {
this.host
.scopedSync()
.write(normalize(path), typeof content === 'string' ? Buffer.from(content) : content);
watchEvents?.push({ path: getSystemPath(join(this.host.root(), path)), type: 'modified' });
}
if (watchEvents) {
this.watcherNotifier?.notify(watchEvents);
}
}
async removeFile(path: string): Promise<void> {
this.host.scopedSync().delete(normalize(path));
this.watcherNotifier?.notify([
{ path: getSystemPath(join(this.host.root(), path)), type: 'deleted' },
]);
}
async modifyFile(
path: string,
modifier: (content: string) => string | Promise<string>,
): Promise<void> {
const content = this.readFile(path);
await this.writeFile(path, await modifier(content));
this.watcherNotifier?.notify([
{ path: getSystemPath(join(this.host.root(), path)), type: 'modified' },
]);
}
hasFile(path: string): boolean {
return this.host.scopedSync().exists(normalize(path));
}
hasFileMatch(directory: string, pattern: RegExp): boolean {
return this.host
.scopedSync()
.list(normalize(directory))
.some((name) => pattern.test(name));
}
readFile(path: string): string {
const content = this.host.scopedSync().read(normalize(path));
return Buffer.from(content).toString('utf8');
}
private validateProjectName(name: string): void {
if (name !== this.projectName) {
throw new Error(`Project "${name}" does not exist.`);
}
}
}
interface ContextHost extends WorkspaceHost {
findBuilderByTarget(
project: string,
target: string,
): Promise<{ info: BuilderInfo; handler: BuilderHandlerFn<json.JsonObject> }>;
validate(options: json.JsonObject, builderName: string): Promise<json.JsonObject>;
}
class HarnessBuilderContext implements BuilderContext {
id = Math.trunc(Math.random() * 1000000);
logger = new logging.Logger(`builder-harness-${this.id}`);
workspaceRoot: string;
currentDirectory: string;
target?: Target;
teardowns: (() => Promise<void> | void)[] = [];
constructor(
public builder: BuilderInfo,
basePath: string,
private readonly contextHost: ContextHost,
public readonly watcherFactory: BuilderWatcherFactory | undefined,
) {
this.workspaceRoot = this.currentDirectory = basePath;
}
get analytics(): analytics.Analytics {
// Can be undefined even though interface does not allow it
return undefined as unknown as analytics.Analytics;
}
addTeardown(teardown: () => Promise<void> | void): void {
this.teardowns.push(teardown);
}
async getBuilderNameForTarget(target: Target): Promise<string> {
return this.contextHost.getBuilderName(target.project, target.target);
}
async getProjectMetadata(targetOrName: Target | string): Promise<json.JsonObject> {
const project = typeof targetOrName === 'string' ? targetOrName : targetOrName.project;
return this.contextHost.getMetadata(project);
}
async getTargetOptions(target: Target): Promise<json.JsonObject> {
return this.contextHost.getOptions(target.project, target.target, target.configuration);
}
// Unused by builders in this package
async scheduleBuilder(
builderName: string,
options?: json.JsonObject,
scheduleOptions?: ScheduleOptions,
): Promise<BuilderRun> {
throw new Error('Not Implemented.');
}
async scheduleTarget(
target: Target,
overrides?: json.JsonObject,
scheduleOptions?: ScheduleOptions,
): Promise<BuilderRun> {
const { info, handler } = await this.contextHost.findBuilderByTarget(
target.project,
target.target,
);
const targetOptions = await this.validateOptions(
{
...(await this.getTargetOptions(target)),
...overrides,
},
info.builderName,
);
const context = new HarnessBuilderContext(
info,
this.workspaceRoot,
this.contextHost,
this.watcherFactory,
);
context.target = target;
context.logger = scheduleOptions?.logger || this.logger.createChild('');
const progressSubject = new Subject<BuilderProgressReport>();
const output = convertBuilderOutputToObservable(handler(targetOptions, context));
const run: BuilderRun = {
id: context.id,
info,
progress: progressSubject.asObservable(),
async stop() {
for (const teardown of context.teardowns) {
await teardown();
}
progressSubject.complete();
},
output: output.pipe(shareReplay()),
get result() {
return this.output.pipe(first()).toPromise();
},
};
return run;
}
async validateOptions<T extends json.JsonObject = json.JsonObject>(
options: json.JsonObject,
builderName: string,
): Promise<T> {
return this.contextHost.validate(options, builderName) as unknown as T;
}
// Unused report methods
reportRunning(): void {}
reportStatus(): void {}
reportProgress(): void {}
}
function isAsyncIterable<T>(obj: unknown): obj is AsyncIterable<T> {
return !!obj && typeof (obj as AsyncIterable<T>)[Symbol.asyncIterator] === 'function';
}
function convertBuilderOutputToObservable(output: BuilderOutputLike): Observable<BuilderOutput> {
if (isBuilderOutput(output)) {
return observableOf(output);
} else if (isAsyncIterable(output)) {
return fromAsyncIterable(output);
} else {
return observableFrom(output);
}
}