|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Native\Laravel\Commands; |
| 4 | + |
| 5 | +use Composer\InstalledVersions; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Contracts\Console\PromptsForMissingInput; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use Illuminate\Support\Facades\File; |
| 10 | +use Illuminate\Support\Facades\Process; |
| 11 | + |
| 12 | +use function Laravel\Prompts\error; |
| 13 | +use function Laravel\Prompts\info; |
| 14 | +use function Laravel\Prompts\intro; |
| 15 | +use function Laravel\Prompts\note; |
| 16 | +use function Laravel\Prompts\outro; |
| 17 | +use function Laravel\Prompts\select; |
| 18 | + |
| 19 | +class DebugCommand extends Command implements PromptsForMissingInput |
| 20 | +{ |
| 21 | + protected $signature = 'native:debug {output}'; |
| 22 | + |
| 23 | + protected $description = 'Generate debug information required for opening an issue.'; |
| 24 | + |
| 25 | + private Collection $debugInfo; |
| 26 | + |
| 27 | + public function handle(): void |
| 28 | + { |
| 29 | + $this->debugInfo = collect(); |
| 30 | + intro('Generating Debug Information...'); |
| 31 | + |
| 32 | + $this->processEnvironment() |
| 33 | + ->processNativePHP(); |
| 34 | + |
| 35 | + switch ($this->argument('output')) { |
| 36 | + case 'File': |
| 37 | + $this->outputToFile(); |
| 38 | + break; |
| 39 | + case 'Clipboard': |
| 40 | + $this->outputToClipboard(); |
| 41 | + break; |
| 42 | + case 'Console': |
| 43 | + $this->outputToConsole(); |
| 44 | + break; |
| 45 | + default: |
| 46 | + error('Invalid output option specified.'); |
| 47 | + } |
| 48 | + |
| 49 | + outro('Debug Information Generated.'); |
| 50 | + } |
| 51 | + |
| 52 | + private function processEnvironment(): static |
| 53 | + { |
| 54 | + $locationCommand = 'which'; |
| 55 | + |
| 56 | + if (PHP_OS_FAMILY === 'Windows') { |
| 57 | + $locationCommand = 'where'; |
| 58 | + } |
| 59 | + |
| 60 | + info('Generating Environment Data...'); |
| 61 | + $environment = [ |
| 62 | + 'PHP' => [ |
| 63 | + 'Version' => phpversion(), |
| 64 | + 'Path' => PHP_BINARY, |
| 65 | + ], |
| 66 | + 'Laravel' => [ |
| 67 | + 'Version' => app()->version(), |
| 68 | + 'ConfigCached' => file_exists($this->laravel->getCachedConfigPath()), |
| 69 | + 'DebugEnabled' => $this->laravel->hasDebugModeEnabled(), |
| 70 | + ], |
| 71 | + 'Node' => [ |
| 72 | + 'Version' => trim(Process::run('node -v')->output()), |
| 73 | + 'Path' => trim(Process::run("$locationCommand node")->output()), |
| 74 | + ], |
| 75 | + 'NPM' => [ |
| 76 | + 'Version' => trim(Process::run('npm -v')->output()), |
| 77 | + 'Path' => trim(Process::run("$locationCommand npm")->output()), |
| 78 | + ], |
| 79 | + 'OperatingSystem' => PHP_OS, |
| 80 | + ]; |
| 81 | + |
| 82 | + $this->debugInfo->put('Environment', $environment); |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + private function processNativePHP(): static |
| 88 | + { |
| 89 | + info('Processing NativePHP Data...'); |
| 90 | + // Get composer versions |
| 91 | + $versions = collect([ |
| 92 | + 'nativephp/electron' => null, |
| 93 | + 'nativephp/laravel' => null, |
| 94 | + 'nativephp/php-bin' => null, |
| 95 | + ])->mapWithKeys(function ($version, $key) { |
| 96 | + try { |
| 97 | + $version = InstalledVersions::getVersion($key); |
| 98 | + } catch (\OutOfBoundsException) { |
| 99 | + $version = 'Not Installed'; |
| 100 | + } |
| 101 | + |
| 102 | + return [$key => $version]; |
| 103 | + }); |
| 104 | + |
| 105 | + $isNotarizationConfigured = config('nativephp-internal.notarization.apple_id') |
| 106 | + && config('nativephp-internal.notarization.apple_id_pass') |
| 107 | + && config('nativephp-internal.notarization.apple_team_id'); |
| 108 | + |
| 109 | + $this->debugInfo->put( |
| 110 | + 'NativePHP', |
| 111 | + [ |
| 112 | + 'Versions' => $versions, |
| 113 | + 'Configuration' => [ |
| 114 | + 'Provider' => config('nativephp.provider'), |
| 115 | + 'BuildHooks' => [ |
| 116 | + 'Pre' => config('nativephp.prebuild'), |
| 117 | + 'Post' => config('nativephp.postbuild'), |
| 118 | + ], |
| 119 | + 'NotarizationEnabled' => $isNotarizationConfigured, |
| 120 | + 'CustomPHPBinary' => config('nativephp-internal.php_binary_path') ?? false, |
| 121 | + ], |
| 122 | + ] |
| 123 | + ); |
| 124 | + |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + protected function promptForMissingArgumentsUsing(): array |
| 129 | + { |
| 130 | + return [ |
| 131 | + 'output' => fn () => select( |
| 132 | + 'Where would you like to output the debug information?', |
| 133 | + ['File', 'Clipboard', 'Console'], |
| 134 | + 'File' |
| 135 | + ), |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + private function outputToFile(): void |
| 140 | + { |
| 141 | + File::put(base_path('nativephp_debug.json'), json_encode($this->debugInfo->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 142 | + note('Debug information saved to '.base_path('nativephp_debug.json')); |
| 143 | + } |
| 144 | + |
| 145 | + private function outputToConsole(): void |
| 146 | + { |
| 147 | + $this->output->writeln( |
| 148 | + print_r($this->debugInfo->toArray(), true) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + private function outputToClipboard(): void |
| 153 | + { |
| 154 | + $json = json_encode($this->debugInfo->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 155 | + |
| 156 | + // Copy json to clipboard |
| 157 | + if (PHP_OS_FAMILY === 'Windows') { |
| 158 | + Process::run('echo '.escapeshellarg($json).' | clip'); |
| 159 | + } elseif (PHP_OS_FAMILY === 'Linux') { |
| 160 | + Process::run('echo '.escapeshellarg($json).' | xclip -selection clipboard'); |
| 161 | + } else { |
| 162 | + Process::run('echo '.escapeshellarg($json).' | pbcopy'); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments