| 1 | #!/usr/bin/env php |
| 2 | <?php |
| 3 | eval(`cv php:boot`); |
| 4 | |
| 5 | global $civicrm_root; |
| 6 | $realArgs = $argv; |
| 7 | $diffCmd = FALSE; |
| 8 | $files = []; |
| 9 | |
| 10 | array_shift($realArgs); |
| 11 | foreach ($realArgs as $arg) { |
| 12 | switch ($arg) { |
| 13 | case '--diff': |
| 14 | $diffCmd = 'diff -wu'; |
| 15 | break; |
| 16 | |
| 17 | case '--colordiff': |
| 18 | $diffCmd = 'colordiff -wu'; |
| 19 | break; |
| 20 | |
| 21 | default: |
| 22 | $files[] = $arg; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | if (empty($files)) { |
| 27 | echo "usage: cleanup-angular.php [--diff|--colordiff] <file>\n"; |
| 28 | echo "example: cleanup-angular.php '/full/path/to/crmMailing/BlockSummary.html'\n"; |
| 29 | echo "note: The file path must be absolute.\n"; |
| 30 | exit(1); |
| 31 | } |
| 32 | else { |
| 33 | foreach ($files as $file) { |
| 34 | compareFile($file, $diffCmd); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function compareFile($file, $diffCmd) { |
| 39 | $coder = new \Civi\Angular\Coder(); |
| 40 | |
| 41 | if (!file_exists($file)) { |
| 42 | fwrite(STDERR, "Failed to find file $file (CWD=" . getcwd() . ")\n"); |
| 43 | return; |
| 44 | } |
| 45 | $oldMarkup = file_get_contents($file); |
| 46 | if ($coder->checkConsistentHtml($oldMarkup)) { |
| 47 | echo "File \"$file\" appears sufficiently consistent.\n"; |
| 48 | } |
| 49 | else { |
| 50 | $newMarkup = $coder->recode($oldMarkup); |
| 51 | $newFile = "{$file}.recoded"; |
| 52 | echo "File \"$file\" appears to have consistency issues. Created $newFile.\n"; |
| 53 | file_put_contents($newFile, $newMarkup); |
| 54 | if ($diffCmd) { |
| 55 | passthru($diffCmd . ' ' . escapeshellarg($file) . ' ' . escapeshellarg($newFile)); |
| 56 | } |
| 57 | } |
| 58 | } |