Skip to content

Commit ab59082

Browse files
committed
added several checks for common version number mistakes
1 parent ed77026 commit ab59082

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

bin/release.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23

34
/*
@@ -12,11 +13,64 @@
1213
require_once __DIR__.'/../vendor/autoload.php';
1314

1415
if (!isset($argv[1])) {
15-
throw new \InvalidArgumentException('You must provide version.');
16+
throw new \InvalidArgumentException('You must specify the version: v1.x.x or next.');
1617
}
1718

1819
$version = $argv[1];
1920

20-
echo sprintf("Releasing symfony version \"%s\".\n", $version);
21+
exec('git tag -l', $tags, $resultCode);
22+
23+
if ($resultCode > 0 || count($tags) === 0) {
24+
throw new \RuntimeException('Reading tag failed.');
25+
}
26+
27+
$latestVersionNumber = $tags[0];
28+
foreach ($tags as $tag) {
29+
if (version_compare($latestVersionNumber, $tag) < 0) {
30+
$latestVersionNumber = $tag;
31+
}
32+
}
33+
34+
if ($version !== 'next') {
35+
if (!preg_match('/^v1\.([5-9]|\d{2,})\.\d+$/', $version)) {
36+
throw new \InvalidArgumentException(sprintf('The format of the specified version number is incorrect: "%s"', $version));
37+
}
38+
39+
if (in_array($version, $tags)) {
40+
throw new \InvalidArgumentException(sprintf('The specified version number already exists: "%s"', $version));
41+
}
42+
43+
[$latestMajorPart, $latestMinorPart, $latestPatchPart] = explode('.', $latestVersionNumber);
44+
[$versionMajorPart, $versionMinorPart, $versionPatchPart] = explode('.', $version);
45+
46+
// This cannot be due to regexp. Just double check.
47+
if ($latestMajorPart !== $versionMajorPart) {
48+
throw new \InvalidArgumentException(sprintf('The specified version number can\'t change major: "%s"', $version));
49+
}
50+
51+
// changed minor or patch
52+
if ($latestMinorPart !== $versionMinorPart) {
53+
if ($versionPatchPart !== '0') {
54+
throw new \InvalidArgumentException(sprintf('The specified version number should be: "%s.%s.0"', $versionMajorPart, $versionMinorPart));
55+
}
56+
} elseif ($latestPatchPart !== $versionPatchPart) {
57+
$latestPatchPartInt = (int) $latestPatchPart;
58+
$versionPatchPartInt = (int) $versionPatchPart;
59+
60+
$nextPatchPartInt = $latestPatchPartInt+1;
61+
62+
if ($nextPatchPartInt !== $versionPatchPartInt) {
63+
throw new \InvalidArgumentException(sprintf('Don\'t skip patch version. The specified version number should be: "%s.%s.%d"', $versionMajorPart, $versionMinorPart, $nextPatchPartInt));
64+
}
65+
}
66+
} else {
67+
[$latestMajorPart, $latestMinorPart, $latestPatchPart] = explode('.', $latestVersionNumber);
68+
$nextPatchPart = (int) $latestPatchPart + 1;
69+
$version = sprintf('%s.%s.%d', $latestMajorPart, $latestMinorPart, $nextPatchPart);
70+
}
71+
72+
$rawVersion = substr($version, 1);
73+
74+
echo sprintf("Prepare symfony version \"%s\".\n", $rawVersion);
2175

2276
exit(0);

0 commit comments

Comments
 (0)