Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogNextVersionCommand
100.00% covered (success)
100.00%
36 / 36
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%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
23 / 23
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\Log\LoggerInterface;
29use Symfony\Component\Console\Attribute\AsCommand;
30use Symfony\Component\Console\Input\InputInterface;
31use Symfony\Component\Console\Input\InputOption;
32use Symfony\Component\Console\Output\OutputInterface;
33
34/**
35 * Infers the next semantic version from changelog content.
36 */
37#[AsCommand(
38    name: 'changelog:next-version',
39    description: 'Infers the next semantic version from the Unreleased changelog section.',
40    help: 'This command inspects Unreleased changelog categories and prints the next semantic version inferred from the current changelog state.'
41)]
42 class ChangelogNextVersionCommand extends BaseCommand implements LoggerAwareCommandInterface
43{
44    use HasJsonOption;
45    use LogsCommandResults;
46
47    /**
48     * @param FilesystemInterface $filesystem
49     * @param ChangelogManagerInterface $changelogManager
50     * @param LoggerInterface $logger
51     */
52    public function __construct(
53        private  FilesystemInterface $filesystem,
54        private  ChangelogManagerInterface $changelogManager,
55        private  LoggerInterface $logger,
56    ) {
57        parent::__construct();
58    }
59
60    /**
61     * Configures version inference options.
62     */
63    protected function configure(): void
64    {
65        $this->addJsonOption()
66            ->addOption(
67                name: 'file',
68                mode: InputOption::VALUE_REQUIRED,
69                description: 'Path to the changelog file.',
70                default: 'CHANGELOG.md',
71            )
72            ->addOption(
73                name: 'current-version',
74                mode: InputOption::VALUE_REQUIRED,
75                description: 'Explicit current version to use as the bump base.',
76            );
77    }
78
79    /**
80     * Prints the inferred next semantic version.
81     *
82     * @param InputInterface $input
83     * @param OutputInterface $output
84     */
85    protected function execute(InputInterface $input, OutputInterface $output): int
86    {
87        try {
88            $path = $this->filesystem->getAbsolutePath($input->getOption('file'));
89            $currentVersion = $input->getOption('current-version');
90            $nextVersion = $this->changelogManager->inferNextVersion($path, $currentVersion);
91
92            // This command is consumed via shell capture in changelog.yml, so
93            // plain-text mode MUST keep emitting the raw version string.
94            if (! $this->isJsonOutput($input)) {
95                $output->writeln($nextVersion);
96
97                return self::SUCCESS;
98            }
99
100            return $this->success(
101                $nextVersion,
102                $input,
103                [
104                    'current_version' => $currentVersion,
105                    'next_version' => $nextVersion,
106                ],
107            );
108        } catch (Throwable $throwable) {
109            return $this->failure(
110                'Unable to infer the next changelog version.',
111                $input,
112                [
113                    'exception_message' => $throwable->getMessage(),
114                ],
115                (string) $input->getOption('file'),
116            );
117        }
118    }
119}