-
-
Notifications
You must be signed in to change notification settings - Fork 203
Implement Debug Command #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dafcbd6
Implement Debug Command
3058bbc
Fix styling
PeteBishwhip 419b198
Output to Clipboard support
21e75fb
Windows support for path detection
448fc85
Fix styling
PeteBishwhip e7d32b2
z vs s - z wins
fb914b0
Remove error log parsing for now
9ea8cf6
Implement config vars for notarization
8d2bde7
Add PHP Binary Path to config
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Commands; | ||
|
||
use Composer\InstalledVersions; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Console\PromptsForMissingInput; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Process; | ||
|
||
use function Laravel\Prompts\error; | ||
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; | ||
|
||
class DebugCommand extends Command implements PromptsForMissingInput | ||
{ | ||
protected $signature = 'native:debug {output}'; | ||
|
||
protected $description = 'Generate debug information required for opening an issue.'; | ||
|
||
private Collection $debugInfo; | ||
|
||
public function handle(): void | ||
{ | ||
$this->debugInfo = collect(); | ||
intro('Generating Debug Information...'); | ||
|
||
$this->processEnvironment() | ||
->processNativePHP() | ||
->processErrorLog(); | ||
|
||
switch ($this->argument('output')) { | ||
case 'File': | ||
$this->outputToFile(); | ||
break; | ||
case 'Clipboard': | ||
$this->outputToClipboard(); | ||
break; | ||
case 'Console': | ||
$this->outputToConsole(); | ||
break; | ||
default: | ||
error('Invalid output option specified.'); | ||
} | ||
|
||
outro('Debug Information Generated.'); | ||
} | ||
|
||
private function processEnvironment(): static | ||
{ | ||
$locationCommand = 'which'; | ||
|
||
if (PHP_OS_FAMILY === 'Windows') { | ||
$locationCommand = 'where'; | ||
} | ||
|
||
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("$locationCommand node")->output()), | ||
], | ||
'NPM' => [ | ||
'Version' => trim(Process::run('npm -v')->output()), | ||
'Path' => trim(Process::run("$locationCommand npm")->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, | ||
PeteBishwhip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
] | ||
); | ||
|
||
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) { | ||
PeteBishwhip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', 'Clipboard', '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) | ||
); | ||
} | ||
|
||
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'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.