Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogEntryCommand
100.00% covered (success)
100.00%
52 / 52
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%
34 / 34
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
17 / 17
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 FastForward\DevTools\Changelog\Document\ChangelogDocument;
24use FastForward\DevTools\Changelog\Entry\ChangelogEntryType;
25use FastForward\DevTools\Changelog\Manager\ChangelogManagerInterface;
26use FastForward\DevTools\Console\Input\HasJsonOption;
27use FastForward\DevTools\Filesystem\FilesystemInterface;
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 * Inserts a changelog entry into the managed changelog file.
37 */
38#[AsCommand(
39    name: 'changelog:entry',
40    description: 'Adds a changelog entry to Unreleased or a specific version section.'
41)]
42 class ChangelogEntryCommand extends Command
43{
44    use HasJsonOption;
45    use LogsCommandResults;
46
47    /**
48     * @param FilesystemInterface $filesystem
49     * @param ChangelogManagerInterface $changelogManager
50     */
51    public function __construct(
52        private  FilesystemInterface $filesystem,
53        private  ChangelogManagerInterface $changelogManager,
54    ) {
55        parent::__construct();
56    }
57
58    /**
59     * Configures the entry authoring arguments and options.
60     */
61    protected function configure(): void
62    {
63        $this->setHelp(
64            'This command appends one categorized changelog entry to the selected changelog file so it can be'
65            . ' reused by local authoring flows and skills.'
66        );
67
68        $this->addJsonOption()
69            ->addArgument(
70                name: 'message',
71                mode: InputArgument::REQUIRED,
72                description: 'The changelog entry text to append.',
73            )
74            ->addOption(
75                name: 'type',
76                shortcut: 't',
77                mode: InputOption::VALUE_REQUIRED,
78                description: 'The changelog category (added, changed, deprecated, removed, fixed, security).',
79                default: 'added',
80            )
81            ->addOption(
82                name: 'release',
83                mode: InputOption::VALUE_REQUIRED,
84                description: 'The target release section. Defaults to Unreleased.',
85                default: ChangelogDocument::UNRELEASED_VERSION,
86            )
87            ->addOption(
88                name: 'date',
89                mode: InputOption::VALUE_REQUIRED,
90                description: 'Optional release date for published sections in YYYY-MM-DD format.',
91            )
92            ->addOption(
93                name: 'file',
94                mode: InputOption::VALUE_REQUIRED,
95                description: 'Path to the changelog file.',
96                default: 'CHANGELOG.md',
97            );
98    }
99
100    /**
101     * Appends a changelog entry to the requested section.
102     *
103     * @param InputInterface $input
104     * @param OutputInterface $output
105     */
106    protected function execute(InputInterface $input, OutputInterface $output): int
107    {
108        $file = $this->filesystem->getAbsolutePath((string) $input->getOption('file'));
109        $type = ChangelogEntryType::fromInput((string) $input->getOption('type'));
110        $version = (string) $input->getOption('release');
111        $date = $input->getOption('date');
112        $message = (string) $input->getArgument('message');
113
114        $this->changelogManager->addEntry($file, $type, $message, $version, \is_string($date) ? $date : null);
115
116        return $this->success(
117            'Added {type} changelog entry to [{release}] in {absolute_file}.',
118            $input,
119            [
120                'absolute_file' => $file,
121                'type' => strtolower($type->value),
122                'release' => $version,
123                'date' => \is_string($date) ? $date : null,
124                'message' => $message,
125            ],
126        );
127    }
128}