Skip to content

Commit

Permalink
added several checks for common version number mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
connorhu committed Mar 27, 2024
1 parent ed77026 commit ab59082
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions bin/release.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php

/*
Expand All @@ -12,11 +13,64 @@
require_once __DIR__.'/../vendor/autoload.php';

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

$version = $argv[1];

echo sprintf("Releasing symfony version \"%s\".\n", $version);
exec('git tag -l', $tags, $resultCode);

if ($resultCode > 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);

0 comments on commit ab59082

Please sign in to comment.