forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSelector.ts
More file actions
235 lines (198 loc) · 7.94 KB
/
TaskSelector.ts
File metadata and controls
235 lines (198 loc) · 7.94 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
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
import {
RushConfiguration,
IRushLinkJson
} from '../api/RushConfiguration';
import { RushConfigurationProject } from '../api/RushConfigurationProject';
import { JsonFile } from '@microsoft/node-core-library';
import { ProjectTask, convertSlashesForWindows } from '../logic/taskRunner/ProjectTask';
import { PackageChangeAnalyzer } from './PackageChangeAnalyzer';
import { TaskCollection } from './taskRunner/TaskCollection';
export interface ITaskSelectorConstructor {
rushConfiguration: RushConfiguration;
toFlags: ReadonlyArray<string>;
fromFlags: ReadonlyArray<string>;
commandToRun: string;
customParameterValues: string[];
isQuietMode: boolean;
isIncrementalBuildAllowed: boolean;
ignoreMissingScript: boolean;
ignoreDependencyOrder: boolean;
packageDepsFilename: string;
}
/**
* This class is responsible for:
* - based on to/from flags, solving the dependency graph and figuring out which projects need to be run
* - creating a ProjectTask for each project that needs to be built
* - registering the necessary ProjectTasks with the TaskRunner, which actually orchestrates execution
*/
export class TaskSelector {
private _taskCollection: TaskCollection;
private _dependentList: Map<string, Set<string>>;
private _rushLinkJson: IRushLinkJson;
private _options: ITaskSelectorConstructor;
private _packageChangeAnalyzer: PackageChangeAnalyzer;
constructor(options: ITaskSelectorConstructor) {
this._options = options;
this._packageChangeAnalyzer = new PackageChangeAnalyzer(options.rushConfiguration);
this._taskCollection = new TaskCollection({
quietMode: options.isQuietMode
});
try {
this._rushLinkJson = JsonFile.load(this._options.rushConfiguration.rushLinkJsonFilename);
} catch (error) {
throw new Error(`Could not read "${this._options.rushConfiguration.rushLinkJsonFilename}".`
+ ` Did you run "rush install" or "rush update"?`);
}
}
public registerTasks(): TaskCollection {
if (this._options.toFlags.length > 0) {
this._registerToFlags(this._options.toFlags);
}
if (this._options.fromFlags.length > 0) {
this._registerFromFlags(this._options.fromFlags);
}
if (this._options.toFlags.length === 0 && this._options.fromFlags.length === 0) {
this._registerAll();
}
return this._taskCollection;
}
private _registerToFlags(toFlags: ReadonlyArray<string>): void {
const dependencies: Set<string> = new Set<string>();
for (const toFlag of toFlags) {
const toProject: RushConfigurationProject | undefined =
this._options.rushConfiguration.findProjectByShorthandName(toFlag);
if (!toProject) {
throw new Error(`The project '${toFlag}' does not exist in rush.json`);
}
this._collectAllDependencies(toProject.packageName, dependencies);
}
// Register any dependencies it may have
for (const dependency of dependencies) {
this._registerTask(this._options.rushConfiguration.getProjectByName(dependency));
}
if (!this._options.ignoreDependencyOrder) {
// Add ordering relationships for each dependency
for (const dependency of dependencies) {
this._taskCollection.addDependencies(
dependency,
this._rushLinkJson.localLinks[dependency] || []
);
}
}
}
private _registerFromFlags(fromFlags: ReadonlyArray<string>): void {
this._buildDependentGraph();
const dependents: Set<string> = new Set<string>();
for (const fromFlag of fromFlags) {
const fromProject: RushConfigurationProject | undefined
= this._options.rushConfiguration.findProjectByShorthandName(fromFlag);
if (!fromProject) {
throw new Error(`The project '${fromFlag}' does not exist in rush.json`);
}
this._collectAllDependents(fromProject.packageName, dependents);
}
// Register all downstream dependents
for (const dependent of dependents) {
this._registerTask(this._options.rushConfiguration.getProjectByName(dependent));
}
if (!this._options.ignoreDependencyOrder) {
// Only add ordering relationships for projects which have been registered
// e.g. package C may depend on A & B, but if we are only building A's downstream, we will ignore B
for (const dependent of dependents) {
this._taskCollection.addDependencies(
dependent,
(this._rushLinkJson.localLinks[dependent] || []).filter(dep => dependents.has(dep))
);
}
}
}
private _registerAll(): void {
// Register all tasks
for (const rushProject of this._options.rushConfiguration.projects) {
this._registerTask(rushProject);
}
if (!this._options.ignoreDependencyOrder) {
// Add ordering relationships for each dependency
for (const projectName of Object.keys(this._rushLinkJson.localLinks)) {
this._taskCollection.addDependencies(projectName, this._rushLinkJson.localLinks[projectName]);
}
}
}
/**
* Collects all upstream dependencies for a certain project
*/
private _collectAllDependencies(project: string, result: Set<string>): void {
if (!result.has(project)) {
result.add(project);
for (const dependency of this._rushLinkJson.localLinks[project] || []) {
this._collectAllDependencies(dependency, result);
}
}
}
/**
* Collects all downstream dependents of a certain project
*/
private _collectAllDependents(project: string, result: Set<string>): void {
if (!result.has(project)) {
result.add(project);
for (const dependent of (this._dependentList.get(project) || new Set<string>())) {
this._collectAllDependents(dependent, result);
}
}
}
/**
* Inverts the localLinks to arrive at the dependent graph. This helps when using the --from flag
*/
private _buildDependentGraph(): void {
this._dependentList = new Map<string, Set<string>>();
for (const project of Object.keys(this._rushLinkJson.localLinks)) {
for (const dep of this._rushLinkJson.localLinks[project]) {
if (!this._dependentList.has(dep)) {
this._dependentList.set(dep, new Set<string>());
}
this._dependentList.get(dep)!.add(project);
}
}
}
private _registerTask(project: RushConfigurationProject | undefined): void {
if (project) {
const projectTask: ProjectTask = new ProjectTask({
rushProject: project,
rushConfiguration: this._options.rushConfiguration,
commandToRun: this._getScriptToRun(project),
isIncrementalBuildAllowed: this._options.isIncrementalBuildAllowed,
packageChangeAnalyzer: this._packageChangeAnalyzer,
packageDepsFilename: this._options.packageDepsFilename
});
if (!this._taskCollection.hasTask(projectTask.name)) {
this._taskCollection.addTask(projectTask);
}
}
}
private _getScriptToRun(rushProject: RushConfigurationProject): string {
const script: string | undefined = this._getScriptCommand(rushProject, this._options.commandToRun);
if (script === undefined && !this._options.ignoreMissingScript) {
// tslint:disable-next-line:max-line-length
throw new Error(`The project [${rushProject.packageName}] does not define a '${this._options.commandToRun}' command in the 'scripts' section of its package.json`);
}
if (!script) {
return '';
}
const taskCommand: string = `${script} ${this._options.customParameterValues.join(' ')}`;
return process.platform === 'win32'
? convertSlashesForWindows(taskCommand)
: taskCommand;
}
private _getScriptCommand(rushProject: RushConfigurationProject, script: string): string | undefined {
// tslint:disable-next-line:no-string-literal
if (!rushProject.packageJson.scripts) {
return undefined;
}
const rawCommand: string = rushProject.packageJson.scripts[script];
// tslint:disable-next-line:no-null-keyword
if (rawCommand === undefined || rawCommand === null) {
return undefined;
}
return rawCommand;
}
}