-
Notifications
You must be signed in to change notification settings - Fork 370
/
versioning-strategy.ts
142 lines (134 loc) · 3.71 KB
/
versioning-strategy.ts
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Version} from './version';
import {ConventionalCommit} from './commit';
/**
* An interface for updating a version.
*/
export interface VersionUpdater {
/**
* Returns the new bumped version
*
* @param {Version} version The current version
* @returns {Version} The bumped version
*/
bump(version: Version): Version;
}
/**
* This VersionUpdater performs a SemVer major version bump.
*/
export class MajorVersionUpdate implements VersionUpdater {
/**
* Returns the new bumped version
*
* @param {Version} version The current version
* @returns {Version} The bumped version
*/
bump(version: Version): Version {
return new Version(
version.major + 1,
0,
0,
version.preRelease,
version.build
);
}
}
/**
* This VersionUpdater performs a SemVer minor version bump.
*/
export class MinorVersionUpdate implements VersionUpdater {
/**
* Returns the new bumped version
*
* @param {Version} version The current version
* @returns {Version} The bumped version
*/
bump(version: Version): Version {
return new Version(
version.major,
version.minor + 1,
0,
version.preRelease,
version.build
);
}
}
/**
* This VersionUpdater performs a SemVer patch version bump.
*/
export class PatchVersionUpdate implements VersionUpdater {
/**
* Returns the new bumped version
*
* @param {Version} version The current version
* @returns {Version} The bumped version
*/
bump(version: Version): Version {
return new Version(
version.major,
version.minor,
version.patch + 1,
version.preRelease,
version.build
);
}
}
/**
* This VersionUpdater sets the version to a specific version.
*/
export class CustomVersionUpdate implements VersionUpdater {
private versionString: string;
constructor(versionString: string) {
this.versionString = versionString;
}
/**
* Returns the new bumped version. This version is specified
* at initialization.
*
* @param {Version} version The current version
* @returns {Version} The bumped version
*/
bump(_version: Version): Version {
return Version.parse(this.versionString);
}
}
/**
* Implement this interface to create a new versioning scheme.
*/
export interface VersioningStrategy {
/**
* Given the current version of an artifact and a list of commits,
* return the next version.
*
* @param {Version} version The current version
* @param {ConventionalCommit[]} commits The list of commits to consider
* @returns {Version} The next version
*/
bump(version: Version, commits: ConventionalCommit[]): Version;
/**
* Given the current version of an artifact and a list of commits,
* return a VersionUpdater that knows how to bump the version.
*
* This is useful for chaining together versioning strategies.
*
* @param {Version} version The current version
* @param {ConventionalCommit[]} commits The list of commits to consider
* @returns {VersionUpdater} Updater for bumping the next version.
*/
determineReleaseType(
version: Version,
commits: ConventionalCommit[]
): VersionUpdater;
}