|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { |
| 5 | + Terminal, |
| 6 | + ConsoleTerminalProvider |
| 7 | +} from '@microsoft/node-core-library'; |
| 8 | + |
| 9 | +import { ITask, ITaskDefinition } from './ITask'; |
| 10 | +import { TaskStatus } from './TaskStatus'; |
| 11 | + |
| 12 | +export interface ITaskCollectionOptions { |
| 13 | + quietMode: boolean; |
| 14 | + terminal?: Terminal; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * A class which manages the execution of a set of tasks with interdependencies. |
| 19 | + * Any class of task definition may be registered, and dependencies between tasks are |
| 20 | + * easily specified. Initially, and at the end of each task execution, all unblocked tasks |
| 21 | + * are added to a ready queue which is then executed. This is done continually until all |
| 22 | + * tasks are complete, or prematurely fails if any of the tasks fail. |
| 23 | + */ |
| 24 | +export class TaskCollection { |
| 25 | + private _tasks: Map<string, ITask>; |
| 26 | + private _quietMode: boolean; |
| 27 | + private _terminal: Terminal; |
| 28 | + |
| 29 | + constructor(options: ITaskCollectionOptions) { |
| 30 | + const { |
| 31 | + quietMode, |
| 32 | + terminal = new Terminal(new ConsoleTerminalProvider()) |
| 33 | + } = options; |
| 34 | + this._tasks = new Map<string, ITask>(); |
| 35 | + this._quietMode = quietMode; |
| 36 | + this._terminal = terminal; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Registers a task definition to the map of defined tasks |
| 41 | + */ |
| 42 | + public addTask(taskDefinition: ITaskDefinition): void { |
| 43 | + if (this._tasks.has(taskDefinition.name)) { |
| 44 | + throw new Error('A task with that name has already been registered.'); |
| 45 | + } |
| 46 | + |
| 47 | + const task: ITask = taskDefinition as ITask; |
| 48 | + task.dependencies = new Set<ITask>(); |
| 49 | + task.dependents = new Set<ITask>(); |
| 50 | + task.status = TaskStatus.Ready; |
| 51 | + task.criticalPathLength = undefined; |
| 52 | + this._tasks.set(task.name, task); |
| 53 | + |
| 54 | + if (!this._quietMode) { |
| 55 | + this._terminal.writeLine(`Registered ${task.name}`); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns true if a task with that name has been registered |
| 61 | + */ |
| 62 | + public hasTask(taskName: string): boolean { |
| 63 | + return this._tasks.has(taskName); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Defines the list of dependencies for an individual task. |
| 68 | + * @param taskName - the string name of the task for which we are defining dependencies. A task with this |
| 69 | + * name must already have been registered. |
| 70 | + */ |
| 71 | + public addDependencies(taskName: string, taskDependencies: string[]): void { |
| 72 | + const task: ITask | undefined = this._tasks.get(taskName); |
| 73 | + |
| 74 | + if (!task) { |
| 75 | + throw new Error(`The task '${taskName}' has not been registered`); |
| 76 | + } |
| 77 | + if (!taskDependencies) { |
| 78 | + throw new Error('The list of dependencies must be defined'); |
| 79 | + } |
| 80 | + |
| 81 | + for (const dependencyName of taskDependencies) { |
| 82 | + if (!this._tasks.has(dependencyName)) { |
| 83 | + throw new Error(`The project '${dependencyName}' has not been registered.`); |
| 84 | + } |
| 85 | + const dependency: ITask = this._tasks.get(dependencyName)!; |
| 86 | + task.dependencies.add(dependency); |
| 87 | + dependency.dependents.add(task); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Executes all tasks which have been registered, returning a promise which is resolved when all the |
| 93 | + * tasks are completed successfully, or rejects when any task fails. |
| 94 | + */ |
| 95 | + public getOrderedTasks(): ITask[] { |
| 96 | + this._checkForCyclicDependencies(this._tasks.values(), []); |
| 97 | + |
| 98 | + // Precalculate the number of dependent packages |
| 99 | + this._tasks.forEach((task: ITask) => { |
| 100 | + this._calculateCriticalPaths(task); |
| 101 | + }); |
| 102 | + |
| 103 | + const buildQueue: ITask[] = []; |
| 104 | + // Add everything to the buildQueue |
| 105 | + this._tasks.forEach((task: ITask) => { |
| 106 | + buildQueue.push(task); |
| 107 | + }); |
| 108 | + |
| 109 | + // Sort the queue in descending order, nothing will mess with the order |
| 110 | + buildQueue.sort((taskA: ITask, taskB: ITask): number => { |
| 111 | + return taskB.criticalPathLength! - taskA.criticalPathLength!; |
| 112 | + }); |
| 113 | + |
| 114 | + return buildQueue; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Checks for projects that indirectly depend on themselves. |
| 119 | + */ |
| 120 | + private _checkForCyclicDependencies(tasks: Iterable<ITask>, dependencyChain: string[]): void { |
| 121 | + for (const task of tasks) { |
| 122 | + if (dependencyChain.indexOf(task.name) >= 0) { |
| 123 | + throw new Error('A cyclic dependency was encountered:\n' |
| 124 | + + ' ' + [...dependencyChain, task.name].reverse().join('\n -> ') |
| 125 | + + '\nConsider using the cyclicDependencyProjects option for rush.json.'); |
| 126 | + } |
| 127 | + dependencyChain.push(task.name); |
| 128 | + this._checkForCyclicDependencies(task.dependents, dependencyChain); |
| 129 | + dependencyChain.pop(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Calculate the number of packages which must be built before we reach |
| 135 | + * the furthest away "root" node |
| 136 | + */ |
| 137 | + private _calculateCriticalPaths(task: ITask): number { |
| 138 | + // Return the memoized value |
| 139 | + if (task.criticalPathLength !== undefined) { |
| 140 | + return task.criticalPathLength; |
| 141 | + } |
| 142 | + |
| 143 | + // If no dependents, we are in a "root" |
| 144 | + if (task.dependents.size === 0) { |
| 145 | + return task.criticalPathLength = 0; |
| 146 | + } else { |
| 147 | + // Otherwise we are as long as the longest package + 1 |
| 148 | + const depsLengths: number[] = []; |
| 149 | + task.dependents.forEach(dep => depsLengths.push(this._calculateCriticalPaths(dep))); |
| 150 | + return task.criticalPathLength = Math.max(...depsLengths) + 1; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments