Skip to content

Commit 17406f2

Browse files
djonas-noipcweiske
authored andcommitted
Separate out direct dependencies
1 parent d8f6352 commit 17406f2

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

composer-lock-diff

+62-7
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ function diff($key, $data_from, $data_to) {
4747
$pkgs = array();
4848

4949
foreach($data_from->$key as $pkg) {
50-
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED', '');
50+
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED', '', array('direct' => property_exists($pkg, 'direct')));
5151
}
5252

5353
foreach($data_to->$key as $pkg) {
5454
if (! array_key_exists($pkg->name, $pkgs)) {
55-
$pkgs[$pkg->name] = array('NEW', version($pkg), '');
55+
$pkgs[$pkg->name] = array('NEW', version($pkg), '', array('direct' => property_exists($pkg, 'direct')));
5656
continue;
5757
}
5858

@@ -61,6 +61,9 @@ function diff($key, $data_from, $data_to) {
6161
} else {
6262
$pkgs[$pkg->name][1] = version($pkg);
6363
$pkgs[$pkg->name][2] = makeCompareUrl($pkg, $pkgs);
64+
if ($pkgs[$pkg->name][3]['direct'] === false) { // Don't overwrite direct if it was already set to true
65+
$pkgs[$pkg->name][3]['direct'] = property_exists($pkg, 'direct');
66+
}
6467
}
6568
}
6669

@@ -101,7 +104,7 @@ function tableize($header, $data, $opts = array()) {
101104

102105
$widths = array(maxLength(array_merge(array($header), array_keys($data))));
103106

104-
$count = count(reset($data));
107+
$count = 3; // it will always be 3. The fourth item is a properties array
105108
for($i = 0; $i < $count; $i++) {
106109
$widths[] = max(strlen($titles[$i + 1]), maxLength(array_map(function($k) use ($data, $i) { return $data[$k][$i]; }, array_keys($data))));
107110
}
@@ -113,8 +116,18 @@ function tableize($header, $data, $opts = array()) {
113116
$lines[] = tabelizeLine($titles, $widths);
114117
$lines[] = separatorLine($widths, $opts['joint']);
115118

119+
$lines[] = fillLine(array("Direct"), '~', $widths);
120+
121+
foreach($data as $key => $v) {
122+
if (! $v[3]['direct']) continue;
123+
$lines[] = tabelizeLine(array_merge(array($key), array_slice($v, 0, $count)), $widths);
124+
}
125+
126+
$lines[] = fillLine(array("Indirect"), '~', $widths);
127+
116128
foreach($data as $key => $v) {
117-
$lines[] = tabelizeLine(array_merge(array($key), $v), $widths);
129+
if ($v[3]['direct']) continue;
130+
$lines[] = tabelizeLine(array_merge(array($key), array_slice($v, 0, $count)), $widths);
118131
}
119132

120133
if ($opts['capped']) {
@@ -132,6 +145,19 @@ function maxLength(array $array) {
132145
return max(array_map('strlen', $array));
133146
}
134147

148+
function fillLine($data, $fill_char, $widths) {
149+
$count = count($data);
150+
for ($i = 0; $i < count($widths); $i++) {
151+
if ($i < $count) {
152+
$data[$i] = $fill_char . " " . $data[$i] . " " . str_repeat($fill_char, $widths[$i] - (strlen($data[$i]) + 3));
153+
} else {
154+
$data[$i] = str_repeat($fill_char, $widths[$i]);
155+
}
156+
}
157+
158+
return tabelizeLine($data, $widths);
159+
}
160+
135161
function tabelizeLine($data, $widths) {
136162
$fields = array();
137163
$count = max(array(count($data), count($widths)));
@@ -209,11 +235,20 @@ function loadFile($fileish, $base_path, $default_fileish) {
209235
}
210236

211237
// Is it a file in the local filesystem?
212-
if (file_exists($fileish)) {
213-
return array(mustDecodeJson(file_get_contents($fileish), $fileish), false);
238+
if (! file_exists($fileish)) {
239+
return array(false, "Candidate '$fileish' does not look loadable from the fs or php stream wrappers");
240+
}
241+
242+
$data = mustDecodeJson(file_get_contents($fileish), $fileish);
243+
244+
// Try to load composer.json and mark deps.
245+
if (substr($fileish, -4) == "lock") {
246+
$composer_json_fileish = substr($fileish, 0, -4) . 'json';
247+
$composer_json = mustDecodeJson(file_get_contents($composer_json_fileish), $composer_json_fileish);
248+
markDirectDependencies($data, $composer_json);
214249
}
215250

216-
return array(false, "Candidate '$fileish' does not look loadable from the fs or php stream wrappers");
251+
return array($data, false);
217252
}
218253

219254
function isUrl($string) {
@@ -231,6 +266,26 @@ function mustDecodeJson($json, $context) {
231266
return $data;
232267
}
233268

269+
function markDirectDependencies($lock, $json) {
270+
foreach (array('', '-dev') as $ext) {
271+
foreach ($json->{'require'.$ext} as $pkg => $_) {
272+
$packages = 'packages'.$ext;
273+
274+
$index = false;
275+
for($i = 0; $i < count($lock->{$packages}); $i++) {
276+
if ($lock->{$packages}[$i]->name == $pkg) {
277+
$index = $i;
278+
break;
279+
}
280+
}
281+
282+
if ($index !== false) {
283+
$lock->{$packages}[$i]->direct = true;
284+
}
285+
}
286+
}
287+
}
288+
234289
function makeCompareUrl($pkg, $diff) {
235290
$func = 'formatCompare' . ucfirst(getSourceRepoType((string) @$pkg->source->url));
236291
return call_user_func($func, @$pkg->source->url, $diff[$pkg->name][0], $diff[$pkg->name][1]);

0 commit comments

Comments
 (0)