|
1 | | -#!/usr/bin/perl -w |
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
2 | 3 |
|
3 | | -use strict; |
4 | | -use List::Util qw(max); |
| 4 | +$prod = diff('packages'); |
| 5 | +$dev = diff('packages-dev'); |
5 | 6 |
|
6 | | -my $JQ_FILTER = "jq -r '.packages[] | \"\\(.name) \\(.version)\"'"; |
7 | | -my $JQ_FILTER_DEV = "sed 's/packages-dev/packages_dev/g' | jq -r '.packages_dev[] | \"\\(.name) \\(.version)\"'"; |
| 7 | +if (hasOpt('json')) { |
| 8 | + $opts = (hasOpt('pretty')) ? JSON_PRETTY_PRINT : 0; |
| 9 | + print json_encode(array('changes' => $prod, 'changes-dev' => $dev), $opts); |
| 10 | + return; |
| 11 | +} |
| 12 | + |
| 13 | +print tableize('Production Changes', $prod); |
| 14 | +print tableize('Dev Changes', $dev); |
8 | 15 |
|
9 | | -print "production packages\n===================\n"; |
10 | | -print_pkgs(diff($JQ_FILTER)); |
| 16 | +function diff($key) { |
11 | 17 |
|
12 | | -print "\ndev packages\n============\n"; |
13 | | -print_pkgs(diff($JQ_FILTER_DEV)); |
| 18 | + $pkgs = array(); |
14 | 19 |
|
15 | | -sub diff { |
16 | | - my $jq_filter = shift; |
17 | | - my %pkgs; |
18 | | - my $fh; |
| 20 | + $lines = ''; |
| 21 | + exec('git show HEAD:composer.lock', $lines); |
| 22 | + $data = json_decode(implode("\n", $lines)); |
19 | 23 |
|
20 | | - open($fh, "-|", "git show HEAD:composer.lock | $jq_filter | sort") or die "Failed to open before command for reading; $!"; |
21 | | - while(<$fh>) { |
22 | | - next unless /^(\S+) (\S+)$/; |
23 | | - $pkgs{$1} = [$2, "REMOVED"]; |
| 24 | + foreach($data->$key as $pkg) { |
| 25 | + $pkgs[$pkg->name] = array($pkg->version, 'REMOVED'); |
24 | 26 | } |
25 | | - close $fh; |
26 | | - |
27 | | - open($fh, "-|", "cat composer.lock | $jq_filter | sort") or die "Failed to open before command for reading; $!"; |
28 | | - while(<$fh>) { |
29 | | - next unless /^(\S+) (\S+)$/; |
30 | | - $pkgs{$1} = ["NEW", 0] unless defined $pkgs{$1}; |
31 | | - if ($pkgs{$1}[0] eq $2) { |
32 | | - delete $pkgs{$1}; |
| 27 | + |
| 28 | + $data = json_decode(file_get_contents('composer.lock')); |
| 29 | + |
| 30 | + foreach($data->$key as $pkg) { |
| 31 | + if (! array_key_exists($pkg->name, $pkgs)) { |
| 32 | + $pkgs[$pkg->name] = array('NEW', $pkg->version); |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + if ($pkgs[$pkg->name][0] == $pkg->version) { |
| 37 | + unset($pkgs[$pkg->name]); |
33 | 38 | } else { |
34 | | - $pkgs{$1}[1] = $2; |
| 39 | + $pkgs[$pkg->name][1] = $pkg->version; |
35 | 40 | } |
36 | 41 | } |
37 | | - close $fh; |
38 | 42 |
|
39 | | - return \%pkgs; |
| 43 | + return $pkgs; |
| 44 | +} |
| 45 | + |
| 46 | +function hasOpt($opt) { |
| 47 | + global $argv; |
| 48 | + $prefix = strlen($opt) === 1 ? '-' : '--'; |
| 49 | + return in_array($prefix.$opt, $argv); |
40 | 50 | } |
41 | 51 |
|
42 | | -sub print_pkgs { |
43 | | - my $pkgs = shift; |
44 | | - my $pkg_width = max_len(keys %$pkgs) + 1; |
45 | | - my $before_width = max_len(map { $pkgs->{$_}[0] } keys %$pkgs); |
46 | | - for my $k (keys %$pkgs) { |
47 | | - printf "%-${pkg_width}s %-${before_width}s => %s\n", $k, $pkgs->{$k}[0], $pkgs->{$k}[1]; |
| 52 | +function tableize($header, $data) { |
| 53 | + $widths = array(maxLength(array_merge(array($header), array_keys($data)))); |
| 54 | + |
| 55 | + for($i = 0; $i < count(reset($data)); $i++) { |
| 56 | + $widths[] = maxLength(array_map(function($k) use ($data, $i) { return $data[$k][$i]; }, array_keys($data))); |
48 | 57 | } |
| 58 | + |
| 59 | + $total_width = array_sum($widths) + (count($widths) * 3) + 1; |
| 60 | + |
| 61 | + $lines[] = '+' . str_repeat('-', $total_width - 2) . '+'; |
| 62 | + $lines[] = tabelizeLine(array($header, 'From', 'To'), $widths); |
| 63 | + $lines[] = '+' . str_repeat('-', $total_width - 2) . '+'; |
| 64 | + |
| 65 | + foreach($data as $key => $v) { |
| 66 | + $lines[] = tabelizeLine(array_merge(array($key), $v), $widths); |
| 67 | + } |
| 68 | + |
| 69 | + $lines[] = '+' . str_repeat('-', $total_width - 2) . '+'; |
| 70 | + |
| 71 | + return implode(PHP_EOL, $lines) . PHP_EOL; |
| 72 | +} |
| 73 | + |
| 74 | +function maxLength(array $array) { |
| 75 | + return max(array_map('strlen', $array)); |
49 | 76 | } |
50 | 77 |
|
51 | | -sub max_len { |
52 | | - return max map { length } @_; |
| 78 | +function tabelizeLine($data, $widths) { |
| 79 | + $fields = array(); |
| 80 | + $count = max(array(count($data), count($widths))); |
| 81 | + for($i = 0; $i < $count; $i++) { |
| 82 | + $value = ($i >= count($data)) ? '' : $data[$i]; |
| 83 | + $width = ($i >= count($widths)) ? strlen($value) : $widths[$i]; |
| 84 | + $fields[] = sprintf("%-{$width}s", $value); |
| 85 | + } |
| 86 | + return '| ' . implode(' | ', $fields) . ' |'; |
53 | 87 | } |
54 | 88 |
|
0 commit comments