From dafcbd668553c06bfdd277c57dcbcf9ceeb5b4ad Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Thu, 13 Mar 2025 13:05:34 +0000 Subject: [PATCH 1/9] Implement Debug Command --- src/Commands/DebugCommand.php | 175 ++++++++++++++++++++++++++++++++++ src/NativeServiceProvider.php | 4 +- 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/Commands/DebugCommand.php diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php new file mode 100644 index 0000000..a11ed4e --- /dev/null +++ b/src/Commands/DebugCommand.php @@ -0,0 +1,175 @@ +debugInfo = collect(); + intro('Generating Debug Information...'); + + $this->processEnvironment() + ->processNativePHP() + ->processErrorLog(); + + switch ($this->argument('output')) { + case 'File': + $this->outputToFile(); + break; + case 'Console': + $this->outputToConsole(); + break; + default: + error('Invalid output option specified.'); + } + + outro('Debug Information Generated.'); + } + + private function processEnvironment(): static + { + info('Generating Environment Data...'); + $environment = [ + 'PHP' => [ + 'Version' => phpversion(), + 'Path' => PHP_BINARY, + ], + 'Laravel' => [ + 'Version' => app()->version(), + 'ConfigCached' => file_exists($this->laravel->getCachedConfigPath()), + 'DebugEnabled' => $this->laravel->hasDebugModeEnabled() + ], + 'Node' => [ + 'Version' => trim(Process::run('node -v')->output()), + 'Path' => trim(Process::run('which node')->output()), + 'NPM' => trim(Process::run('npm -v')->output()), + ], + 'OperatingSystem' => PHP_OS, + ]; + + $this->debugInfo->put('Environment', $environment); + + return $this; + } + + private function processNativePHP(): static + { + info('Processing NativePHP Data...'); + // Get composer versions + $versions = collect([ + 'nativephp/electron' => null, + 'nativephp/laravel' => null, + 'nativephp/php-bin' => null, + ])->mapWithKeys(function ($version, $key) { + try { + $version = InstalledVersions::getVersion($key); + } catch (\OutOfBoundsException) { + $version = 'Not Installed'; + } + + return [$key => $version]; + }); + + $isNotarisationConfigured = env('NATIVEPHP_APPLE_ID') + && env('NATIVEPHP_APPLE_ID_PASS') + && env('NATIVEPHP_APPLE_TEAM_ID'); + + $this->debugInfo->put( + 'NativePHP', + [ + 'Versions' => $versions, + 'Configuration' => [ + 'Provider' => config('nativephp.provider'), + 'BuildHooks' => [ + 'Pre' => config('nativephp.prebuild'), + 'Post' => config('nativephp.postbuild'), + ], + 'NotarizationEnabled' => $isNotarisationConfigured, + 'CustomPHPBinary' => env('NATIVEPHP_PHP_BINARY_PATH') ?: false, + ], + ] + ); + + return $this; + } + + private function processErrorLog(): void + { + info('Processing Error Log Data...'); + $errorLog = file_exists($logPath = storage_path('logs/laravel.log')) + ? file_get_contents($logPath) + : 'No logs found.'; + + // Process each line as a single array element + $errorLog = explode(PHP_EOL, $errorLog); + $errorCount = 0; + $errors = []; + + $currentLine = ''; + foreach ($errorLog as $line) { + if ($errorCount === 5) { + break; + } + + // Check if string starts with date format Y-m-d H:i:s in square brackets + if (preg_match('/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]/', $line)) { + if (!empty($currentLine)) { + $errors[] = $currentLine; + $currentLine = ''; + $errorCount++; + } + } + + $currentLine .= $line . PHP_EOL; + } + + if (!empty($currentLine)) { + $errors[] = $currentLine; + } + + $this->debugInfo->put('ErrorLog', $errors); + } + + protected function promptForMissingArgumentsUsing(): array + { + return [ + 'output' => fn () => select( + 'Where would you like to output the debug information?', + ['File', 'Console'], + 'File' + ) + ]; + } + + private function outputToFile(): void + { + File::put(base_path('nativephp_debug.json'), json_encode($this->debugInfo->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + note('Debug information saved to ' . base_path('nativephp_debug.json')); + } + + private function outputToConsole(): void + { + $this->output->writeln( + print_r($this->debugInfo->toArray(), true) + ); + } +} diff --git a/src/NativeServiceProvider.php b/src/NativeServiceProvider.php index b5b3225..c425e56 100644 --- a/src/NativeServiceProvider.php +++ b/src/NativeServiceProvider.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Native\Laravel\ChildProcess as ChildProcessImplementation; +use Native\Laravel\Commands\DebugCommand; use Native\Laravel\Commands\FreshCommand; use Native\Laravel\Commands\LoadPHPConfigurationCommand; use Native\Laravel\Commands\LoadStartupConfigurationCommand; @@ -35,8 +36,9 @@ public function configurePackage(Package $package): void $package ->name('nativephp') ->hasCommands([ - MigrateCommand::class, + DebugCommand::class, FreshCommand::class, + MigrateCommand::class, SeedDatabaseCommand::class, ]) ->hasConfigFile() From 3058bbc123956a4c71a2cf00fe2ce1c5daafa5c7 Mon Sep 17 00:00:00 2001 From: PeteBishwhip <9081809+PeteBishwhip@users.noreply.github.com> Date: Thu, 13 Mar 2025 13:08:14 +0000 Subject: [PATCH 2/9] Fix styling --- src/Commands/DebugCommand.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index a11ed4e..e7cfa46 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -8,9 +8,10 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Process; + use function Laravel\Prompts\error; -use function Laravel\Prompts\intro; use function Laravel\Prompts\info; +use function Laravel\Prompts\intro; use function Laravel\Prompts\note; use function Laravel\Prompts\outro; use function Laravel\Prompts\select; @@ -18,6 +19,7 @@ class DebugCommand extends Command implements PromptsForMissingInput { protected $signature = 'native:debug {output}'; + protected $description = 'Generate debug information required for opening an issue.'; private Collection $debugInfo; @@ -56,7 +58,7 @@ private function processEnvironment(): static 'Laravel' => [ 'Version' => app()->version(), 'ConfigCached' => file_exists($this->laravel->getCachedConfigPath()), - 'DebugEnabled' => $this->laravel->hasDebugModeEnabled() + 'DebugEnabled' => $this->laravel->hasDebugModeEnabled(), ], 'Node' => [ 'Version' => trim(Process::run('node -v')->output()), @@ -132,17 +134,17 @@ private function processErrorLog(): void // Check if string starts with date format Y-m-d H:i:s in square brackets if (preg_match('/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]/', $line)) { - if (!empty($currentLine)) { + if (! empty($currentLine)) { $errors[] = $currentLine; $currentLine = ''; $errorCount++; } } - $currentLine .= $line . PHP_EOL; + $currentLine .= $line.PHP_EOL; } - if (!empty($currentLine)) { + if (! empty($currentLine)) { $errors[] = $currentLine; } @@ -156,14 +158,14 @@ protected function promptForMissingArgumentsUsing(): array 'Where would you like to output the debug information?', ['File', 'Console'], 'File' - ) + ), ]; } private function outputToFile(): void { File::put(base_path('nativephp_debug.json'), json_encode($this->debugInfo->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - note('Debug information saved to ' . base_path('nativephp_debug.json')); + note('Debug information saved to '.base_path('nativephp_debug.json')); } private function outputToConsole(): void From 419b1984486e1c1ee8d2f84ed5982d7b5b19f760 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Thu, 13 Mar 2025 13:30:06 +0000 Subject: [PATCH 3/9] Output to Clipboard support --- src/Commands/DebugCommand.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index e7cfa46..9ecb309 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -37,6 +37,9 @@ public function handle(): void case 'File': $this->outputToFile(); break; + case 'Clipboard': + $this->outputToClipboard(); + break; case 'Console': $this->outputToConsole(); break; @@ -156,7 +159,7 @@ protected function promptForMissingArgumentsUsing(): array return [ 'output' => fn () => select( 'Where would you like to output the debug information?', - ['File', 'Console'], + ['File', 'Clipboard', 'Console'], 'File' ), ]; @@ -174,4 +177,18 @@ private function outputToConsole(): void print_r($this->debugInfo->toArray(), true) ); } + + private function outputToClipboard(): void + { + $json = json_encode($this->debugInfo->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + + // Copy json to clipboard + if (PHP_OS_FAMILY === 'Windows') { + Process::run('echo ' . escapeshellarg($json) . ' | clip'); + } elseif (PHP_OS_FAMILY === 'Linux') { + Process::run('echo ' . escapeshellarg($json) . ' | xclip -selection clipboard'); + } else { + Process::run('echo ' . escapeshellarg($json) . ' | pbcopy'); + } + } } From 21e75fb1c44e0f484297a0e18e0a989294fd8be2 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Thu, 13 Mar 2025 13:31:55 +0000 Subject: [PATCH 4/9] Windows support for path detection --- src/Commands/DebugCommand.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index 9ecb309..6edadf5 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -52,6 +52,12 @@ public function handle(): void private function processEnvironment(): static { + $locationCommand = 'which'; + + if (PHP_OS_FAMILY === 'Windows') { + $locationCommand = 'where'; + } + info('Generating Environment Data...'); $environment = [ 'PHP' => [ @@ -65,8 +71,11 @@ private function processEnvironment(): static ], 'Node' => [ 'Version' => trim(Process::run('node -v')->output()), - 'Path' => trim(Process::run('which node')->output()), - 'NPM' => trim(Process::run('npm -v')->output()), + 'Path' => trim(Process::run("$locationCommand node")->output()), + ], + 'NPM' => [ + 'Version' => trim(Process::run('npm -v')->output()), + 'Path' => trim(Process::run("$locationCommand npm")->output()), ], 'OperatingSystem' => PHP_OS, ]; From 448fc852f983b58123bb32c20dc874599000621b Mon Sep 17 00:00:00 2001 From: PeteBishwhip <9081809+PeteBishwhip@users.noreply.github.com> Date: Thu, 13 Mar 2025 13:32:18 +0000 Subject: [PATCH 5/9] Fix styling --- src/Commands/DebugCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index 6edadf5..5de850d 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -193,11 +193,11 @@ private function outputToClipboard(): void // Copy json to clipboard if (PHP_OS_FAMILY === 'Windows') { - Process::run('echo ' . escapeshellarg($json) . ' | clip'); + Process::run('echo '.escapeshellarg($json).' | clip'); } elseif (PHP_OS_FAMILY === 'Linux') { - Process::run('echo ' . escapeshellarg($json) . ' | xclip -selection clipboard'); + Process::run('echo '.escapeshellarg($json).' | xclip -selection clipboard'); } else { - Process::run('echo ' . escapeshellarg($json) . ' | pbcopy'); + Process::run('echo '.escapeshellarg($json).' | pbcopy'); } } } From e7d32b23c864b98f7e943e2f3a2ab282c9a50939 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Fri, 14 Mar 2025 09:40:08 +0000 Subject: [PATCH 6/9] z vs s - z wins --- src/Commands/DebugCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index 5de850d..681b501 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -103,7 +103,7 @@ private function processNativePHP(): static return [$key => $version]; }); - $isNotarisationConfigured = env('NATIVEPHP_APPLE_ID') + $isNotarizationConfigured = env('NATIVEPHP_APPLE_ID') && env('NATIVEPHP_APPLE_ID_PASS') && env('NATIVEPHP_APPLE_TEAM_ID'); @@ -117,7 +117,7 @@ private function processNativePHP(): static 'Pre' => config('nativephp.prebuild'), 'Post' => config('nativephp.postbuild'), ], - 'NotarizationEnabled' => $isNotarisationConfigured, + 'NotarizationEnabled' => $isNotarizationConfigured, 'CustomPHPBinary' => env('NATIVEPHP_PHP_BINARY_PATH') ?: false, ], ] From fb914b0b74195cc474f847c979fac1ab169565f9 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Fri, 14 Mar 2025 09:46:46 +0000 Subject: [PATCH 7/9] Remove error log parsing for now --- src/Commands/DebugCommand.php | 40 +---------------------------------- 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index 681b501..cdef869 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -30,8 +30,7 @@ public function handle(): void intro('Generating Debug Information...'); $this->processEnvironment() - ->processNativePHP() - ->processErrorLog(); + ->processNativePHP(); switch ($this->argument('output')) { case 'File': @@ -126,43 +125,6 @@ private function processNativePHP(): static return $this; } - private function processErrorLog(): void - { - info('Processing Error Log Data...'); - $errorLog = file_exists($logPath = storage_path('logs/laravel.log')) - ? file_get_contents($logPath) - : 'No logs found.'; - - // Process each line as a single array element - $errorLog = explode(PHP_EOL, $errorLog); - $errorCount = 0; - $errors = []; - - $currentLine = ''; - foreach ($errorLog as $line) { - if ($errorCount === 5) { - break; - } - - // Check if string starts with date format Y-m-d H:i:s in square brackets - if (preg_match('/^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]/', $line)) { - if (! empty($currentLine)) { - $errors[] = $currentLine; - $currentLine = ''; - $errorCount++; - } - } - - $currentLine .= $line.PHP_EOL; - } - - if (! empty($currentLine)) { - $errors[] = $currentLine; - } - - $this->debugInfo->put('ErrorLog', $errors); - } - protected function promptForMissingArgumentsUsing(): array { return [ From 9ea8cf62e7c8bc35a192874fa9fb9ab4b2e3dd08 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Mon, 17 Mar 2025 09:10:08 +0000 Subject: [PATCH 8/9] Implement config vars for notarization --- config/nativephp-internal.php | 9 +++++++++ src/Commands/DebugCommand.php | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/config/nativephp-internal.php b/config/nativephp-internal.php index 4210df9..b01436c 100644 --- a/config/nativephp-internal.php +++ b/config/nativephp-internal.php @@ -29,4 +29,13 @@ * The URL to the NativePHP API. */ 'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'), + + /** + * The credentials to use Apples Notarization service. + */ + 'notarization' => [ + 'apple_id' => env('NATIVEPHP_APPLE_ID'), + 'apple_id_pass' => env('NATIVEPHP_APPLE_ID_PASS'), + 'apple_team_id' => env('NATIVEPHP_APPLE_TEAM_ID'), + ], ]; diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index cdef869..a5d49cf 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -102,9 +102,9 @@ private function processNativePHP(): static return [$key => $version]; }); - $isNotarizationConfigured = env('NATIVEPHP_APPLE_ID') - && env('NATIVEPHP_APPLE_ID_PASS') - && env('NATIVEPHP_APPLE_TEAM_ID'); + $isNotarizationConfigured = config('nativephp-internal.notarization.apple_id') + && config('nativephp-internal.notarization.apple_id_pass') + && config('nativephp-internal.notarization.apple_team_id'); $this->debugInfo->put( 'NativePHP', From 8d2bde75ec7e1f2591c954f8c607f297051bddf7 Mon Sep 17 00:00:00 2001 From: Pete Bishop Date: Mon, 17 Mar 2025 09:12:05 +0000 Subject: [PATCH 9/9] Add PHP Binary Path to config --- config/nativephp-internal.php | 5 +++++ src/Commands/DebugCommand.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config/nativephp-internal.php b/config/nativephp-internal.php index b01436c..2fbb4a3 100644 --- a/config/nativephp-internal.php +++ b/config/nativephp-internal.php @@ -38,4 +38,9 @@ 'apple_id_pass' => env('NATIVEPHP_APPLE_ID_PASS'), 'apple_team_id' => env('NATIVEPHP_APPLE_TEAM_ID'), ], + + /** + * The binary path of PHP for NativePHP to use at build. + */ + 'php_binary_path' => env('NATIVEPHP_PHP_BINARY_PATH'), ]; diff --git a/src/Commands/DebugCommand.php b/src/Commands/DebugCommand.php index a5d49cf..3900d3c 100644 --- a/src/Commands/DebugCommand.php +++ b/src/Commands/DebugCommand.php @@ -117,7 +117,7 @@ private function processNativePHP(): static 'Post' => config('nativephp.postbuild'), ], 'NotarizationEnabled' => $isNotarizationConfigured, - 'CustomPHPBinary' => env('NATIVEPHP_PHP_BINARY_PATH') ?: false, + 'CustomPHPBinary' => config('nativephp-internal.php_binary_path') ?? false, ], ] );