1 | <?php |
||
2 | /** |
||
3 | * @category Library |
||
4 | * @license MIT http://opensource.org/licenses/MIT |
||
5 | * @link https://github.com/emlynwest/changelog |
||
6 | */ |
||
7 | |||
8 | namespace ChangeLog\Console; |
||
9 | |||
10 | use Symfony\Component\Console\Command\Command; |
||
11 | use Symfony\Component\Console\Input\InputInterface; |
||
12 | use Symfony\Component\Console\Input\InputOption; |
||
13 | use Symfony\Component\Console\Output\OutputInterface; |
||
14 | |||
15 | /** |
||
16 | * Publishes a new release. |
||
17 | */ |
||
18 | class Release extends AbstractCommand |
||
19 | { |
||
20 | /** |
||
21 | * @return string |
||
22 | * @codeCoverageIgnore |
||
23 | */ |
||
24 | public function getDescription(): string |
||
25 | { |
||
26 | return 'Publishes the unreleased version as a released version.'; |
||
27 | } |
||
28 | |||
29 | 6 | protected function configure() |
|
30 | { |
||
31 | 6 | parent::configure(); |
|
32 | |||
33 | 6 | $this->addArgument( |
|
34 | 6 | 'release', |
|
35 | 6 | InputOption::VALUE_REQUIRED, |
|
36 | 6 | 'New release number, can be any valid semver or [major|minor|patch]' |
|
37 | 6 | ); |
|
38 | 6 | $this->addOption( |
|
39 | 6 | 'link', |
|
40 | 6 | null, |
|
41 | 6 | InputOption::VALUE_REQUIRED, |
|
42 | 6 | 'Optional link to the release\'s info page' |
|
43 | 6 | ); |
|
44 | 6 | $this->addOption( |
|
45 | 6 | 'linkName', |
|
46 | 6 | null, |
|
47 | 6 | InputOption::VALUE_REQUIRED, |
|
48 | 6 | 'Optional link name, release name will be used if not specified' |
|
49 | 6 | ); |
|
50 | } |
||
51 | |||
52 | 6 | public function execute(InputInterface $input, OutputInterface $output): int |
|
53 | { |
||
54 | 6 | parent::execute($input, $output); |
|
55 | |||
56 | 6 | $releaseName = $input->getArgument('release'); |
|
57 | |||
58 | 6 | if ($releaseName === null) { |
|
59 | 1 | throw new InvalidArgumentException('A release name is required'); |
|
60 | } |
||
61 | |||
62 | 5 | $log = $this->changeLog->parse(); |
|
63 | |||
64 | // Don't continue if there is no unreleased version |
||
65 | 5 | if ( ! $log->hasRelease('unreleased')) { |
|
66 | 1 | throw new ReleaseNotFoundException('Unable to find an unreleased version.'); |
|
67 | } |
||
68 | |||
69 | // Work out what version number we actually want |
||
70 | 4 | $newReleaseName = $log->getNextVersion($releaseName); |
|
71 | |||
72 | // Don't continue if the release already exists |
||
73 | 4 | if ($log->hasRelease($newReleaseName)) { |
|
74 | 1 | throw new ReleaseExistsException('A release with the name "' . $newReleaseName . '" already exists."'); |
|
75 | } |
||
76 | |||
77 | 3 | $release = $log->getRelease('unreleased'); |
|
78 | 3 | $release->setName($newReleaseName); |
|
79 | |||
80 | 3 | $newLink = $input->getOption('link'); |
|
81 | |||
82 | 3 | if ($newLink === null) { |
|
83 | 1 | $release->setLink(null); |
|
84 | 1 | $release->setLinkName(null); |
|
85 | } else { |
||
86 | 2 | $newLinkName = $input->getOption('linkName'); |
|
87 | 2 | $newLinkName = $newLinkName === null ? $newReleaseName : $newLinkName; |
|
88 | |||
89 | 2 | $release->setLink($newLink); |
|
90 | 2 | $release->setLinkName($newLinkName); |
|
91 | } |
||
92 | |||
93 | // Remove and re-add the release to trigger the log internals |
||
94 | 3 | $log->addRelease($release); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
95 | 3 | $log->removeRelease('unreleased'); |
|
96 | |||
97 | 3 | $this->changeLog->write($log); |
|
98 | |||
99 | 3 | return Command::SUCCESS; |
|
100 | } |
||
101 | } |
||
102 |