Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pre-release versions #29

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions src/utils/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,78 @@

namespace staabm\PHPStanTodoBy\utils;

use Version\Exception\InvalidVersionString;
use Version\Version;

final class ReferenceVersionFinder
{
private const PRE_RELEASE_CHUNK_COUNT = 2;

private TagFetcher $fetcher;
private string $referenceVersion;

private VersionNormalizer $versionNormalizer;

public function __construct(string $referenceVersion, TagFetcher $fetcher)
{
$this->referenceVersion = $referenceVersion;
$this->fetcher = $fetcher;
$this->versionNormalizer = new VersionNormalizer();
}

public function find(?string $workingDirectory): string
{
if (in_array($this->referenceVersion, ['nextMajor', 'nextMinor', 'nextPatch'], true)) {
$latestTagVersion = $this->fetcher->fetchLatestTagVersion($workingDirectory);
return $this->nextVersion($latestTagVersion);
}

$normalized = $this->versionNormalizer->normalize($latestTagVersion);
// composer/semver versions have 4 parts, but Version\Version only accepts 3.
$normalized = preg_replace('/\.0$/', '', $normalized);
if ($normalized === null) {
throw new \RuntimeException('Could not normalize version: ' . $latestTagVersion);
}

$version = Version::fromString($normalized);
// a version string like "1.2.3"
return $this->referenceVersion;
}

if ($this->referenceVersion === 'nextMajor') {
return $version->incrementMajor()->toString();
// adopted from https://github.com/WyriHaximus/github-action-next-semvers/blob/master/src/Next.php
private function nextVersion(string $versionString): string
{
try {
$version = Version::fromString($versionString);
} catch (InvalidVersionString $invalidVersionException) {
if (count(explode('.', $versionString)) === 3) {
throw $invalidVersionException;
}
if ($this->referenceVersion === 'nextMinor') {
return $version->incrementMinor()->toString();

// split versionString by '-' (in case it is a pre-release)
if (strpos($versionString, '-') !== false) {
[$versionString, $preRelease] = explode('-', $versionString, self::PRE_RELEASE_CHUNK_COUNT);
$versionString .= '.0-' . $preRelease;
} else {
$versionString .= '.0';
}
if ($this->referenceVersion === 'nextPatch') {
return $version->incrementPatch()->toString();

return self::nextVersion($versionString);
}

$wasPreRelease = false;

// if current version is a pre-release
if ($version->isPreRelease()) {
// get current version by removing anything else (e.g., pre-release, build-id, ...)
$version = Version::from($version->getMajor(), $version->getMinor(), $version->getPatch());
$wasPreRelease = true;
}

if ($this->referenceVersion === 'nextMajor') {
return $version->incrementMajor()->toString();
}
if ($this->referenceVersion === 'nextMinor') {
return $version->incrementMinor()->toString();
}
if ($this->referenceVersion === 'nextPatch') {
// check if current version is a pre-release
if ($wasPreRelease) {
// use current version (without pre-release)
return $version->__toString();
}
return $version->incrementPatch()->toString();
}

// a version string like "1.2.3"
return $this->referenceVersion;
throw new \RuntimeException('Invalid reference version: ' . $this->referenceVersion);
}
}
16 changes: 16 additions & 0 deletions tests/ReferenceVersionFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,21 @@ public static function provideData(): iterable
'1',
'1.0.1',
];

yield [
'nextMajor',
'1.0-beta',
'2.0.0',
];
yield [
'nextMinor',
'1.0-beta',
'1.1.0',
];
yield [
'nextPatch',
'1.0-beta',
'1.0.0',
];
}
}