|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require __DIR__ . '/../vendor/autoload.php'; |
| 6 | + |
| 7 | +use BlackCat\Installer\Installer; |
| 8 | +use Psr\Log\AbstractLogger; |
| 9 | + |
| 10 | +final class CliLogger extends AbstractLogger |
| 11 | +{ |
| 12 | + public function log($level, $message, array $context = []): void |
| 13 | + { |
| 14 | + echo strtoupper((string) $level) . ' ' . $message . ' ' . json_encode($context) . PHP_EOL; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +$installer = new Installer(null, new CliLogger()); |
| 19 | + |
| 20 | +$argv = $_SERVER['argv'] ?? []; |
| 21 | +$command = $argv[1] ?? 'help'; |
| 22 | +$args = array_slice($argv, 2); |
| 23 | + |
| 24 | +switch ($command) { |
| 25 | + case 'list': |
| 26 | + echo json_encode($installer->available(), JSON_PRETTY_PRINT) . PHP_EOL; |
| 27 | + break; |
| 28 | + case 'install': |
| 29 | + [$mods, $envOut, $runBootstrap, $includeFeatureViews] = parseInstallArgs($args); |
| 30 | + if ($mods === []) { |
| 31 | + fwrite(STDERR, "Usage: installer install --modules=auth-core,observability\n"); |
| 32 | + exit(1); |
| 33 | + } |
| 34 | + $extraEnv = $includeFeatureViews ? ['BC_INCLUDE_FEATURE_VIEWS' => '1'] : []; |
| 35 | + if ($includeFeatureViews) { |
| 36 | + $_ENV['BC_INCLUDE_FEATURE_VIEWS'] = '1'; |
| 37 | + if (function_exists('putenv')) { |
| 38 | + putenv('BC_INCLUDE_FEATURE_VIEWS=1'); |
| 39 | + } else { |
| 40 | + fwrite(STDERR, "Warning: putenv() is disabled; bootstrap commands may not see BC_INCLUDE_FEATURE_VIEWS.\n"); |
| 41 | + } |
| 42 | + } |
| 43 | + $installer->install($mods, $envOut, $runBootstrap, $extraEnv); |
| 44 | + break; |
| 45 | + case 'help': |
| 46 | + default: |
| 47 | + echo <<<TXT |
| 48 | +BlackCat Installer CLI |
| 49 | +
|
| 50 | +Usage: |
| 51 | + installer list |
| 52 | + installer install --modules=auth-core,observability |
| 53 | +TXT; |
| 54 | + exit($command === 'help' ? 0 : 1); |
| 55 | +} |
| 56 | + |
| 57 | +function parseInstallArgs(array $args): array |
| 58 | +{ |
| 59 | + $modules = []; |
| 60 | + $envOut = '.blackcat/env.generated'; |
| 61 | + $runBootstrap = true; |
| 62 | + $includeFeatureViews = false; |
| 63 | + foreach ($args as $index => $arg) { |
| 64 | + if (str_starts_with($arg, '--modules=')) { |
| 65 | + $modules = explode(',', substr($arg, 10)); |
| 66 | + } elseif ($arg === '--modules' && isset($args[$index + 1])) { |
| 67 | + $modules = explode(',', $args[$index + 1]); |
| 68 | + } elseif (str_starts_with($arg, '--env-out=')) { |
| 69 | + $envOut = substr($arg, 10); |
| 70 | + } elseif ($arg === '--env-out' && isset($args[$index + 1])) { |
| 71 | + $envOut = $args[$index + 1]; |
| 72 | + } elseif ($arg === '--no-env') { |
| 73 | + $envOut = null; |
| 74 | + } elseif ($arg === '--no-bootstrap') { |
| 75 | + $runBootstrap = false; |
| 76 | + } elseif ($arg === '--include-feature-views') { |
| 77 | + $includeFeatureViews = true; |
| 78 | + } |
| 79 | + } |
| 80 | + $modules = array_values(array_filter(array_map('trim', $modules))); |
| 81 | + return [$modules, $envOut, $runBootstrap, $includeFeatureViews]; |
| 82 | +} |
0 commit comments