Skip to content

Commit b8a753a

Browse files
authored
Implement Debug Command (#516)
* Implement Debug Command * Fix styling * Output to Clipboard support * Windows support for path detection * Fix styling * z vs s - z wins * Remove error log parsing for now * Implement config vars for notarization * Add PHP Binary Path to config
1 parent 372614d commit b8a753a

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

config/nativephp-internal.php

+14
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@
2929
* The URL to the NativePHP API.
3030
*/
3131
'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'),
32+
33+
/**
34+
* The credentials to use Apples Notarization service.
35+
*/
36+
'notarization' => [
37+
'apple_id' => env('NATIVEPHP_APPLE_ID'),
38+
'apple_id_pass' => env('NATIVEPHP_APPLE_ID_PASS'),
39+
'apple_team_id' => env('NATIVEPHP_APPLE_TEAM_ID'),
40+
],
41+
42+
/**
43+
* The binary path of PHP for NativePHP to use at build.
44+
*/
45+
'php_binary_path' => env('NATIVEPHP_PHP_BINARY_PATH'),
3246
];

src/Commands/DebugCommand.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
}

src/NativeServiceProvider.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Artisan;
99
use Illuminate\Support\Facades\DB;
1010
use Native\Laravel\ChildProcess as ChildProcessImplementation;
11+
use Native\Laravel\Commands\DebugCommand;
1112
use Native\Laravel\Commands\FreshCommand;
1213
use Native\Laravel\Commands\LoadPHPConfigurationCommand;
1314
use Native\Laravel\Commands\LoadStartupConfigurationCommand;
@@ -35,8 +36,9 @@ public function configurePackage(Package $package): void
3536
$package
3637
->name('nativephp')
3738
->hasCommands([
38-
MigrateCommand::class,
39+
DebugCommand::class,
3940
FreshCommand::class,
41+
MigrateCommand::class,
4042
SeedDatabaseCommand::class,
4143
])
4244
->hasConfigFile()

0 commit comments

Comments
 (0)