|
1 | 1 | #!/usr/bin/env php |
2 | 2 | <?php |
3 | 3 |
|
4 | | -print "production packages\n===================\n"; |
5 | | -print_pkgs(diff('packages')); |
| 4 | +$prod = diff('packages'); |
| 5 | +$dev = diff('packages-dev'); |
6 | 6 |
|
7 | | -print "\ndev packages\n============\n"; |
8 | | -print_pkgs(diff('packages-dev')); |
| 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); |
9 | 15 |
|
10 | 16 | function diff($key) { |
11 | 17 |
|
@@ -37,12 +43,46 @@ function diff($key) { |
37 | 43 | return $pkgs; |
38 | 44 | } |
39 | 45 |
|
40 | | -function print_pkgs($pkgs) { |
| 46 | +function hasOpt($opt) { |
| 47 | + global $argv; |
| 48 | + $prefix = strlen($opt) === 1 ? '-' : '--'; |
| 49 | + return in_array($prefix.$opt, $argv); |
| 50 | +} |
| 51 | + |
| 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))); |
| 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)); |
| 76 | +} |
41 | 77 |
|
42 | | - $pkg_width = max(array_map('strlen', array_keys($pkgs))) + 1; |
43 | | - $before_width = max(array_map('strlen', array_map(function($v) { return $v[0]; }, $pkgs))); |
44 | | - foreach($pkgs as $name => $v) { |
45 | | - printf("%-{$pkg_width}s %-{$before_width}s => %s\n", $name, $v[0], $v[1]); |
| 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); |
46 | 85 | } |
| 86 | + return '| ' . implode(' | ', $fields) . ' |'; |
47 | 87 | } |
48 | 88 |
|
0 commit comments