forked from composer/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LicensesCommand.php
202 lines (175 loc) · 7.36 KB
/
LicensesCommand.php
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
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Command;
use Composer\Json\JsonFile;
use Composer\Package\CompletePackageInterface;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;
use Composer\Util\PackageInfo;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* @author Benoît Merlet <benoit.merlet@gmail.com>
*/
class LicensesCommand extends BaseCommand
{
/**
* @return void
*/
protected function configure(): void
{
$this
->setName('licenses')
->setDescription('Shows information about licenses of dependencies.')
->setDefinition(array(
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text, json or summary', 'text'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'),
))
->setHelp(
<<<EOT
The license command displays detailed information about the licenses of
the installed dependencies.
Read more at https://getcomposer.org/doc/03-cli.md#licenses
EOT
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = $this->requireComposer();
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$root = $composer->getPackage();
$repo = $composer->getRepositoryManager()->getLocalRepository();
if ($input->getOption('no-dev')) {
$packages = $this->filterRequiredPackages($repo, $root);
} else {
$packages = $this->appendPackages($repo->getPackages(), array());
}
ksort($packages);
$io = $this->getIO();
switch ($format = $input->getOption('format')) {
case 'text':
$io->write('Name: <comment>'.$root->getPrettyName().'</comment>');
$io->write('Version: <comment>'.$root->getFullPrettyVersion().'</comment>');
$io->write('Licenses: <comment>'.(implode(', ', $root->getLicense()) ?: 'none').'</comment>');
$io->write('Dependencies:');
$io->write('');
$table = new Table($output);
$table->setStyle('compact');
$tableStyle = $table->getStyle();
$tableStyle->setVerticalBorderChars('');
$tableStyle->setCellRowContentFormat('%s ');
$table->setHeaders(array('Name', 'Version', 'Licenses'));
foreach ($packages as $package) {
$link = PackageInfo::getViewSourceOrHomepageUrl($package);
if ($link !== null) {
$name = '<href='.OutputFormatter::escape($link).'>'.$package->getPrettyName().'</>';
} else {
$name = $package->getPrettyName();
}
$table->addRow(array(
$name,
$package->getFullPrettyVersion(),
implode(', ', $package instanceof CompletePackageInterface ? $package->getLicense() : array()) ?: 'none',
));
}
$table->render();
break;
case 'json':
$dependencies = array();
foreach ($packages as $package) {
$dependencies[$package->getPrettyName()] = array(
'version' => $package->getFullPrettyVersion(),
'license' => $package instanceof CompletePackageInterface ? $package->getLicense() : array(),
);
}
$io->write(JsonFile::encode(array(
'name' => $root->getPrettyName(),
'version' => $root->getFullPrettyVersion(),
'license' => $root->getLicense(),
'dependencies' => $dependencies,
)));
break;
case 'summary':
$usedLicenses = array();
foreach ($packages as $package) {
$licenses = $package instanceof CompletePackageInterface ? $package->getLicense() : array();
if (count($licenses) === 0) {
$licenses[] = 'none';
}
foreach ($licenses as $licenseName) {
if (!isset($usedLicenses[$licenseName])) {
$usedLicenses[$licenseName] = 0;
}
$usedLicenses[$licenseName]++;
}
}
// Sort licenses so that the most used license will appear first
arsort($usedLicenses, SORT_NUMERIC);
$rows = array();
foreach ($usedLicenses as $usedLicense => $numberOfDependencies) {
$rows[] = array($usedLicense, $numberOfDependencies);
}
$symfonyIo = new SymfonyStyle($input, $output);
$symfonyIo->table(
array('License', 'Number of dependencies'),
$rows
);
break;
default:
throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format));
}
return 0;
}
/**
* Find package requires and child requires
*
* @param array<string, PackageInterface> $bucket
* @return array<string, PackageInterface>
*/
private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array()): array
{
$requires = array_keys($package->getRequires());
$packageListNames = array_keys($bucket);
$packages = array_filter(
$repo->getPackages(),
function ($package) use ($requires, $packageListNames): bool {
return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
}
);
$bucket = $this->appendPackages($packages, $bucket);
foreach ($packages as $package) {
$bucket = $this->filterRequiredPackages($repo, $package, $bucket);
}
return $bucket;
}
/**
* Adds packages to the package list
*
* @param PackageInterface[] $packages the list of packages to add
* @param array<string, PackageInterface> $bucket the list to add packages to
* @return array<string, PackageInterface>
*/
public function appendPackages(array $packages, array $bucket): array
{
foreach ($packages as $package) {
$bucket[$package->getName()] = $package;
}
return $bucket;
}
}