-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathExportPackagesAsJsonCommand.php
More file actions
77 lines (61 loc) · 2.12 KB
/
ExportPackagesAsJsonCommand.php
File metadata and controls
77 lines (61 loc) · 2.12 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
<?php
declare(strict_types=1);
namespace EonX\EasyMonorepo\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use UnexpectedValueException;
#[AsCommand(
name: 'export-packages'
)]
final class ExportPackagesAsJsonCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composerFiles = $this->getComposerJsonFiles();
$packages = [];
foreach ($composerFiles as $composerFile) {
$repoName = $this->getRepoShortname($composerFile);
$packages[$repoName] = [
'dir' => $this->getDir($composerFile),
'repo' => $repoName,
];
}
\ksort($packages);
// Remove keys
$noKeys = [];
foreach ($packages as $package) {
$noKeys[] = $package;
}
$output->write((string)\json_encode($noKeys));
return 0;
}
private function getComposerJsonFiles(): Finder
{
return new Finder()
->in([__DIR__ . '/../../packages'])
->name('composer.json');
}
private function getDir(SplFileInfo $composerJson): string
{
/** @var string $dir */
$dir = \last(\explode('/', $composerJson->getRelativePath()));
return $dir;
}
private function getRepoShortname(SplFileInfo $composerJson): string
{
$json = \json_decode($composerJson->getContents(), true);
if (\is_array($json) === false) {
throw new UnexpectedValueException('Invalid ' . $composerJson->getRealPath() . ' content.');
}
if (isset($json['name']) === false || \is_string($json['name']) === false) {
throw new UnexpectedValueException(
'Missing or invalid `name` in ' . $composerJson->getRealPath() . ' content.'
);
}
return \str_replace('eonx-com/', '', $json['name']);
}
}