-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyze.php
More file actions
171 lines (137 loc) · 4.42 KB
/
Analyze.php
File metadata and controls
171 lines (137 loc) · 4.42 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
<?php
namespace Phug\Split\Command;
use Phug\Split;
use Phug\Split\UnableToListDirectoryItems;
use SimpleCli\SimpleCli;
use Traversable;
/**
* Display the tree of nested packages in the mono-repository.
*/
class Analyze extends CommandBase
{
/**
* @argument
*
* Root project directory.
*
* @var string
*/
public $directory = '.';
/**
* @option c, composer-file
*
* Composer file name.
*
* @var string
*/
public $composerFile = 'composer.json';
/**
* Last AST of projects.
*
* @var ?iterable<array>
*/
protected $ast;
/**
* @param Split|SimpleCli $cli
*
* @return bool
*/
public function run(SimpleCli $cli): bool
{
$cli = $this->getSplitCli($cli);
return $this->calculatePackagesTree($cli) &&
$this->dumpPackagesTree($cli, $this->getPackages());
}
protected function calculatePackagesTree(Split $cli): bool
{
/** @psalm-var truthy-string|false $directory */
$directory = realpath($this->directory);
if (!$directory) {
return $cli->error('Input directory not found.');
}
$this->directory = $directory;
$cli->chdir($this->directory);
if (!file_exists($this->composerFile)) {
return $cli->error('Root project directory should contains a '.$this->composerFile.' file.');
}
$data = (array) json_decode((string) file_get_contents($this->composerFile), true);
$vendorDirectory = ($data['config'] ?? [])['vendor-dir'] ?? 'vendor';
$cli->writeLine((string) $data['name']);
$this->ast = $this->mapDirectories('.', function (string $path, string $element) use ($vendorDirectory) {
if ($element === $vendorDirectory) {
return null;
}
return $this->scanDirectories($path);
});
return true;
}
/** @psalm-suppress RiskyTruthyFalsyComparison */
protected function getPackages(): iterable
{
if ($this->ast instanceof Traversable) {
$this->ast = iterator_to_array($this->ast);
}
return $this->ast ?: [];
}
protected function dumpPackagesTree(Split $cli, iterable $packages, int $level = 0): bool
{
$count = is_countable($packages) ? count($packages) : 0;
foreach ($packages as $index => $package) {
$symbol = $index === $count - 1 ? '└' : '├';
$cli->writeLine(str_repeat(' │ ', $level).' '.$symbol.' '.$package['name'], 'light_cyan');
$this->dumpPackagesTree($cli, $package['children'], $level + 1);
}
return true;
}
protected function mapDirectories(string $directory, callable $callback): iterable
{
$elements = scandir($directory);
if ($elements === false) {
throw new UnableToListDirectoryItems($directory);
}
foreach ($elements as $element) {
if (substr($element, 0, 1) === '.') {
continue;
}
$path = $directory.DIRECTORY_SEPARATOR.$element;
if (is_dir($path)) {
$result = $callback($path, $element);
if ($result !== null) {
foreach ($result as $item) {
yield $item;
}
}
}
}
}
/** @return array{name: string, directory: string, children: array} */
protected function getPackage(string $directory, array $data): array
{
return [
'name' => $data['name'],
'directory' => $directory,
'children' => [],
];
}
protected function scanDirectories(string $directory): iterable
{
$mainPackage = null;
$composerPath = $directory.DIRECTORY_SEPARATOR.$this->composerFile;
if (file_exists($composerPath)) {
$data = json_decode((string) file_get_contents($composerPath), true);
$mainPackage = $this->getPackage($directory, $data);
}
foreach ($this->mapDirectories($directory, function (string $path) {
return $this->scanDirectories($path);
}) as $package) {
if ($mainPackage) {
$mainPackage['children'][] = $package;
continue;
}
yield $package;
}
if ($mainPackage) {
yield $mainPackage;
}
}
}