@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
107 lines • 4.99 kB
JavaScript
// 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 } from '../utilities/CoreConfigFiles';
function* getAllTasks(phases) {
for (const phase of phases) {
yield* phase.tasks;
}
}
export class InternalHeftSession {
constructor(heftConfigurationJson, options) {
this._phaseSessionsByPhase = new Map();
this.heftConfiguration = options.heftConfiguration;
this.loggingManager = options.loggingManager;
this.metricsCollector = options.metricsCollector;
this.debug = options.debug;
this._heftConfigurationJson = heftConfigurationJson;
}
static async initializeAsync(options) {
// Initialize the rig. Must be done before the HeftConfiguration.rigConfig is used.
await options.heftConfiguration._checkForRigAsync();
const heftConfigurationJson = await CoreConfigFiles.loadHeftConfigurationFileForProjectAsync(options.heftConfiguration.globalTerminal, options.heftConfiguration.buildFolderPath, options.heftConfiguration.rigConfig);
const 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 = getAllTasks(internalHeftSession.phases);
await Async.forEachAsync(tasks, async (task) => {
await task.ensureInitializedAsync();
}, { concurrency: Constants.maxParallelism });
function* getAllPluginDefinitions() {
yield* internalHeftSession.lifecycle.pluginDefinitions;
for (const task of getAllTasks(internalHeftSession.phases)) {
yield task.pluginDefinition;
}
}
const loadedPluginPathsByName = new Map();
for (const { pluginName, entryPoint } of getAllPluginDefinitions()) {
let existingPluginPaths = 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;
}
get parameterManager() {
if (!this._parameterManager) {
throw new InternalError('A parameter manager for the session has not been provided.');
}
return this._parameterManager;
}
set parameterManager(value) {
this._parameterManager = value;
}
get actionReferencesByAlias() {
if (!this._actionReferencesByAlias) {
this._actionReferencesByAlias = new Map(Object.entries(this._heftConfigurationJson.aliasesByName || {}));
}
return this._actionReferencesByAlias;
}
get lifecycle() {
if (!this._lifecycle) {
this._lifecycle = new HeftLifecycle(this, this._heftConfigurationJson.heftPlugins || []);
}
return this._lifecycle;
}
get phases() {
this._ensurePhases();
return this._phases;
}
get phasesByName() {
this._ensurePhases();
return this._phasesByName;
}
getSessionForPhase(phase) {
let phaseSession = this._phaseSessionsByPhase.get(phase);
if (!phaseSession) {
phaseSession = new HeftPhaseSession({ internalHeftSession: this, phase });
this._phaseSessionsByPhase.set(phase, phaseSession);
}
return phaseSession;
}
_ensurePhases() {
if (!this._phases || !this._phasesByName) {
this._phasesByName = new Map();
for (const [phaseName, phaseSpecifier] of Object.entries(this._heftConfigurationJson.phasesByName || {})) {
const phase = new HeftPhase(this, phaseName, phaseSpecifier);
this._phasesByName.set(phaseName, phase);
}
this._phases = new Set(this._phasesByName.values());
}
}
}
//# sourceMappingURL=InternalHeftSession.js.map