Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogPromoteCommand
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Command;
21
22use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
23use Throwable;
24use FastForward\DevTools\Changelog\Manager\ChangelogManagerInterface;
25use FastForward\DevTools\Console\Input\HasJsonOption;
26use FastForward\DevTools\Filesystem\FilesystemInterface;
27use Psr\Clock\ClockInterface;
28use Symfony\Component\Console\Attribute\AsCommand;
29use Symfony\Component\Console\Command\Command;
30use Symfony\Component\Console\Input\InputArgument;
31use Symfony\Component\Console\Input\InputInterface;
32use Symfony\Component\Console\Input\InputOption;
33use Symfony\Component\Console\Output\OutputInterface;
34
35/**
36 * Promotes the Unreleased section into a published changelog version.
37 */
38#[AsCommand(
39    name: 'changelog:promote',
40    description: 'Promotes Unreleased entries into a published changelog version.'
41)]
42 class ChangelogPromoteCommand extends Command
43{
44    use HasJsonOption;
45    use LogsCommandResults;
46
47    /**
48     * @param FilesystemInterface $filesystem
49     * @param ChangelogManagerInterface $changelogManager
50     * @param ClockInterface $clock
51     */
52    public function __construct(
53        private  FilesystemInterface $filesystem,
54        private  ChangelogManagerInterface $changelogManager,
55        private  ClockInterface $clock,
56    ) {
57        parent::__construct();
58    }
59
60    /**
61     * Configures the promotion arguments and options.
62     */
63    protected function configure(): void
64    {
65        $this->setHelp(
66            'This command moves the current Unreleased entries into a released version section, records the'
67            . ' release date, and restores an empty Unreleased section.'
68        );
69
70        $this->addJsonOption()
71            ->addArgument(
72                name: 'version',
73                mode: InputArgument::REQUIRED,
74                description: 'The semantic version that should receive the current Unreleased entries.',
75            )
76            ->addOption(
77                name: 'date',
78                mode: InputOption::VALUE_REQUIRED,
79                description: 'The release date to record in YYYY-MM-DD format.',
80            )
81            ->addOption(
82                name: 'file',
83                mode: InputOption::VALUE_REQUIRED,
84                description: 'Path to the changelog file.',
85                default: 'CHANGELOG.md',
86            );
87    }
88
89    /**
90     * Promotes unreleased entries into the requested version section.
91     *
92     * @param InputInterface $input
93     * @param OutputInterface $output
94     */
95    protected function execute(InputInterface $input, OutputInterface $output): int
96    {
97        try {
98            $file = $this->filesystem->getAbsolutePath((string) $input->getOption('file'));
99            $version = (string) $input->getArgument('version');
100            $date = (string) ($input->getOption('date') ?: $this->clock->now()->format('Y-m-d'));
101
102            $this->changelogManager->promote($file, $version, $date);
103
104            return $this->success(
105                'Promoted Unreleased changelog entries to [{version}] in {absolute_file}.',
106                $input,
107                [
108                    'absolute_file' => $file,
109                    'version' => $version,
110                    'date' => $date,
111                ],
112            );
113        } catch (Throwable $throwable) {
114            return $this->failure(
115                'Unable to promote the changelog release.',
116                $input,
117                [
118                    'exception_message' => $throwable->getMessage(),
119                ],
120                (string) $input->getOption('file'),
121            );
122        }
123    }
124}