|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +use SebastianBergmann\Diff\Differ; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 6 | + |
| 7 | +$diffFile = $argv[1] ?? null; |
| 8 | +$oldDir = $argv[2] ?? null; |
| 9 | +$newDir = $argv[3] ?? null; |
| 10 | +if ($diffFile === null || !is_file($diffFile)) { |
| 11 | + exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +if ($oldDir === null || !is_dir($oldDir)) { |
| 15 | + exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +if ($newDir === null || !is_dir($newDir)) { |
| 19 | + exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +$diffLines = explode("\n", file_get_contents($diffFile)); |
| 23 | +$differ = new Differ(new \SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder('')); |
| 24 | +$isDifferent = false; |
| 25 | +foreach ($diffLines as $diffLine) { |
| 26 | + $operation = $diffLine[0]; |
| 27 | + if ($operation === ' ') { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + $path = substr($diffLine, 1); |
| 32 | + $oldFilePath = $oldDir . '/' . $path; |
| 33 | + if (!is_file($oldFilePath)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + $newFilePath = $newDir . '/' . $path; |
| 38 | + if (!is_file($newFilePath)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + $stringDiff = $differ->diff(file_get_contents($oldFilePath), file_get_contents($newFilePath)); |
| 43 | + if ($stringDiff === '') { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $isDifferent = true; |
| 48 | + |
| 49 | + echo "$path:\n"; |
| 50 | + foreach (explode("\n", $stringDiff) as $line) { |
| 51 | + if (str_starts_with($line, '+')) { |
| 52 | + echo "\033[32m$line\033[0m\n"; |
| 53 | + } elseif (str_starts_with($line, '-')) { |
| 54 | + echo "\033[31m$line\033[0m\n"; |
| 55 | + } else { |
| 56 | + echo "$line\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + echo "\n"; |
| 61 | +} |
| 62 | + |
| 63 | +if ($isDifferent) { |
| 64 | + exit(1); |
| 65 | +} |
0 commit comments