-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncrementCommand.php
More file actions
122 lines (102 loc) · 3.25 KB
/
IncrementCommand.php
File metadata and controls
122 lines (102 loc) · 3.25 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
<?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_string;
use function sprintf;
final class IncrementCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->setName('increment');
$this->setDescription('Increment a version.');
$this->setHelp('This command increments the provided version by the specified part (major, minor, patch).');
$this->setAliases(['incr', 'bump']);
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch)', '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;
default:
throw new InvalidArgumentException(sprintf("Invalid part '%s' provided. Expected 'major', 'minor', or 'patch'.", $part));
}
if (is_string($pre) && $pre !== '') {
$version = $version->withPreRelease($pre);
}
if (is_string($build) && $build !== '') {
$version = $version->withBuild($build);
}
return $version;
}
}