-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicenseCommand.php
More file actions
239 lines (216 loc) · 7.86 KB
/
LicenseCommand.php
File metadata and controls
239 lines (216 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Command;
use Composer\Command\BaseCommand;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use FastForward\DevTools\Console\Input\HasJsonOption;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\License\GeneratorInterface;
use FastForward\DevTools\Resource\FileDiffer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Generates and copies LICENSE files to projects.
*
* This command generates a LICENSE file if one does not exist and a supported
* license is declared in composer.json.
*/
#[AsCommand(
name: 'license',
description: 'Generates a LICENSE file from composer.json license information.'
)]
final class LicenseCommand extends BaseCommand implements LoggerAwareCommandInterface
{
use HasJsonOption;
use LogsCommandResults;
/**
* Creates a new LicenseCommand instance.
*
* @param GeneratorInterface $generator the generator component
* @param FilesystemInterface $filesystem the filesystem component
* @param FileDiffer $fileDiffer
* @param LoggerInterface $logger the output-aware logger
*/
public function __construct(
private readonly GeneratorInterface $generator,
private readonly FilesystemInterface $filesystem,
private readonly FileDiffer $fileDiffer,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this->setHelp(
'This command generates a LICENSE file if one does not exist and a supported license is declared in'
. ' composer.json.'
);
$this->addJsonOption()
->addOption(
name: 'target',
mode: InputOption::VALUE_OPTIONAL,
description: 'The target path for the generated LICENSE file.',
default: 'LICENSE',
)
->addOption(
name: 'dry-run',
mode: InputOption::VALUE_NONE,
description: 'Preview LICENSE generation without writing the file.',
)
->addOption(
name: 'check',
mode: InputOption::VALUE_NONE,
description: 'Report LICENSE drift and exit non-zero when changes are required.',
)
->addOption(
name: 'interactive',
mode: InputOption::VALUE_NONE,
description: 'Prompt before writing LICENSE changes.',
);
}
/**
* Executes the license generation process.
*
* Generates a LICENSE file if one does not exist and a supported license is declared in composer.json.
*
* @param InputInterface $input the input interface
* @param OutputInterface $output the output interface
*
* @return int the status code
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$targetPath = $this->filesystem->getAbsolutePath($input->getOption('target'));
$dryRun = (bool) $input->getOption('dry-run');
$check = (bool) $input->getOption('check');
$interactive = (bool) $input->getOption('interactive');
$existingContent = $this->filesystem->exists($targetPath) ? $this->filesystem->readFile($targetPath) : null;
$generatedContent = $this->generator->generateContent();
if (null === $generatedContent) {
$this->notice(
'No supported license found in composer.json or license is unsupported. Skipping LICENSE generation.',
$input,
[
'target_path' => $targetPath,
],
);
return $this->success(
'LICENSE generation was skipped because no supported license metadata was available.',
$input,
[
'target_path' => $targetPath,
],
'notice',
);
}
$comparison = $this->fileDiffer->diffContents(
'generated LICENSE content',
$targetPath,
$generatedContent,
$existingContent,
null === $existingContent
? \sprintf('Managed file %s will be created from generated LICENSE content.', $targetPath)
: \sprintf('Updating managed file %s from generated LICENSE content.', $targetPath),
);
$this->logger->notice($comparison->getSummary(), [
'input' => $input,
'target_path' => $targetPath,
]);
if ($comparison->isChanged()) {
$consoleDiff = $this->fileDiffer->formatForConsole($comparison->getDiff(), $output->isDecorated());
if (null !== $consoleDiff) {
$this->logger->notice(
$consoleDiff,
[
'input' => $input,
'target_path' => $targetPath,
'diff' => $comparison->getDiff(),
],
);
}
}
if ($comparison->isUnchanged()) {
return $this->success(
'LICENSE already matches the generated content.',
$input,
[
'target_path' => $targetPath,
],
);
}
if ($check) {
return $this->failure(
'LICENSE requires synchronization updates.',
$input,
[
'target_path' => $targetPath,
],
$targetPath,
);
}
if ($dryRun) {
return $this->success(
'LICENSE generation preview completed.',
$input,
[
'target_path' => $targetPath,
],
'notice',
);
}
if ($interactive && $input->isInteractive() && ! $this->shouldWriteLicense($targetPath)) {
$this->notice('Skipped updating {target_path}.', $input, [
'target_path' => $targetPath,
]);
return $this->success(
'LICENSE generation was skipped.',
$input,
[
'target_path' => $targetPath,
],
'notice',
);
}
$this->filesystem->dumpFile($targetPath, $generatedContent);
return $this->success(
'{file_name} file generated successfully at {target_path}.',
$input,
[
'file_name' => basename($targetPath),
'target_path' => $targetPath,
],
);
}
/**
* Prompts whether the generated LICENSE should be written.
*
* @param string $targetPath the license path that would be written
*
* @return bool true when the write SHOULD proceed
*/
private function shouldWriteLicense(string $targetPath): bool
{
return $this->getIO()
->askConfirmation(\sprintf('Write managed file %s? [y/N] ', $targetPath), false);
}
}