@@ -9,9 +9,11 @@ function usage()
9
9
10
10
echo <<<EOT
11
11
Usage:
12
- {$ argv [0 ]} <command>
12
+ {$ argv [0 ]} <command> [<version>]
13
13
14
14
Commands:
15
+ release: Release the given version (requires second argument)
16
+ to-next-dev: Update to the next version following the current version
15
17
to-stable: Mark the current version as stable
16
18
to-next-patch-dev: Update to the next patch development version
17
19
to-next-minor-dev: Update to the next minor development version
@@ -50,6 +52,25 @@ function read_release_version(string $filename): array
50
52
return $ versions ;
51
53
}
52
54
55
+ function parse_release_version (string $ version ): array
56
+ {
57
+ // Regex copied from https://github.com/pear/pear-core/blob/6f4c3a0b134626d238d75a44af01a2f7c4e688d9/PEAR/Common.php#L32
58
+ if (! preg_match ('#^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:(?<stability>(?:alpha|beta))(?<prereleasenum>\d+))?$# ' , $ version , $ matches )) {
59
+ throw new Exception (sprintf ('Given version "%s" is not in the PEAR version format ' ));
60
+ }
61
+
62
+ return [
63
+ 'version ' => $ version ,
64
+ 'stability ' => $ matches ['stability ' ] ?? 'stable ' ,
65
+ 'versionComponents ' => [
66
+ $ matches ['major ' ],
67
+ $ matches ['minor ' ],
68
+ $ matches ['patch ' ],
69
+ 0 ,
70
+ ],
71
+ ];
72
+ }
73
+
53
74
function write_release_version (string $ filename , array $ version ): void
54
75
{
55
76
if (! is_file ($ filename )) {
@@ -140,6 +161,45 @@ function get_next_minor_version(array $versions): array
140
161
];
141
162
}
142
163
164
+ function get_next_release_version (array $ versions , string $ releaseVersion ): array
165
+ {
166
+ $ releaseVersion = parse_release_version ($ releaseVersion );
167
+
168
+ // When bumping to the specified release version, check if the major, minor, and patch versions match what's currently in the file
169
+ if (array_slice ($ versions ['versionComponents ' ], 0 , 3 ) !== array_slice ($ releaseVersion ['versionComponents ' ], 0 , 3 )) {
170
+ throw new Exception (sprintf ('Cannot bump version "%s" to version "%s". ' , $ versions ['version ' ], $ releaseVersion ['version ' ]));
171
+ }
172
+
173
+ // Now, re-use the existing version components to copy over the previous build number used for DLLs
174
+ $ releaseVersion ['versionComponents ' ] = $ versions ['versionComponents ' ];
175
+
176
+ return $ releaseVersion ;
177
+ }
178
+
179
+ function get_next_dev_version (array $ versions ): array
180
+ {
181
+ $ versionComponents = $ versions ['versionComponents ' ];
182
+
183
+ // We're dealing with a pre-release. The next version is a devel version of the corresponding stable release
184
+ // Examples:
185
+ // 1.19.1snapshot => 1.19.1dev
186
+ // 1.20.0alpha1 => 1.20.0dev
187
+ // 1.20.0beta1 => 1.20.0dev
188
+ if ($ versions ['stability ' ] != 'stable ' ) {
189
+ // Increase the build number for unique DLL versions
190
+ $ versionComponents [3 ]++;
191
+
192
+ return [
193
+ 'version ' => get_version_string_from_components ($ versionComponents ) . 'dev ' ,
194
+ 'stability ' => 'devel ' ,
195
+ 'versionComponents ' => $ versionComponents ,
196
+ ];
197
+ }
198
+
199
+ // For all other releases, return the next patch version
200
+ return get_next_patch_version ($ versions );
201
+ }
202
+
143
203
// Allow 2 arguments as the bump-version action always passes a version number, even when not needed
144
204
if (! in_array ($ argc , [2 , 3 ])) {
145
205
usage ();
@@ -153,6 +213,18 @@ function get_next_minor_version(array $versions): array
153
213
154
214
exit (0 );
155
215
216
+ case 'release ' :
217
+ if ($ argc !== 3 ) {
218
+ usage ();
219
+ }
220
+
221
+ $ newVersion = get_next_release_version ($ currentVersion , $ argv [2 ]);
222
+ break ;
223
+
224
+ case 'to-next-dev ' :
225
+ $ newVersion = get_next_dev_version ($ currentVersion );
226
+ break ;
227
+
156
228
case 'to-stable ' :
157
229
$ newVersion = get_stable_version ($ currentVersion );
158
230
break ;
0 commit comments