-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFundingCommand.php
More file actions
507 lines (455 loc) · 17.1 KB
/
FundingCommand.php
File metadata and controls
507 lines (455 loc) · 17.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?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 Composer\Factory;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use FastForward\DevTools\Console\Input\HasJsonOption;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\Funding\ComposerFundingCodec;
use FastForward\DevTools\Funding\FundingProfileMerger;
use FastForward\DevTools\Funding\FundingYamlCodec;
use FastForward\DevTools\Process\ProcessBuilderInterface;
use FastForward\DevTools\Process\ProcessQueueInterface;
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;
/**
* Synchronizes funding metadata between composer.json and .github/FUNDING.yml.
*/
#[AsCommand(
name: 'funding',
description: 'Synchronizes funding metadata between composer.json and .github/FUNDING.yml.'
)]
final class FundingCommand extends BaseCommand implements LoggerAwareCommandInterface
{
use HasJsonOption;
use LogsCommandResults;
/**
* Creates a new FundingCommand instance.
*
* @param FilesystemInterface $filesystem the filesystem used to read and write funding metadata files
* @param ComposerFundingCodec $composerFundingCodec the codec used to parse and render Composer funding metadata
* @param FundingYamlCodec $fundingYamlCodec the codec used to parse and render GitHub funding YAML metadata
* @param FundingProfileMerger $fundingProfileMerger the merger used to synchronize normalized funding profiles
* @param FileDiffer $fileDiffer the differ used to summarize managed-file drift
* @param ProcessBuilderInterface $processBuilder the process builder used to normalize composer.json after updates
* @param ProcessQueueInterface $processQueue the process queue used to execute composer normalize
* @param LoggerInterface $logger the output-aware logger
*/
public function __construct(
private readonly FilesystemInterface $filesystem,
private readonly ComposerFundingCodec $composerFundingCodec,
private readonly FundingYamlCodec $fundingYamlCodec,
private readonly FundingProfileMerger $fundingProfileMerger,
private readonly FileDiffer $fileDiffer,
private readonly ProcessBuilderInterface $processBuilder,
private readonly ProcessQueueInterface $processQueue,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
/**
* Configures command options.
*/
protected function configure(): void
{
$this->setHelp(
'This command merges supported funding entries across composer.json and .github/FUNDING.yml while'
. ' preserving unsupported providers.'
);
$this->addJsonOption()
->addOption(
name: 'composer-file',
mode: InputOption::VALUE_OPTIONAL,
description: 'Path to the composer.json file to synchronize.',
default: Factory::getComposerFile(),
)
->addOption(
name: 'funding-file',
mode: InputOption::VALUE_OPTIONAL,
description: 'Path to the .github/FUNDING.yml file to synchronize.',
default: '.github/FUNDING.yml',
)
->addOption(
name: 'dry-run',
mode: InputOption::VALUE_NONE,
description: 'Preview funding metadata synchronization without writing files.',
)
->addOption(
name: 'check',
mode: InputOption::VALUE_NONE,
description: 'Report funding metadata drift and exit non-zero when updates are required.',
)
->addOption(
name: 'interactive',
mode: InputOption::VALUE_NONE,
description: 'Prompt before applying funding metadata updates.',
);
}
/**
* Synchronizes funding metadata across Composer and GitHub files.
*
* @param InputInterface $input the command input
* @param OutputInterface $output the command output
*
* @return int the command status code
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composerFile = (string) $input->getOption('composer-file');
$fundingFile = (string) $input->getOption('funding-file');
$dryRun = (bool) $input->getOption('dry-run');
$check = (bool) $input->getOption('check');
$interactive = (bool) $input->getOption('interactive');
$this->logger->info('Synchronizing funding metadata...', [
'input' => $input,
]);
if (! $this->filesystem->exists($composerFile)) {
$this->notice(
'Composer file {composer_file} does not exist. Skipping funding synchronization.',
$input,
[
'composer_file' => $composerFile,
'funding_file' => $fundingFile,
],
);
return $this->success(
'Funding synchronization was skipped because composer.json was not found.',
$input,
[
'composer_file' => $composerFile,
'funding_file' => $fundingFile,
],
'notice',
);
}
$composerContents = $this->filesystem->readFile($composerFile);
$fundingContents = $this->filesystem->exists($fundingFile) ? $this->filesystem->readFile($fundingFile) : null;
$mergedProfile = $this->fundingProfileMerger->merge(
$this->composerFundingCodec->parse($composerContents),
$this->fundingYamlCodec->parse($fundingContents),
);
$updatedComposerContents = $this->composerFundingCodec->dump($composerContents, $mergedProfile);
$updatedFundingContents = $mergedProfile->hasYamlContent()
? $this->fundingYamlCodec->dump($mergedProfile)
: null;
$composerStatus = $this->handleComposerFile(
$composerFile,
$composerContents,
$updatedComposerContents,
$dryRun,
$check,
$interactive,
$input,
$output,
);
$fundingStatus = $this->handleFundingFile(
$fundingFile,
$fundingContents,
$updatedFundingContents,
$dryRun,
$check,
$interactive,
$input,
$output,
);
return max($composerStatus, $fundingStatus);
}
/**
* Handles composer.json synchronization reporting and writes.
*
* @param string $composerFile
* @param string $composerContents
* @param string $updatedComposerContents
* @param bool $dryRun
* @param bool $check
* @param bool $interactive
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int the command status code
*/
private function handleComposerFile(
string $composerFile,
string $composerContents,
string $updatedComposerContents,
bool $dryRun,
bool $check,
bool $interactive,
InputInterface $input,
OutputInterface $output,
): int {
$comparison = $this->fileDiffer->diffContents(
'generated funding metadata synchronization',
$composerFile,
$updatedComposerContents,
$composerContents,
\sprintf('Updating managed file %s from generated funding metadata synchronization.', $composerFile),
);
$this->logger->notice(
$comparison->getSummary(),
[
'input' => $input,
'composer_file' => $composerFile,
],
);
if ($comparison->isChanged()) {
$consoleDiff = $this->fileDiffer->formatForConsole($comparison->getDiff(), $output->isDecorated());
if (null !== $consoleDiff) {
$this->logger->notice(
$consoleDiff,
[
'input' => $input,
'composer_file' => $composerFile,
'diff' => $comparison->getDiff(),
],
);
}
}
if ($comparison->isUnchanged()) {
return $this->success(
'{composer_file} already matches the synchronized funding metadata.',
$input,
[
'composer_file' => $composerFile,
],
);
}
if ($check) {
return $this->failure(
'{composer_file} requires synchronized funding metadata updates.',
$input,
[
'composer_file' => $composerFile,
],
$composerFile,
);
}
if ($dryRun) {
return $this->success(
'Funding synchronization preview completed for {composer_file}.',
$input,
[
'composer_file' => $composerFile,
],
'notice',
);
}
if ($interactive && $input->isInteractive() && ! $this->shouldWriteManagedFile($composerFile)) {
$this->notice('Skipped updating {composer_file}.', $input, [
'composer_file' => $composerFile,
]);
return $this->success(
'Funding synchronization was skipped for {composer_file}.',
$input,
[
'composer_file' => $composerFile,
],
'notice',
);
}
$this->filesystem->dumpFile($composerFile, $updatedComposerContents);
if (self::SUCCESS !== $this->normalizeComposerFile($composerFile, $output)) {
return $this->failure(
'Composer normalization failed after updating {composer_file}.',
$input,
[
'composer_file' => $composerFile,
],
$composerFile,
);
}
return $this->success(
'Updated funding metadata in {composer_file}.',
$input,
[
'composer_file' => $composerFile,
],
);
}
/**
* Handles .github/FUNDING.yml synchronization reporting and writes.
*
* @param string $fundingFile
* @param ?string $currentFundingContents
* @param ?string $updatedFundingContents
* @param bool $dryRun
* @param bool $check
* @param bool $interactive
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int the command status code
*/
private function handleFundingFile(
string $fundingFile,
?string $currentFundingContents,
?string $updatedFundingContents,
bool $dryRun,
bool $check,
bool $interactive,
InputInterface $input,
OutputInterface $output,
): int {
if (null === $updatedFundingContents && null === $currentFundingContents) {
$this->notice(
'No supported funding metadata found. Skipping .github/FUNDING.yml synchronization.',
$input,
[
'funding_file' => $fundingFile,
],
);
return $this->success(
'Funding synchronization found no supported GitHub funding metadata to write.',
$input,
[
'funding_file' => $fundingFile,
],
'notice',
);
}
if (null === $updatedFundingContents) {
return $this->success(
'No GitHub funding file changes were required.',
$input,
[
'funding_file' => $fundingFile,
],
);
}
$comparison = $this->fileDiffer->diffContents(
'generated funding metadata synchronization',
$fundingFile,
$updatedFundingContents,
$currentFundingContents,
null === $currentFundingContents
? \sprintf(
'Managed file %s will be created from generated funding metadata synchronization.',
$fundingFile
)
: \sprintf('Updating managed file %s from generated funding metadata synchronization.', $fundingFile),
);
$this->logger->notice($comparison->getSummary(), [
'input' => $input,
'funding_file' => $fundingFile,
]);
if ($comparison->isChanged()) {
$consoleDiff = $this->fileDiffer->formatForConsole($comparison->getDiff(), $output->isDecorated());
if (null !== $consoleDiff) {
$this->logger->notice(
$consoleDiff,
[
'input' => $input,
'funding_file' => $fundingFile,
'diff' => $comparison->getDiff(),
],
);
}
}
if ($comparison->isUnchanged()) {
return $this->success(
'{funding_file} already matches the synchronized funding metadata.',
$input,
[
'funding_file' => $fundingFile,
],
);
}
if ($check) {
return $this->failure(
'{funding_file} requires synchronized funding metadata updates.',
$input,
[
'funding_file' => $fundingFile,
],
$fundingFile,
);
}
if ($dryRun) {
return $this->success(
'Funding synchronization preview completed for {funding_file}.',
$input,
[
'funding_file' => $fundingFile,
],
'notice',
);
}
if ($interactive && $input->isInteractive() && ! $this->shouldWriteManagedFile($fundingFile)) {
$this->notice('Skipped updating {funding_file}.', $input, [
'funding_file' => $fundingFile,
]);
return $this->success(
'Funding synchronization was skipped for {funding_file}.',
$input,
[
'funding_file' => $fundingFile,
],
'notice',
);
}
$this->filesystem->mkdir($this->filesystem->dirname($fundingFile));
$this->filesystem->dumpFile($fundingFile, $updatedFundingContents);
return $this->success(
'Updated funding metadata in {funding_file}.',
$input,
[
'funding_file' => $fundingFile,
],
);
}
/**
* Prompts whether a managed file should be written.
*
* @param string $targetFile
*
* @return bool true when the write SHOULD proceed
*/
private function shouldWriteManagedFile(string $targetFile): bool
{
return $this->getIO()
->askConfirmation(\sprintf('Update managed file %s? [y/N] ', $targetFile), false);
}
/**
* Normalizes a composer manifest after funding metadata changes.
*
* @param string $composerFile the composer manifest path
* @param OutputInterface $output the command output
*
* @return int the normalization status code
*/
private function normalizeComposerFile(string $composerFile, OutputInterface $output): int
{
$processBuilder = $this->processBuilder
->withArgument('--ansi')
->withArgument('--no-update-lock');
$workingDirectory = $this->filesystem->dirname($composerFile);
if ('.' !== $workingDirectory) {
$processBuilder = $processBuilder->withArgument('--working-dir', $workingDirectory);
}
$composerBasename = $this->filesystem->basename($composerFile);
if ('composer.json' !== $composerBasename) {
$processBuilder = $processBuilder->withArgument('--file', $composerBasename);
}
$this->processQueue->add($processBuilder->build('composer normalize'));
return $this->processQueue->run($output);
}
}