-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncrementCommand.php
More file actions
146 lines (119 loc) · 4.14 KB
/
IncrementCommand.php
File metadata and controls
146 lines (119 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Syntatis\Version\CLI\Commands;
use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Syntatis\Version\CLI\Exceptions\InvalidArgumentType;
use Throwable;
use Version\Version;
use function is_numeric;
use function is_string;
use function sprintf;
use function trim;
final class IncrementCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->setName('increment');
$this->setDescription('Increment a version');
$this->setAliases(['incr']);
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch, and prerelease)', 'patch');
$this->addOption('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version');
$this->addOption('pre', null, InputArgument::OPTIONAL, 'Pre-release identifier to append to the version');
$this->setHelp(<<<'HELP'
This command increments the provided version by the specified part (major, minor, patch).
You can also append build metadata or a pre-release identifier to the version.
Usage:
<info>version increment 1.0.0</info>
<info>version increment 1.0.0 --part=minor</info>
<info>version increment 1.0.0 --build=123</info>
<info>version increment 1.0.0 --pre=beta</info>
HELP,);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$part = $input->getOption('part');
$version = $input->getArgument('version');
$build = $input->getOption('build');
$pre = $input->getOption('pre');
try {
if (! is_string($part)) {
throw new InvalidArgumentType($part);
}
} catch (Throwable $th) {
$style->error($th->getMessage());
return Command::FAILURE;
}
try {
if (! is_string($version)) {
throw new InvalidArgumentType($version);
}
/** @var Version $parsed */
$parsed = Version::fromString($version);
$style->writeln(
(string) $this->increment(
$parsed,
$part,
$pre,
$build,
),
);
} catch (Throwable $th) {
$style->error($th->getMessage());
return Command::FAILURE;
}
return Command::SUCCESS;
}
/**
* @param mixed $pre
* @param mixed $build
*/
private function increment(Version $version, string $part, $pre = null, $build = null): Version
{
switch ($part) {
case 'major':
$version = $version->incrementMajor();
break;
case 'minor':
$version = $version->incrementMinor();
break;
case 'patch':
$version = $version->incrementPatch();
break;
case 'prerelease':
$currentPrerelease = $version->getPreRelease();
if ($currentPrerelease === null) {
throw new InvalidArgumentException('Unable to increment prerelease on a version without a prerelease tag.');
}
if ($pre !== null && is_string($pre) && $pre !== '') {
throw new InvalidArgumentException('Specifying a prerelease tag when incrementing the prerelease part is not allowed.');
}
$identifier = $currentPrerelease->getIdentifiers();
$preTag = isset($identifier[0]) && is_string($identifier[0]) ? trim($identifier[0]) : null;
if ($preTag === null || $preTag === '') {
throw new InvalidArgumentException('Unable to determine the prerelease tag for incrementing.');
}
$preVersion = isset($identifier[1]) && is_numeric($identifier[1]) ? (int) $identifier[1] : 0;
$version = $version->withPreRelease(sprintf('%s.%d', $preTag, $preVersion + 1));
break;
default:
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', 'patch', or 'prerelease'.", $part));
}
if ($part !== 'prerelease' && is_string($pre) && $pre !== '') {
$version = $version->withPreRelease($pre);
}
if (is_string($build) && $build !== '') {
$version = $version->withBuild($build);
}
return $version;
}
}