Repository navigation
Expand file tree
/
Copy pathHeftPhaseSession.ts
More file actions
91 lines (79 loc) · 3.3 KB
/
Copy pathHeftPhaseSession.ts
File metadata and controls
91 lines (79 loc) · 3.3 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { HeftTaskSession } from './HeftTaskSession';
import { HeftPluginHost } from './HeftPluginHost';
import type { ScopedLogger } from './logging/ScopedLogger';
import type { InternalHeftSession } from './InternalHeftSession';
import type { HeftPhase } from './HeftPhase';
import type { HeftTask } from './HeftTask';
import type { IHeftTaskPlugin } from './IHeftPlugin';
import type { LoggingManager } from './logging/LoggingManager';
export interface IHeftPhaseSessionOptions {
internalHeftSession: InternalHeftSession;
phase: HeftPhase;
}
export class HeftPhaseSession extends HeftPluginHost {
public readonly phaseLogger: ScopedLogger;
public readonly cleanLogger: ScopedLogger;
readonly #options: IHeftPhaseSessionOptions;
readonly #taskSessionsByTask: Map<HeftTask, HeftTaskSession> = new Map();
public constructor(options: IHeftPhaseSessionOptions) {
super();
this.#options = options;
const loggingManager: LoggingManager = options.internalHeftSession.loggingManager;
this.phaseLogger = loggingManager.requestScopedLogger(options.phase.phaseName);
this.cleanLogger = loggingManager.requestScopedLogger(`${options.phase.phaseName}:clean`);
}
/**
* Get a task session for the given task.
*/
public getSessionForTask(task: HeftTask): HeftTaskSession {
let taskSession: HeftTaskSession | undefined = this.#taskSessionsByTask.get(task);
if (!taskSession) {
taskSession = new HeftTaskSession({
...this.#options,
task,
pluginHost: this
});
this.#taskSessionsByTask.set(task, taskSession);
}
return taskSession;
}
/**
* Apply all task plugins specified by the phase.
*/
protected async applyPluginsInternalAsync(): Promise<void> {
const {
internalHeftSession: { heftConfiguration },
phase: { tasks }
} = this.#options;
// Load up all plugins concurrently
const loadPluginPromises: Promise<IHeftTaskPlugin<object | void>>[] = [];
for (const task of tasks) {
const taskSession: HeftTaskSession = this.getSessionForTask(task);
loadPluginPromises.push(task.getPluginAsync(taskSession.logger));
}
// Promise.all maintains the order of the input array
const plugins: IHeftTaskPlugin<object | void>[] = await Promise.all(loadPluginPromises);
// Iterate through and apply the plugins
let pluginIndex: number = 0;
for (const task of tasks) {
const taskSession: HeftTaskSession = this.getSessionForTask(task);
const taskPlugin: IHeftTaskPlugin<object | void> = plugins[pluginIndex++];
try {
taskPlugin.apply(taskSession, heftConfiguration, task.pluginOptions);
} catch (error) {
throw new Error(
`Error applying plugin ${JSON.stringify(task.pluginDefinition.pluginName)} from package ` +
`${JSON.stringify(task.pluginDefinition.pluginPackageName)}: ${error}`
);
}
}
// Do a second pass to apply the plugin access requests for each plugin
pluginIndex = 0;
for (const task of tasks) {
const taskPlugin: IHeftTaskPlugin<object | void> = plugins[pluginIndex++];
this.resolvePluginAccessRequests(taskPlugin, task.pluginDefinition);
}
}
}