Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogPromoteCommand
100.00% covered (success)
100.00%
40 / 40
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%
17 / 17
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 Composer\Command\BaseCommand;
25use FastForward\DevTools\Changelog\Manager\ChangelogManagerInterface;
26use FastForward\DevTools\Console\Input\HasJsonOption;
27use FastForward\DevTools\Filesystem\FilesystemInterface;
28use Psr\Clock\ClockInterface;
29use Psr\Log\LoggerInterface;
30use Symfony\Component\Console\Attribute\AsCommand;
31use Symfony\Component\Console\Input\InputArgument;
32use Symfony\Component\Console\Input\InputInterface;
33use Symfony\Component\Console\Input\InputOption;
34use Symfony\Component\Console\Output\OutputInterface;
35
36/**
37 * Promotes the Unreleased section into a published changelog version.
38 */
39#[AsCommand(
40    name: 'changelog:promote',
41    description: 'Promotes Unreleased entries into a published changelog version.',
42    help: 'This command moves the current Unreleased entries into a released version section, records the release date, and restores an empty Unreleased section.'
43)]
44 class ChangelogPromoteCommand extends BaseCommand implements LoggerAwareCommandInterface
45{
46    use HasJsonOption;
47    use LogsCommandResults;
48
49    /**
50     * @param FilesystemInterface $filesystem
51     * @param ChangelogManagerInterface $changelogManager
52     * @param ClockInterface $clock
53     * @param LoggerInterface $logger
54     */
55    public function __construct(
56        private  FilesystemInterface $filesystem,
57        private  ChangelogManagerInterface $changelogManager,
58        private  ClockInterface $clock,
59        private  LoggerInterface $logger,
60    ) {
61        parent::__construct();
62    }
63
64    /**
65     * Configures the promotion arguments and options.
66     */
67    protected function configure(): void
68    {
69        $this->addJsonOption()
70            ->addArgument(
71                name: 'version',
72                mode: InputArgument::REQUIRED,
73                description: 'The semantic version that should receive the current Unreleased entries.',
74            )
75            ->addOption(
76                name: 'date',
77                mode: InputOption::VALUE_REQUIRED,
78                description: 'The release date to record in YYYY-MM-DD format.',
79            )
80            ->addOption(
81                name: 'file',
82                mode: InputOption::VALUE_REQUIRED,
83                description: 'Path to the changelog file.',
84                default: 'CHANGELOG.md',
85            );
86    }
87
88    /**
89     * Promotes unreleased entries into the requested version section.
90     *
91     * @param InputInterface $input
92     * @param OutputInterface $output
93     */
94    protected function execute(InputInterface $input, OutputInterface $output): int
95    {
96        try {
97            $file = $this->filesystem->getAbsolutePath((string) $input->getOption('file'));
98            $version = (string) $input->getArgument('version');
99            $date = (string) ($input->getOption('date') ?: $this->clock->now()->format('Y-m-d'));
100
101            $this->changelogManager->promote($file, $version, $date);
102
103            return $this->success(
104                'Promoted Unreleased changelog entries to [{version}] in {absolute_file}.',
105                $input,
106                [
107                    'absolute_file' => $file,
108                    'version' => $version,
109                    'date' => $date,
110                ],
111            );
112        } catch (Throwable $throwable) {
113            return $this->failure(
114                'Unable to promote the changelog release.',
115                $input,
116                [
117                    'exception_message' => $throwable->getMessage(),
118                ],
119                (string) $input->getOption('file'),
120            );
121        }
122    }
123}