Skip to content

Commit 9d93d0b

Browse files
committed
Add support for incrementing prerelease
1 parent 91c354e commit 9d93d0b

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

app/Commands/IncrementCommand.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function configure(): void
2828
$this->setDescription('Increment a version');
2929
$this->setAliases(['incr']);
3030
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
31-
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch)', 'patch');
31+
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch, and prerelease)', 'patch');
3232
$this->addOption('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version');
3333
$this->addOption('pre', null, InputArgument::OPTIONAL, 'Pre-release identifier to append to the version');
3434
$this->setHelp(<<<'HELP'
@@ -104,11 +104,29 @@ private function increment(Version $version, string $part, $pre = null, $build =
104104
$version = $version->incrementPatch();
105105
break;
106106

107+
case 'prerelease':
108+
$currentPrerelease = $version->getPreRelease();
109+
110+
if ($currentPrerelease === null) {
111+
throw new InvalidArgumentException('Unable to increment prerelease on a version without a prerelease tag.');
112+
}
113+
114+
if ($pre !== null && is_string($pre) && $pre !== '') {
115+
throw new InvalidArgumentException('Specifying a prerelease tag when incrementing the prerelease part is not allowed.');
116+
}
117+
118+
$identifier = $currentPrerelease->getIdentifiers();
119+
$preTag = $identifier[0];
120+
$preVersion = (int) ($identifier[1] ?? 0) + 1;
121+
$version = $version->withPreRelease(sprintf('%s.%d', $preTag, $preVersion));
122+
123+
break;
124+
107125
default:
108-
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', or 'patch'.", $part));
126+
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', 'patch', or 'prerelease'.", $part));
109127
}
110128

111-
if (is_string($pre) && $pre !== '') {
129+
if ($part !== 'prerelease' && is_string($pre) && $pre !== '') {
112130
$version = $version->withPreRelease($pre);
113131
}
114132

tests/bashunit/test_increment.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function test_increment_major() {
1717
}
1818

1919
# @data_provider data_increment_with_pre_release
20-
function test_increment_with_pre_release() {
20+
function test_increment_with_prerelease() {
2121
ver=$(bin/version increment $1 --pre alpha);
2222
assert_equals $ver $2;
2323
}
@@ -34,6 +34,16 @@ function test_increment_with_pre_release_and_build() {
3434
assert_equals $ver $2;
3535
}
3636

37+
# @data_provider data_increment_prerelease
38+
function test_increment_with_prerelease_and_build() {
39+
ver=$(bin/version increment $1 --part prerelease);
40+
assert_equals $ver $2;
41+
}
42+
43+
function data_increment_prerelease() {
44+
echo "1.0.0-beta" "1.0.0-beta.1";
45+
}
46+
3747
function data_increment_patch() {
3848
echo "0.0.0" "0.0.1";
3949
echo "1.0.0" "1.0.1";

tests/phpunit/Commands/IncrementCommandTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,45 @@ public function testIncrementWithPreRelease(): void
8484
);
8585
}
8686

87+
/**
88+
* @dataProvider dataIncrementPrerelease
89+
*/
90+
public function testIncrementPrerelease(string $version, string $expect): void
91+
{
92+
$this->tester->execute(['version' => $version, '--part' => 'prerelease']);
93+
94+
self::assertStringContainsString(
95+
$expect,
96+
$this->tester->getDisplay(),
97+
);
98+
}
99+
100+
public static function dataIncrementPrerelease(): iterable
101+
{
102+
yield ['1.0.0-beta', '1.0.0-beta.1'];
103+
yield ['1.0.0-alpha.1', '1.0.0-alpha.2'];
104+
}
105+
106+
public function testIncrementPrereleaseWithPreTag(): void
107+
{
108+
$this->tester->execute(['version' => '1.0.0-beta', '--part' => 'prerelease', '--pre' => 'rc']);
109+
110+
self::assertStringContainsString(
111+
"[ERROR] Specifying a prerelease tag when incrementing the prerelease part is not allowed.",
112+
$this->tester->getDisplay(),
113+
);
114+
}
115+
116+
public function testIncrementPrereleaseWithoutReleaseTag(): void
117+
{
118+
$this->tester->execute(['version' => '1.0.0', '--part' => 'prerelease']);
119+
120+
self::assertStringContainsString(
121+
"[ERROR] Unable to increment prerelease on a version without a prerelease tag.",
122+
$this->tester->getDisplay(),
123+
);
124+
}
125+
87126
public static function dataInvalidVersionArgument(): iterable
88127
{
89128
yield ['v'];

0 commit comments

Comments
 (0)