1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dominikb\ComposerLicenseChecker; |
6
|
|
|
|
7
|
|
|
use Dominikb\ComposerLicenseChecker\Traits\DependencyLoaderAwareTrait; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
class InitCommand extends Command |
16
|
|
|
{ |
17
|
|
|
use DependencyLoaderAwareTrait; |
18
|
|
|
|
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this->setDefinition(new InputDefinition([ |
22
|
|
|
new InputOption( |
23
|
|
|
'project-path', |
24
|
|
|
'p', |
25
|
|
|
InputOption::VALUE_OPTIONAL, |
26
|
|
|
'Path to directory of composer.json file', |
27
|
|
|
realpath('.') |
28
|
|
|
), |
29
|
|
|
new InputOption( |
30
|
|
|
'composer', |
31
|
|
|
'c', |
32
|
|
|
InputOption::VALUE_OPTIONAL, |
33
|
|
|
'Path to composer executable', |
34
|
|
|
'composer' |
35
|
|
|
), |
36
|
|
|
new InputOption( |
37
|
|
|
'no-dev', |
38
|
|
|
null, |
39
|
|
|
InputOption::VALUE_OPTIONAL, |
40
|
|
|
'Do not include dev dependencies', |
41
|
|
|
'false' |
42
|
|
|
), |
43
|
|
|
new InputOption( |
44
|
|
|
'output', |
45
|
|
|
'o', |
46
|
|
|
InputOption::VALUE_OPTIONAL, |
47
|
|
|
'Path to file to write the used licenses to', |
48
|
|
|
'allowlist.txt' |
49
|
|
|
), |
50
|
|
|
new InputOption( |
51
|
|
|
'force', |
52
|
|
|
'-f', |
53
|
|
|
InputOption::VALUE_NONE, |
54
|
|
|
'Ignore any existing allowlist file and potentially overwrite it with new content', |
55
|
|
|
), |
56
|
|
|
])); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function getDefaultName(): ?string |
60
|
|
|
{ |
61
|
|
|
return 'init'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function getDefaultDescription(): ?string |
65
|
|
|
{ |
66
|
|
|
return 'Generate a list of all licenses used in the project'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
70
|
|
|
{ |
71
|
|
|
$io = new SymfonyStyle($input, $output); |
72
|
|
|
$io->title('Generating list of all licenses used in the project...'); |
73
|
|
|
|
74
|
|
|
if (! $this->canWriteToOutfile($input, $io)) { |
75
|
|
|
return Command::FAILURE; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$dependencies = $this->dependencyLoader->loadDependencies( |
|
|
|
|
79
|
|
|
$input->getOption('composer'), |
80
|
|
|
$input->getOption('project-path'), |
81
|
|
|
($input->getOption('no-dev') ?? 'true') === 'true' |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$output = join(PHP_EOL, $this->extractLicenses($dependencies)).PHP_EOL; |
85
|
|
|
$io->block($output); |
86
|
|
|
|
87
|
|
|
if (! file_put_contents($input->getOption('output'), $output)) { |
88
|
|
|
$io->error("Failed to write to '".$input->getOption('output')."'."); |
89
|
|
|
|
90
|
|
|
return Command::FAILURE; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$io->success("List of used licenses written to '".$input->getOption('output')."'."); |
94
|
|
|
|
95
|
|
|
return Command::SUCCESS; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Dependency[] $dependencies |
100
|
|
|
* @return string[] |
101
|
|
|
*/ |
102
|
|
|
private function extractLicenses(array $dependencies): array |
103
|
|
|
{ |
104
|
|
|
$licenses = []; |
105
|
|
|
foreach ($dependencies as $dependency) { |
106
|
|
|
$licenses[] = $dependency->getLicenses(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$uniqueLicenses = array_values(array_unique(array_flatten($licenses))); |
110
|
|
|
sort($uniqueLicenses); |
111
|
|
|
|
112
|
|
|
return $uniqueLicenses; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param InputInterface $input |
117
|
|
|
* @param SymfonyStyle $io |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
protected function canWriteToOutfile(InputInterface $input, SymfonyStyle $io): bool |
121
|
|
|
{ |
122
|
|
|
if ($input->getOption('force')) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (! file_exists($input->getOption('output'))) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$io->warning('File '.$input->getOption('output').' already exists.'); |
131
|
|
|
|
132
|
|
|
return $io->confirm('Overwrite existing allowlist?', false); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.