|
| 1 | +<?php |
| 2 | + |
| 3 | +chdir(__DIR__.'/..'); |
| 4 | + |
| 5 | +require __DIR__.'/../vendor/autoload.php'; |
| 6 | +$app = require __DIR__.'/../bootstrap/app.php'; |
| 7 | +$app->make(Illuminate\Contracts\Console\Kernel::class); |
| 8 | + |
| 9 | +// Try to get list via Symfony console JSON if available |
| 10 | +$commands = []; |
| 11 | + |
| 12 | +$artisan = $app->make(Illuminate\Contracts\Console\Kernel::class); |
| 13 | +$output = new Symfony\Component\Console\Output\BufferedOutput; |
| 14 | +$input = new Symfony\Component\Console\Input\ArrayInput(['command' => 'list', '--format' => 'json']); |
| 15 | +$artisan->handle($input, $output); |
| 16 | +$data = json_decode($output->fetch(), true); |
| 17 | +if (is_array($data) && isset($data['commands'])) { |
| 18 | + foreach ($data['commands'] as $cmd) { |
| 19 | + $commands[] = [ |
| 20 | + 'name' => $cmd['name'] ?? '', |
| 21 | + 'description' => $cmd['description'] ?? '', |
| 22 | + ]; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Keep only custom commands starting with `app:` then filter unique and sort |
| 27 | +$seen = []; |
| 28 | +$commands = array_values(array_filter($commands, function ($c) use (&$seen) { |
| 29 | + $key = $c['name'] ?? ''; |
| 30 | + if (! $key) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + if (! str_starts_with($key, 'app:')) { |
| 34 | + return false; |
| 35 | + } // only user-defined commands |
| 36 | + if (isset($seen[$key])) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + $seen[$key] = true; |
| 40 | + |
| 41 | + return true; |
| 42 | +})); |
| 43 | +usort($commands, function ($a, $b) { |
| 44 | + return strcmp($a['name'], $b['name']); |
| 45 | +}); |
| 46 | + |
| 47 | +$readmePath = __DIR__.'/../README.md'; |
| 48 | +$existing = file_exists($readmePath) ? file_get_contents($readmePath) : ''; |
| 49 | + |
| 50 | +$start = '<!-- COMMANDS:START -->'; |
| 51 | +$end = '<!-- COMMANDS:END -->'; |
| 52 | + |
| 53 | +$table = "\n"; |
| 54 | +$table .= "| Command | Description |\n"; |
| 55 | +$table .= "|---|---|\n"; |
| 56 | +if ($commands) { |
| 57 | + foreach ($commands as $c) { |
| 58 | + $name = $c['name']; |
| 59 | + $desc = str_replace(["\n", "\r", '|'], [' ', ' ', '\\|'], $c['description']); |
| 60 | + $table .= "| `{$name}` | {$desc} |\n"; |
| 61 | + } |
| 62 | +} else { |
| 63 | + $table .= "| _(no commands found)_ | |\n"; |
| 64 | +} |
| 65 | +$table .= "\n"; |
| 66 | + |
| 67 | +$section = "\n## Console Commands\n\n".$start."\n".$table.$end."\n"; |
| 68 | + |
| 69 | +if (str_contains($existing, $start) && str_contains($existing, $end)) { |
| 70 | + $new = preg_replace('/'.preg_quote($start, '/').'.*?'.preg_quote($end, '/').'/s', $start."\n".$table.$end, $existing); |
| 71 | +} else { |
| 72 | + $new = rtrim($existing)."\n\n".$section; |
| 73 | +} |
| 74 | + |
| 75 | +if ($new !== $existing) { |
| 76 | + file_put_contents($readmePath, $new); |
| 77 | +} |
| 78 | + |
| 79 | +echo "README.md updated.\n"; |
0 commit comments