Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogShowCommand
100.00% covered (success)
100.00%
43 / 43
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%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
26 / 26
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\Log\LoggerInterface;
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 * Prints the rendered notes body for a released changelog version.
37 */
38#[AsCommand(name: 'changelog:show', description: 'Prints the notes body for a released changelog version.')]
39 class ChangelogShowCommand extends Command
40{
41    use HasJsonOption;
42    use LogsCommandResults;
43
44    /**
45     * @param FilesystemInterface $filesystem
46     * @param ChangelogManagerInterface $changelogManager
47     * @param LoggerInterface $logger
48     */
49    public function __construct(
50        private  FilesystemInterface $filesystem,
51        private  ChangelogManagerInterface $changelogManager,
52        private  LoggerInterface $logger,
53    ) {
54        parent::__construct();
55    }
56
57    /**
58     * Configures the show command arguments and options.
59     */
60    protected function configure(): void
61    {
62        $this->setHelp(
63            'This command renders the body of one released changelog section so it can be reused for GitHub'
64            . ' release notes.'
65        );
66
67        $this->addJsonOption()
68            ->addArgument(
69                name: 'version',
70                mode: InputArgument::REQUIRED,
71                description: 'The released version to render.',
72            )
73            ->addOption(
74                name: 'file',
75                mode: InputOption::VALUE_REQUIRED,
76                description: 'Path to the changelog file.',
77                default: 'CHANGELOG.md',
78            );
79    }
80
81    /**
82     * Prints the rendered release notes body.
83     *
84     * @param InputInterface $input
85     * @param OutputInterface $output
86     */
87    protected function execute(InputInterface $input, OutputInterface $output): int
88    {
89        try {
90            $version = (string) $input->getArgument('version');
91            $file = (string) $input->getOption('file');
92            $releaseNotes = $this->changelogManager->renderReleaseNotes(
93                $this->filesystem->getAbsolutePath($file),
94                $version,
95            );
96
97            // This command feeds redirected release-notes files in changelog.yml,
98            // so plain-text mode MUST keep writing the rendered body directly.
99            if (! $this->isJsonOutput($input)) {
100                $output->write($releaseNotes);
101
102                return self::SUCCESS;
103            }
104
105            return $this->success(
106                $releaseNotes,
107                $input,
108                [
109                    'version' => $version,
110                    'release_notes' => $releaseNotes,
111                ],
112            );
113        } catch (Throwable $throwable) {
114            return $this->failure(
115                'Unable to render changelog release notes.',
116                $input,
117                [
118                    'exception_message' => $throwable->getMessage(),
119                ],
120                (string) $input->getOption('file'),
121            );
122        }
123    }
124}