forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionManager.ts
More file actions
348 lines (314 loc) · 12.8 KB
/
VersionManager.ts
File metadata and controls
348 lines (314 loc) · 12.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import * as semver from 'semver';
import { cloneDeep } from 'lodash';
import {
IPackageJson,
JsonFile,
FileConstants
} from '@rushstack/node-core-library';
import {
VersionPolicy,
BumpType,
LockStepVersionPolicy
} from '../api/VersionPolicy';
import { ChangeFile } from '../api/ChangeFile';
import { ChangeType, IChangeInfo } from '../api/ChangeManagement';
import { RushConfiguration } from '../api/RushConfiguration';
import { RushConfigurationProject } from '../api/RushConfigurationProject';
import { VersionPolicyConfiguration } from '../api/VersionPolicyConfiguration';
import { PublishUtilities } from './PublishUtilities';
import { ChangeManager } from './ChangeManager';
export class VersionManager {
private _rushConfiguration: RushConfiguration;
private _userEmail: string;
private _versionPolicyConfiguration: VersionPolicyConfiguration;
private _updatedProjects: Map<string, IPackageJson>;
private _changeFiles: Map<string, ChangeFile>;
public constructor(
_rushConfiguration: RushConfiguration,
_userEmail: string,
_versionPolicyConfiguration?: VersionPolicyConfiguration
) {
this._rushConfiguration = _rushConfiguration;
this._userEmail = _userEmail;
this._versionPolicyConfiguration = _versionPolicyConfiguration
? _versionPolicyConfiguration
: this._rushConfiguration.versionPolicyConfiguration;
this._updatedProjects = new Map<string, IPackageJson>();
this._changeFiles = new Map<string, ChangeFile>();
}
/**
* Ensures project versions follow the provided version policy. If version policy is not
* provided, all projects will have their version checked according to the associated version policy.
* package.json files will be updated if needed.
* This method does not commit changes.
* @param versionPolicyName -- version policy name
* @param shouldCommit -- should update files to disk
* @param force -- update even when project version is higher than policy version.
*/
public ensure(versionPolicyName?: string, shouldCommit?: boolean, force?: boolean): void {
this._ensure(versionPolicyName, shouldCommit, force);
}
/**
* Bumps versions following version policies.
*
* @param lockStepVersionPolicyName - a specified lock step version policy name. Without this value,
* versions for all lock step policies and all individual policies will be bumped.
* With this value, only the specified lock step policy will be bumped along with all individual policies.
* @param bumpType - overrides the default bump type and only works for lock step policy
* @param identifier - overrides the prerelease identifier and only works for lock step policy
* @param shouldCommit - whether the changes will be written to disk
*/
public bump(lockStepVersionPolicyName?: string,
bumpType?: BumpType,
identifier?: string,
shouldCommit?: boolean
): void {
// Bump all the lock step version policies.
this._versionPolicyConfiguration.bump(lockStepVersionPolicyName, bumpType, identifier, shouldCommit);
// Update packages and generate change files due to lock step bump.
this._ensure(lockStepVersionPolicyName, shouldCommit);
// Refresh rush configuration
this._rushConfiguration = RushConfiguration.loadFromConfigurationFile(this._rushConfiguration.rushJsonFile);
// Update projects based on individual policies
const changeManager: ChangeManager = new ChangeManager(this._rushConfiguration,
this._getLockStepProjects());
changeManager.load(this._rushConfiguration.changesFolder);
if (changeManager.hasChanges()) {
changeManager.validateChanges(this._versionPolicyConfiguration);
changeManager.apply(!!shouldCommit)!.forEach(packageJson => {
this._updatedProjects.set(packageJson.name, packageJson);
});
changeManager.updateChangelog(!!shouldCommit);
}
}
public get updatedProjects(): Map<string, IPackageJson> {
return this._updatedProjects;
}
public get changeFiles(): Map<string, ChangeFile> {
return this._changeFiles;
}
private _ensure(versionPolicyName?: string, shouldCommit?: boolean, force?: boolean): void {
this._updateVersionsByPolicy(versionPolicyName, force);
// Update all dependencies if needed.
this._updateDependencies();
if (shouldCommit) {
this._updatePackageJsonFiles();
this._changeFiles.forEach((changeFile) => {
changeFile.writeSync();
});
}
}
private _getLockStepProjects(): Set<string> | undefined {
const lockStepVersionPolicyNames: Set<string> = new Set<string>();
this._versionPolicyConfiguration.versionPolicies.forEach((versionPolicy) => {
if (versionPolicy instanceof LockStepVersionPolicy) {
lockStepVersionPolicyNames.add(versionPolicy.policyName);
}
});
const lockStepProjectNames: Set<string> = new Set<string>();
this._rushConfiguration.projects.forEach((rushProject) => {
if (lockStepVersionPolicyNames.has(rushProject.versionPolicyName!)) {
lockStepProjectNames.add(rushProject.packageName);
}
});
return lockStepProjectNames;
}
private _updateVersionsByPolicy(versionPolicyName?: string, force?: boolean): void {
// Update versions based on version policy
this._rushConfiguration.projects.forEach(rushProject => {
const projectVersionPolicyName: string | undefined = rushProject.versionPolicyName;
if (projectVersionPolicyName &&
(!versionPolicyName || projectVersionPolicyName === versionPolicyName)) {
const versionPolicy: VersionPolicy = this._versionPolicyConfiguration.getVersionPolicy(
projectVersionPolicyName);
const updatedProject: IPackageJson | undefined = versionPolicy.ensure(rushProject.packageJson, force);
if (updatedProject) {
this._updatedProjects.set(updatedProject.name, updatedProject);
// No need to create an entry for prerelease version bump.
if (!this._isPrerelease(updatedProject.version) && rushProject.isMainProject) {
this._addChangeInfo(updatedProject.name,
[this._createChangeInfo(updatedProject, rushProject)]);
}
}
}
});
}
private _isPrerelease(version: string): boolean {
return !!semver.prerelease(version);
}
private _addChangeInfo(packageName: string,
changeInfos: IChangeInfo[]
): void {
if (!changeInfos.length) {
return;
}
let changeFile: ChangeFile | undefined = this._changeFiles.get(packageName);
if (!changeFile) {
changeFile = new ChangeFile({
changes: [],
packageName: packageName,
email: this._userEmail
}, this._rushConfiguration);
this._changeFiles.set(packageName, changeFile);
}
changeInfos.forEach((changeInfo) => {
changeFile!.addChange(changeInfo);
});
}
private _updateDependencies(): void {
this._rushConfiguration.projects.forEach(rushProject => {
let clonedProject: IPackageJson | undefined = this._updatedProjects.get(rushProject.packageName);
let projectVersionChanged: boolean = true;
if (!clonedProject) {
clonedProject = cloneDeep(rushProject.packageJson);
projectVersionChanged = false;
}
this._updateProjectAllDependencies(rushProject, clonedProject, projectVersionChanged);
});
}
private _updateProjectAllDependencies(
rushProject: RushConfigurationProject,
clonedProject: IPackageJson,
projectVersionChanged: boolean
): void {
if (!clonedProject.dependencies && !clonedProject.devDependencies) {
return;
}
const changes: IChangeInfo[] = [];
let updated: boolean = false;
if (this._updateProjectDependencies(clonedProject.dependencies, changes,
clonedProject, rushProject, projectVersionChanged)
) {
updated = true;
}
if (this._updateProjectDependencies(clonedProject.devDependencies, changes,
clonedProject, rushProject, projectVersionChanged)
) {
updated = true;
}
if (this._updateProjectDependencies(clonedProject.peerDependencies, changes,
clonedProject, rushProject, projectVersionChanged)
) {
updated = true;
}
if (updated) {
this._updatedProjects.set(clonedProject.name, clonedProject);
this._addChangeInfo(clonedProject.name, changes);
}
}
private _updateProjectDependencies(dependencies: { [key: string]: string; } | undefined,
changes: IChangeInfo[],
clonedProject: IPackageJson,
rushProject: RushConfigurationProject,
projectVersionChanged: boolean
): boolean {
if (!dependencies) {
return false;
}
let updated: boolean = false;
this._updatedProjects.forEach((updatedDependentProject, updatedDependentProjectName) => {
if (dependencies[updatedDependentProjectName]) {
if (rushProject.cyclicDependencyProjects.has(updatedDependentProjectName)) {
// Skip if cyclic
console.log(`Found cyclic ${rushProject.packageName} ${updatedDependentProjectName}`);
return;
}
const oldDependencyVersion: string = dependencies[updatedDependentProjectName];
const newDependencyVersion: string = PublishUtilities.getNewDependencyVersion(
dependencies,
updatedDependentProjectName,
updatedDependentProject.version
);
if (newDependencyVersion !== oldDependencyVersion) {
updated = true;
if (this._shouldTrackDependencyChange(rushProject, updatedDependentProjectName)) {
this._trackDependencyChange(changes, clonedProject, projectVersionChanged,
updatedDependentProject,
oldDependencyVersion,
newDependencyVersion
);
}
dependencies[updatedDependentProjectName] = newDependencyVersion;
}
}
});
return updated;
}
private _shouldTrackDependencyChange(
rushProject: RushConfigurationProject,
dependencyName: string
): boolean {
const dependencyRushProject: RushConfigurationProject | undefined =
this._rushConfiguration.projectsByName.get(dependencyName);
return !!dependencyRushProject && rushProject.shouldPublish &&
(!rushProject.versionPolicy ||
!rushProject.versionPolicy.isLockstepped ||
rushProject.isMainProject && (dependencyRushProject.versionPolicyName !== rushProject.versionPolicyName));
}
private _trackDependencyChange(
changes: IChangeInfo[],
clonedProject: IPackageJson,
projectVersionChanged: boolean,
updatedDependentProject: IPackageJson,
oldDependencyVersion: string,
newDependencyVersion: string
): void {
if (!semver.satisfies(updatedDependentProject.version, oldDependencyVersion) && !projectVersionChanged) {
this._addChange(changes,
{
changeType: ChangeType.patch,
packageName: clonedProject.name
}
);
}
// If current version is not a prerelease version and new dependency is also not a prerelease version,
// add change entry. Otherwise, too many changes will be created for frequent releases.
if (!this._isPrerelease(updatedDependentProject.version) && !this._isPrerelease(clonedProject.version)) {
this._addChange(changes,
{
changeType: ChangeType.dependency,
comment: `Dependency ${updatedDependentProject.name} version bump from ${oldDependencyVersion}` +
` to ${newDependencyVersion}.`,
packageName: clonedProject.name
}
);
}
}
private _addChange(changes: IChangeInfo[], newChange: IChangeInfo): void {
const exists: boolean = changes.some((changeInfo) => {
return (changeInfo.author === newChange.author &&
changeInfo.changeType === newChange.changeType &&
changeInfo.comment === newChange.comment &&
changeInfo.commit === newChange.commit &&
changeInfo.packageName === newChange.packageName &&
changeInfo.type === newChange.type
);
});
if (!exists) {
changes.push(newChange);
}
}
private _updatePackageJsonFiles(): void {
this._updatedProjects.forEach((newPackageJson, packageName) => {
const rushProject: RushConfigurationProject | undefined = this._rushConfiguration.getProjectByName(packageName);
// Update package.json
if (rushProject) {
const packagePath: string = path.join(rushProject.projectFolder, FileConstants.PackageJson);
JsonFile.save(newPackageJson, packagePath, { updateExistingFile: true });
}
});
}
private _createChangeInfo(newPackageJson: IPackageJson,
rushProject: RushConfigurationProject
): IChangeInfo {
return {
changeType: ChangeType.none,
newVersion: newPackageJson.version,
packageName: newPackageJson.name,
comment: ''
};
}
}