-
Notifications
You must be signed in to change notification settings - Fork 604
/
HeftActionRunner.ts
558 lines (488 loc) · 21.1 KB
/
HeftActionRunner.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { performance } from 'perf_hooks';
import { createInterface, type Interface as ReadlineInterface } from 'readline';
import os from 'os';
import {
AlreadyReportedError,
Colors,
ConsoleTerminalProvider,
InternalError,
type ITerminal,
type IPackageJson
} from '@rushstack/node-core-library';
import {
type IOperationExecutionOptions,
type IWatchLoopState,
Operation,
OperationExecutionManager,
OperationStatus,
WatchLoop
} from '@rushstack/operation-graph';
import type {
CommandLineFlagParameter,
CommandLineParameterProvider,
CommandLineStringListParameter
} from '@rushstack/ts-command-line';
import type { InternalHeftSession } from '../pluginFramework/InternalHeftSession';
import type { HeftConfiguration } from '../configuration/HeftConfiguration';
import type { LoggingManager } from '../pluginFramework/logging/LoggingManager';
import type { MetricsCollector } from '../metrics/MetricsCollector';
import { HeftParameterManager } from '../pluginFramework/HeftParameterManager';
import { TaskOperationRunner } from '../operations/runners/TaskOperationRunner';
import { PhaseOperationRunner } from '../operations/runners/PhaseOperationRunner';
import type { HeftPhase } from '../pluginFramework/HeftPhase';
import type { IHeftAction, IHeftActionOptions } from '../cli/actions/IHeftAction';
import type {
IHeftLifecycleCleanHookOptions,
IHeftLifecycleSession,
IHeftLifecycleToolFinishHookOptions,
IHeftLifecycleToolStartHookOptions
} from '../pluginFramework/HeftLifecycleSession';
import type { HeftLifecycle } from '../pluginFramework/HeftLifecycle';
import type { HeftTask } from '../pluginFramework/HeftTask';
import { deleteFilesAsync, type IDeleteOperation } from '../plugins/DeleteFilesPlugin';
import { Constants } from '../utilities/Constants';
export interface IHeftActionRunnerOptions extends IHeftActionOptions {
action: IHeftAction;
}
export function initializeHeft(
heftConfiguration: HeftConfiguration,
terminal: ITerminal,
isVerbose: boolean
): void {
// Ensure that verbose is enabled on the terminal if requested. terminalProvider.verboseEnabled
// should already be `true` if the `--debug` flag was provided. This is set in HeftCommandLineParser
if (heftConfiguration.terminalProvider instanceof ConsoleTerminalProvider) {
heftConfiguration.terminalProvider.verboseEnabled =
heftConfiguration.terminalProvider.verboseEnabled || isVerbose;
}
// Log some information about the execution
const projectPackageJson: IPackageJson = heftConfiguration.projectPackageJson;
terminal.writeVerboseLine(`Project: ${projectPackageJson.name}@${projectPackageJson.version}`);
terminal.writeVerboseLine(`Project build folder: ${heftConfiguration.buildFolderPath}`);
if (heftConfiguration.rigConfig.rigFound) {
terminal.writeVerboseLine(`Rig package: ${heftConfiguration.rigConfig.rigPackageName}`);
terminal.writeVerboseLine(`Rig profile: ${heftConfiguration.rigConfig.rigProfile}`);
}
terminal.writeVerboseLine(`Heft version: ${heftConfiguration.heftPackageJson.version}`);
terminal.writeVerboseLine(`Node version: ${process.version}`);
terminal.writeVerboseLine('');
}
let _cliAbortSignal: AbortSignal | undefined;
export function ensureCliAbortSignal(terminal: ITerminal): AbortSignal {
if (!_cliAbortSignal) {
// Set up the ability to terminate the build via Ctrl+C and have it exit gracefully if pressed once,
// less gracefully if pressed a second time.
const cliAbortController: AbortController = new AbortController();
_cliAbortSignal = cliAbortController.signal;
const cli: ReadlineInterface = createInterface(process.stdin, undefined, undefined, true);
let forceTerminate: boolean = false;
cli.on('SIGINT', () => {
cli.close();
if (forceTerminate) {
terminal.writeErrorLine(`Forcibly terminating.`);
process.exit(1);
} else {
terminal.writeLine(
Colors.yellow(Colors.bold(`Canceling... Press Ctrl+C again to forcibly terminate.`))
);
}
forceTerminate = true;
cliAbortController.abort();
});
}
return _cliAbortSignal;
}
export async function runWithLoggingAsync(
fn: () => Promise<OperationStatus>,
action: IHeftAction,
loggingManager: LoggingManager,
terminal: ITerminal,
metricsCollector: MetricsCollector,
abortSignal: AbortSignal,
throwOnFailure?: boolean
): Promise<OperationStatus> {
const startTime: number = performance.now();
loggingManager.resetScopedLoggerErrorsAndWarnings();
let result: OperationStatus = OperationStatus.Failure;
// Execute the action operations
let encounteredError: boolean = false;
try {
result = await fn();
if (result === OperationStatus.Failure) {
encounteredError = true;
}
} catch (e) {
encounteredError = true;
throw e;
} finally {
const warningStrings: string[] = loggingManager.getWarningStrings();
const errorStrings: string[] = loggingManager.getErrorStrings();
const wasAborted: boolean = abortSignal.aborted;
const encounteredWarnings: boolean = warningStrings.length > 0 || wasAborted;
encounteredError = encounteredError || errorStrings.length > 0;
await metricsCollector.recordAsync(
action.actionName,
{
encounteredError
},
action.getParameterStringMap()
);
const finishedLoggingWord: string = encounteredError ? 'Failed' : wasAborted ? 'Aborted' : 'Finished';
const duration: number = performance.now() - startTime;
const durationSeconds: number = Math.round(duration) / 1000;
const finishedLoggingLine: string = `-------------------- ${finishedLoggingWord} (${durationSeconds}s) --------------------`;
terminal.writeLine(
Colors.bold(
(encounteredError ? Colors.red : encounteredWarnings ? Colors.yellow : Colors.green)(
finishedLoggingLine
)
)
);
if (warningStrings.length > 0) {
terminal.writeWarningLine(
`Encountered ${warningStrings.length} warning${warningStrings.length === 1 ? '' : 's'}`
);
for (const warningString of warningStrings) {
terminal.writeWarningLine(` ${warningString}`);
}
}
if (errorStrings.length > 0) {
terminal.writeErrorLine(
`Encountered ${errorStrings.length} error${errorStrings.length === 1 ? '' : 's'}`
);
for (const errorString of errorStrings) {
terminal.writeErrorLine(` ${errorString}`);
}
}
}
if (encounteredError && throwOnFailure) {
throw new AlreadyReportedError();
}
return result;
}
export class HeftActionRunner {
private readonly _action: IHeftAction;
private readonly _terminal: ITerminal;
private readonly _internalHeftSession: InternalHeftSession;
private readonly _metricsCollector: MetricsCollector;
private readonly _loggingManager: LoggingManager;
private readonly _heftConfiguration: HeftConfiguration;
private _parameterManager: HeftParameterManager | undefined;
private readonly _parallelism: number;
public constructor(options: IHeftActionRunnerOptions) {
this._action = options.action;
this._internalHeftSession = options.internalHeftSession;
this._heftConfiguration = options.heftConfiguration;
this._loggingManager = options.loggingManager;
this._terminal = options.terminal;
this._metricsCollector = options.metricsCollector;
const numberOfCores: number = os.cpus().length;
// If an explicit parallelism number wasn't provided, then choose a sensible
// default.
if (os.platform() === 'win32') {
// On desktop Windows, some people have complained that their system becomes
// sluggish if Node is using all the CPU cores. Leave one thread for
// other operations. For CI environments, you can use the "max" argument to use all available cores.
this._parallelism = Math.max(numberOfCores - 1, 1);
} else {
// Unix-like operating systems have more balanced scheduling, so default
// to the number of CPU cores
this._parallelism = numberOfCores;
}
this._metricsCollector.setStartTime();
}
protected get parameterManager(): HeftParameterManager {
if (!this._parameterManager) {
throw new InternalError(`HeftActionRunner.defineParameters() has not been called.`);
}
return this._parameterManager;
}
public defineParameters(parameterProvider?: CommandLineParameterProvider | undefined): void {
if (!this._parameterManager) {
// Use the provided parameter provider if one was provided. This is used by the RunAction
// to allow for the Heft plugin parameters to be applied as scoped parameters.
parameterProvider = parameterProvider || this._action;
} else {
throw new InternalError(`HeftActionParameters.defineParameters() has already been called.`);
}
const verboseFlag: CommandLineFlagParameter = parameterProvider.defineFlagParameter({
parameterLongName: Constants.verboseParameterLongName,
parameterShortName: Constants.verboseParameterShortName,
description: 'If specified, log information useful for debugging.'
});
const productionFlag: CommandLineFlagParameter = parameterProvider.defineFlagParameter({
parameterLongName: Constants.productionParameterLongName,
description: 'If specified, run Heft in production mode.'
});
const localesParameter: CommandLineStringListParameter = parameterProvider.defineStringListParameter({
parameterLongName: Constants.localesParameterLongName,
argumentName: 'LOCALE',
description: 'Use the specified locale for this run, if applicable.'
});
let cleanFlagDescription: string =
'If specified, clean the outputs at the beginning of the lifecycle and before running each phase.';
if (this._action.watch) {
cleanFlagDescription =
`${cleanFlagDescription} Cleaning will only be performed once for the lifecycle and each phase, ` +
`and further incremental runs will not be cleaned for the duration of execution.`;
}
const cleanFlag: CommandLineFlagParameter = parameterProvider.defineFlagParameter({
parameterLongName: Constants.cleanParameterLongName,
description: cleanFlagDescription
});
const parameterManager: HeftParameterManager = new HeftParameterManager({
getIsDebug: () => this._internalHeftSession.debug,
getIsVerbose: () => verboseFlag.value,
getIsProduction: () => productionFlag.value,
getIsWatch: () => this._action.watch,
getLocales: () => localesParameter.values,
getIsClean: () => !!cleanFlag?.value
});
// Add all the lifecycle parameters for the action
for (const lifecyclePluginDefinition of this._internalHeftSession.lifecycle.pluginDefinitions) {
parameterManager.addPluginParameters(lifecyclePluginDefinition);
}
// Add all the task parameters for the action
for (const phase of this._action.selectedPhases) {
for (const task of phase.tasks) {
parameterManager.addPluginParameters(task.pluginDefinition);
}
}
// Finalize and apply to the CommandLineParameterProvider
parameterManager.finalizeParameters(parameterProvider);
this._parameterManager = parameterManager;
}
public async executeAsync(): Promise<void> {
const terminal: ITerminal = this._terminal;
// Set the parameter manager on the internal session, which is used to provide the selected
// parameters to plugins. Set this in onExecute() since we now know that this action is being
// executed, and the session should be populated with the executing parameters.
this._internalHeftSession.parameterManager = this.parameterManager;
initializeHeft(this._heftConfiguration, terminal, this.parameterManager.defaultParameters.verbose);
const operations: ReadonlySet<Operation> = this._generateOperations();
const executionManager: OperationExecutionManager = new OperationExecutionManager(operations);
const cliAbortSignal: AbortSignal = ensureCliAbortSignal(this._terminal);
try {
await _startLifecycleAsync(this._internalHeftSession);
if (this._action.watch) {
const watchLoop: WatchLoop = this._createWatchLoop(executionManager);
if (process.send) {
await watchLoop.runIPCAsync();
} else {
await watchLoop.runUntilAbortedAsync(cliAbortSignal, () => {
terminal.writeLine(Colors.bold('Waiting for changes. Press CTRL + C to exit...'));
terminal.writeLine('');
});
}
} else {
await this._executeOnceAsync(executionManager, cliAbortSignal);
}
} finally {
// Invoke this here both to ensure it always runs and that it does so after recordMetrics
// This is treated as a finalizer for any assets created in lifecycle plugins.
// It is the responsibility of the lifecycle plugin to ensure that finish gracefully handles
// aborted runs.
await _finishLifecycleAsync(this._internalHeftSession);
}
}
private _createWatchLoop(executionManager: OperationExecutionManager): WatchLoop {
const { _terminal: terminal } = this;
const watchLoop: WatchLoop = new WatchLoop({
onBeforeExecute: () => {
// Write an empty line to the terminal for separation between iterations. We've already iterated
// at this point, so log out that we're about to start a new run.
terminal.writeLine('');
terminal.writeLine(Colors.bold('Starting incremental build...'));
},
executeAsync: (state: IWatchLoopState): Promise<OperationStatus> => {
return this._executeOnceAsync(executionManager, state.abortSignal, state.requestRun);
},
onRequestRun: (requestor?: string) => {
terminal.writeLine(Colors.bold(`New run requested by ${requestor || 'unknown task'}`));
},
onAbort: () => {
terminal.writeLine(Colors.bold(`Cancelling incremental build...`));
}
});
return watchLoop;
}
private async _executeOnceAsync(
executionManager: OperationExecutionManager,
abortSignal: AbortSignal,
requestRun?: (requestor?: string) => void
): Promise<OperationStatus> {
// Execute the action operations
return await runWithLoggingAsync(
() => {
const operationExecutionManagerOptions: IOperationExecutionOptions = {
terminal: this._terminal,
parallelism: this._parallelism,
abortSignal,
requestRun
};
return executionManager.executeAsync(operationExecutionManagerOptions);
},
this._action,
this._loggingManager,
this._terminal,
this._metricsCollector,
abortSignal,
!requestRun
);
}
private _generateOperations(): Set<Operation> {
const { selectedPhases } = this._action;
const operations: Map<string, Operation> = new Map();
const internalHeftSession: InternalHeftSession = this._internalHeftSession;
let hasWarnedAboutSkippedPhases: boolean = false;
for (const phase of selectedPhases) {
// Warn if any dependencies are excluded from the list of selected phases
if (!hasWarnedAboutSkippedPhases) {
for (const dependencyPhase of phase.dependencyPhases) {
if (!selectedPhases.has(dependencyPhase)) {
// Only write once, and write with yellow to make it stand out without writing a warning to stderr
hasWarnedAboutSkippedPhases = true;
this._terminal.writeLine(
Colors.bold(
'The provided list of phases does not contain all phase dependencies. You may need to run the ' +
'excluded phases manually.'
)
);
break;
}
}
}
// Create operation for the phase start node
const phaseOperation: Operation = _getOrCreatePhaseOperation(internalHeftSession, phase, operations);
// Create operations for each task
for (const task of phase.tasks) {
const taskOperation: Operation = _getOrCreateTaskOperation(internalHeftSession, task, operations);
// Set the phase operation as a dependency of the task operation to ensure the phase operation runs first
taskOperation.addDependency(phaseOperation);
// Set all dependency tasks as dependencies of the task operation
for (const dependencyTask of task.dependencyTasks) {
taskOperation.addDependency(
_getOrCreateTaskOperation(internalHeftSession, dependencyTask, operations)
);
}
// Set all tasks in a in a phase as dependencies of the consuming phase
for (const consumingPhase of phase.consumingPhases) {
if (this._action.selectedPhases.has(consumingPhase)) {
// Set all tasks in a dependency phase as dependencies of the consuming phase to ensure the dependency
// tasks run first
const consumingPhaseOperation: Operation = _getOrCreatePhaseOperation(
internalHeftSession,
consumingPhase,
operations
);
consumingPhaseOperation.addDependency(taskOperation);
// This is purely to simplify the reported graph for phase circularities
consumingPhaseOperation.addDependency(phaseOperation);
}
}
}
}
return new Set(operations.values());
}
}
function _getOrCreatePhaseOperation(
this: void,
internalHeftSession: InternalHeftSession,
phase: HeftPhase,
operations: Map<string, Operation>
): Operation {
const key: string = phase.phaseName;
let operation: Operation | undefined = operations.get(key);
if (!operation) {
// Only create the operation. Dependencies are hooked up separately
operation = new Operation({
groupName: phase.phaseName,
runner: new PhaseOperationRunner({ phase, internalHeftSession })
});
operations.set(key, operation);
}
return operation;
}
function _getOrCreateTaskOperation(
this: void,
internalHeftSession: InternalHeftSession,
task: HeftTask,
operations: Map<string, Operation>
): Operation {
const key: string = `${task.parentPhase.phaseName}.${task.taskName}`;
let operation: Operation | undefined = operations.get(key);
if (!operation) {
operation = new Operation({
groupName: task.parentPhase.phaseName,
runner: new TaskOperationRunner({
internalHeftSession,
task
})
});
operations.set(key, operation);
}
return operation;
}
async function _startLifecycleAsync(this: void, internalHeftSession: InternalHeftSession): Promise<void> {
const { clean } = internalHeftSession.parameterManager.defaultParameters;
// Load and apply the lifecycle plugins
const lifecycle: HeftLifecycle = internalHeftSession.lifecycle;
const { lifecycleLogger } = lifecycle;
await lifecycle.applyPluginsAsync(lifecycleLogger.terminal);
if (lifecycleLogger.hasErrors) {
throw new AlreadyReportedError();
}
if (clean) {
const startTime: number = performance.now();
lifecycleLogger.terminal.writeVerboseLine('Starting clean');
// Grab the additional clean operations from the phase
const deleteOperations: IDeleteOperation[] = [];
// Delete all temp folders for tasks by default
for (const pluginDefinition of lifecycle.pluginDefinitions) {
const lifecycleSession: IHeftLifecycleSession = await lifecycle.getSessionForPluginDefinitionAsync(
pluginDefinition
);
deleteOperations.push({ sourcePath: lifecycleSession.tempFolderPath });
}
// Create the options and provide a utility method to obtain paths to delete
const cleanHookOptions: IHeftLifecycleCleanHookOptions = {
addDeleteOperations: (...deleteOperationsToAdd: IDeleteOperation[]) =>
deleteOperations.push(...deleteOperationsToAdd)
};
// Run the plugin clean hook
if (lifecycle.hooks.clean.isUsed()) {
try {
await lifecycle.hooks.clean.promise(cleanHookOptions);
} catch (e: unknown) {
// Log out using the clean logger, and return an error status
if (!(e instanceof AlreadyReportedError)) {
lifecycleLogger.emitError(e as Error);
}
throw new AlreadyReportedError();
}
}
// Delete the files if any were specified
if (deleteOperations.length) {
const rootFolderPath: string = internalHeftSession.heftConfiguration.buildFolderPath;
await deleteFilesAsync(rootFolderPath, deleteOperations, lifecycleLogger.terminal);
}
lifecycleLogger.terminal.writeVerboseLine(`Finished clean (${performance.now() - startTime}ms)`);
if (lifecycleLogger.hasErrors) {
throw new AlreadyReportedError();
}
}
// Run the start hook
if (lifecycle.hooks.toolStart.isUsed()) {
const lifecycleToolStartHookOptions: IHeftLifecycleToolStartHookOptions = {};
await lifecycle.hooks.toolStart.promise(lifecycleToolStartHookOptions);
if (lifecycleLogger.hasErrors) {
throw new AlreadyReportedError();
}
}
}
async function _finishLifecycleAsync(internalHeftSession: InternalHeftSession): Promise<void> {
const lifecycleToolFinishHookOptions: IHeftLifecycleToolFinishHookOptions = {};
await internalHeftSession.lifecycle.hooks.toolFinish.promise(lifecycleToolFinishHookOptions);
}