Skip to content

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 9 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/nativephp-internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@
* 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'),
],

/**
* The binary path of PHP for NativePHP to use at build.
*/
'php_binary_path' => env('NATIVEPHP_PHP_BINARY_PATH'),
];
165 changes: 165 additions & 0 deletions src/Commands/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?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();

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];
});

$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',
[
'Versions' => $versions,
'Configuration' => [
'Provider' => config('nativephp.provider'),
'BuildHooks' => [
'Pre' => config('nativephp.prebuild'),
'Post' => config('nativephp.postbuild'),
],
'NotarizationEnabled' => $isNotarizationConfigured,
'CustomPHPBinary' => config('nativephp-internal.php_binary_path') ?? false,
],
]
);

return $this;
}

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');
}
}
}
4 changes: 3 additions & 1 deletion src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Loading