|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Script to auto-generate the `phars/index.html` page for the GH Pages website. |
| 5 | + * |
| 6 | + * {@internal This script has a minimum PHP requirement of PHP 7.0.} |
| 7 | + * |
| 8 | + * @copyright 2025 PHPCSStandards and contributors |
| 9 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 10 | + */ |
| 11 | + |
| 12 | +// A directory containing files that are named {phpcs,phpcbf}-*.{phar,phar.asc}. |
| 13 | +$pharDir = __DIR__ . '/../src/phars'; |
| 14 | + |
| 15 | +$allFiles = array_filter(scandir($pharDir), function (string $file) { |
| 16 | + return preg_match('/\.phar(\.asc)?$/', $file); |
| 17 | +}); |
| 18 | + |
| 19 | +$filesGroupedByVersion = []; |
| 20 | + |
| 21 | +foreach ($allFiles as $file) { |
| 22 | + $matches = []; |
| 23 | + |
| 24 | + if (preg_match('/^(?:phpcs|phpcbf)-(.*?)(\.phar(?:\.asc)?)$/', $file, $matches)) { |
| 25 | + $version = $matches[1]; |
| 26 | + |
| 27 | + if (!isset($filesGroupedByVersion[$version])) { |
| 28 | + $filesGroupedByVersion[$version] = []; |
| 29 | + } |
| 30 | + |
| 31 | + $filesGroupedByVersion[$version][] = $file; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +uksort($filesGroupedByVersion, function ($a, $b) { |
| 36 | + return version_compare($b, $a); |
| 37 | +}); |
| 38 | + |
| 39 | +function indent(int $level): string |
| 40 | +{ |
| 41 | + $indentSpaces = ' '; |
| 42 | + |
| 43 | + $output = ''; |
| 44 | + |
| 45 | + for ($i = 0; $i < $level; $i++) { |
| 46 | + $output .= $indentSpaces; |
| 47 | + } |
| 48 | + |
| 49 | + return $output; |
| 50 | +} |
| 51 | + |
| 52 | +function humanReadableFilesize(string $file): string |
| 53 | +{ |
| 54 | + $bytes = filesize($file); |
| 55 | + |
| 56 | + $units = ['B', 'K', 'M', 'G']; |
| 57 | + $factor = floor((strlen($bytes) - 1) / 3); |
| 58 | + return sprintf('%.1f', $bytes / pow(1024, $factor)) . $units[(int) $factor]; |
| 59 | +} |
| 60 | + |
| 61 | +$html = "<ul class=\"phar-list\">\n"; |
| 62 | + |
| 63 | +foreach ($filesGroupedByVersion as $version => $files) { |
| 64 | + sort($files); |
| 65 | + |
| 66 | + $html .= indent(3) . "<li class=\"phar-list__version\">\n" |
| 67 | + . indent(4) . '<h2 class="phar-list__version-label">' . $version . "</h2>\n" |
| 68 | + . indent(4) . "<ul class=\"phar-list__files\">\n"; |
| 69 | + |
| 70 | + foreach ($files as $file) { |
| 71 | + $fileSize = humanReadableFilesize($pharDir . '/' . $file); |
| 72 | + |
| 73 | + $html .= indent(5) . '<li><a download href="/phars/' . htmlspecialchars($file) . '">' . htmlspecialchars($file) |
| 74 | + . '</a> <span class="phar-list__filesize">' . htmlspecialchars($fileSize) . "</span></li>\n"; |
| 75 | + } |
| 76 | + |
| 77 | + $html .= indent(4) . "</ul>\n" |
| 78 | + . indent(3) . "</li>\n"; |
| 79 | +} |
| 80 | + |
| 81 | +$html .= indent(2) . "</li>\n" |
| 82 | + . "</ul>\n"; |
| 83 | + |
| 84 | +$template = file_get_contents(__DIR__ . '/phars.html.template'); |
| 85 | + |
| 86 | +$output = str_replace('<!-- {{ PHARS }} -->', $html, $template); |
| 87 | + |
| 88 | +file_put_contents($pharDir . '/index.html', $output); |
| 89 | + |
| 90 | +echo $pharDir . "/index.html generated successfully.\n"; |
0 commit comments