Repository navigation
Expand file tree
/
Copy pathInternalHeftSession.ts
More file actions
178 lines (149 loc) · 6.55 KB
/
Copy pathInternalHeftSession.ts
File metadata and controls
178 lines (149 loc) · 6.55 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
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Async, InternalError } from '@rushstack/node-core-library';
import { Constants } from '../utilities/Constants';
import { HeftLifecycle } from './HeftLifecycle';
import { HeftPhaseSession } from './HeftPhaseSession';
import { HeftPhase } from './HeftPhase';
import {
CoreConfigFiles,
type IHeftConfigurationJson,
type IHeftConfigurationJsonActionReference
} from '../utilities/CoreConfigFiles';
import type { MetricsCollector } from '../metrics/MetricsCollector';
import type { LoggingManager } from './logging/LoggingManager';
import type { HeftConfiguration } from '../configuration/HeftConfiguration';
import type { HeftPluginDefinitionBase } from '../configuration/HeftPluginDefinition';
import type { HeftTask } from './HeftTask';
import type { HeftParameterManager } from './HeftParameterManager';
import type { IHeftParsedCommandLine } from './HeftTaskSession';
export interface IInternalHeftSessionOptions {
heftConfiguration: HeftConfiguration;
loggingManager: LoggingManager;
metricsCollector: MetricsCollector;
debug: boolean;
}
function* getAllTasks(phases: Iterable<HeftPhase>): IterableIterator<HeftTask> {
for (const phase of phases) {
yield* phase.tasks;
}
}
export class InternalHeftSession {
readonly #phaseSessionsByPhase: Map<HeftPhase, HeftPhaseSession> = new Map();
readonly #heftConfigurationJson: IHeftConfigurationJson;
#actionReferencesByAlias: ReadonlyMap<string, IHeftConfigurationJsonActionReference> | undefined;
#lifecycle: HeftLifecycle | undefined;
#phases: Set<HeftPhase> | undefined;
#phasesByName: Map<string, HeftPhase> | undefined;
#parameterManager: HeftParameterManager | undefined;
public readonly heftConfiguration: HeftConfiguration;
public readonly loggingManager: LoggingManager;
public readonly metricsCollector: MetricsCollector;
public parsedCommandLine: IHeftParsedCommandLine | undefined;
public readonly debug: boolean;
private constructor(heftConfigurationJson: IHeftConfigurationJson, options: IInternalHeftSessionOptions) {
this.heftConfiguration = options.heftConfiguration;
this.loggingManager = options.loggingManager;
this.metricsCollector = options.metricsCollector;
this.debug = options.debug;
this.#heftConfigurationJson = heftConfigurationJson;
}
public static async initializeAsync(options: IInternalHeftSessionOptions): Promise<InternalHeftSession> {
// Initialize the rig. Must be done before the HeftConfiguration.rigConfig is used.
await options.heftConfiguration._checkForRigAsync();
const heftConfigurationJson: IHeftConfigurationJson =
await CoreConfigFiles.loadHeftConfigurationFileForProjectAsync(
options.heftConfiguration.globalTerminal,
options.heftConfiguration.buildFolderPath,
options.heftConfiguration.rigConfig
);
const internalHeftSession: InternalHeftSession = new InternalHeftSession(heftConfigurationJson, options);
// Initialize the lifecycle and the tasks. This will ensure that we throw an error if a plugin is improperly
// specified, or if the options provided to a plugin are invalid. We will avoid loading the actual plugins
// until they are needed.
await internalHeftSession.lifecycle.ensureInitializedAsync();
const tasks: Iterable<HeftTask> = getAllTasks(internalHeftSession.phases);
await Async.forEachAsync(
tasks,
async (task: HeftTask) => {
await task.ensureInitializedAsync();
},
{ concurrency: Constants.maxParallelism }
);
function* getAllPluginDefinitions(): IterableIterator<HeftPluginDefinitionBase> {
yield* internalHeftSession.lifecycle.pluginDefinitions;
for (const task of getAllTasks(internalHeftSession.phases)) {
yield task.pluginDefinition;
}
}
const loadedPluginPathsByName: Map<string, Set<string>> = new Map();
for (const { pluginName, entryPoint } of getAllPluginDefinitions()) {
let existingPluginPaths: Set<string> | undefined = loadedPluginPathsByName.get(pluginName);
if (!existingPluginPaths) {
existingPluginPaths = new Set();
loadedPluginPathsByName.set(pluginName, existingPluginPaths);
}
existingPluginPaths.add(entryPoint);
}
for (const [pluginName, pluginPaths] of loadedPluginPathsByName) {
if (pluginPaths.size > 1) {
throw new Error(
`Multiple plugins named ${JSON.stringify(pluginName)} were loaded from different paths: ` +
`${Array.from(pluginPaths, (x) => JSON.stringify(x)).join(', ')}. Plugins must have unique names.`
);
}
}
return internalHeftSession;
}
public get parameterManager(): HeftParameterManager {
if (!this.#parameterManager) {
throw new InternalError('A parameter manager for the session has not been provided.');
}
return this.#parameterManager;
}
public set parameterManager(value: HeftParameterManager) {
this.#parameterManager = value;
}
public get actionReferencesByAlias(): ReadonlyMap<string, IHeftConfigurationJsonActionReference> {
if (!this.#actionReferencesByAlias) {
this.#actionReferencesByAlias = new Map(
Object.entries(this.#heftConfigurationJson.aliasesByName || {})
);
}
return this.#actionReferencesByAlias;
}
public get lifecycle(): HeftLifecycle {
if (!this.#lifecycle) {
this.#lifecycle = new HeftLifecycle(this, this.#heftConfigurationJson.heftPlugins || []);
}
return this.#lifecycle;
}
public get phases(): ReadonlySet<HeftPhase> {
this.#ensurePhases();
return this.#phases!;
}
public get phasesByName(): ReadonlyMap<string, HeftPhase> {
this.#ensurePhases();
return this.#phasesByName!;
}
public getSessionForPhase(phase: HeftPhase): HeftPhaseSession {
let phaseSession: HeftPhaseSession | undefined = this.#phaseSessionsByPhase.get(phase);
if (!phaseSession) {
phaseSession = new HeftPhaseSession({ internalHeftSession: this, phase });
this.#phaseSessionsByPhase.set(phase, phaseSession);
}
return phaseSession;
}
#ensurePhases(): void {
if (!this.#phases || !this.#phasesByName) {
this.#phasesByName = new Map();
for (const [phaseName, phaseSpecifier] of Object.entries(
this.#heftConfigurationJson.phasesByName || {}
)) {
const phase: HeftPhase = new HeftPhase(this, phaseName, phaseSpecifier);
this.#phasesByName.set(phaseName, phase);
}
this.#phases = new Set(this.#phasesByName.values());
}
}
}