From ab59082562858efdb2a116e57bc5d130895626b1 Mon Sep 17 00:00:00 2001 From: connor Date: Wed, 27 Mar 2024 10:42:51 +0100 Subject: [PATCH] added several checks for common version number mistakes --- bin/release.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/bin/release.php b/bin/release.php index 945e41c23..50b6e27df 100644 --- a/bin/release.php +++ b/bin/release.php @@ -1,3 +1,4 @@ +#!/usr/bin/env php 0 || count($tags) === 0) { + throw new \RuntimeException('Reading tag failed.'); +} + +$latestVersionNumber = $tags[0]; +foreach ($tags as $tag) { + if (version_compare($latestVersionNumber, $tag) < 0) { + $latestVersionNumber = $tag; + } +} + +if ($version !== 'next') { + if (!preg_match('/^v1\.([5-9]|\d{2,})\.\d+$/', $version)) { + throw new \InvalidArgumentException(sprintf('The format of the specified version number is incorrect: "%s"', $version)); + } + + if (in_array($version, $tags)) { + throw new \InvalidArgumentException(sprintf('The specified version number already exists: "%s"', $version)); + } + + [$latestMajorPart, $latestMinorPart, $latestPatchPart] = explode('.', $latestVersionNumber); + [$versionMajorPart, $versionMinorPart, $versionPatchPart] = explode('.', $version); + + // This cannot be due to regexp. Just double check. + if ($latestMajorPart !== $versionMajorPart) { + throw new \InvalidArgumentException(sprintf('The specified version number can\'t change major: "%s"', $version)); + } + + // changed minor or patch + if ($latestMinorPart !== $versionMinorPart) { + if ($versionPatchPart !== '0') { + throw new \InvalidArgumentException(sprintf('The specified version number should be: "%s.%s.0"', $versionMajorPart, $versionMinorPart)); + } + } elseif ($latestPatchPart !== $versionPatchPart) { + $latestPatchPartInt = (int) $latestPatchPart; + $versionPatchPartInt = (int) $versionPatchPart; + + $nextPatchPartInt = $latestPatchPartInt+1; + + if ($nextPatchPartInt !== $versionPatchPartInt) { + throw new \InvalidArgumentException(sprintf('Don\'t skip patch version. The specified version number should be: "%s.%s.%d"', $versionMajorPart, $versionMinorPart, $nextPatchPartInt)); + } + } +} else { + [$latestMajorPart, $latestMinorPart, $latestPatchPart] = explode('.', $latestVersionNumber); + $nextPatchPart = (int) $latestPatchPart + 1; + $version = sprintf('%s.%s.%d', $latestMajorPart, $latestMinorPart, $nextPatchPart); +} + +$rawVersion = substr($version, 1); + +echo sprintf("Prepare symfony version \"%s\".\n", $rawVersion); exit(0);