From 4c10d906f72d556d2920c72e687a1d0cf4604f48 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Wed, 11 Jan 2023 11:37:47 -0500 Subject: [PATCH] Add debug instructions to valet status, expand "valet installed" status check --- cli/Valet/Status.php | 41 ++++++++++++++++++++++++++++--- cli/app.php | 14 ++++++----- cli/includes/helpers.php | 2 +- tests/BaseApplicationTestCase.php | 2 ++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/cli/Valet/Status.php b/cli/Valet/Status.php index d03d82b..2cf5df5 100644 --- a/cli/Valet/Status.php +++ b/cli/Valet/Status.php @@ -6,6 +6,7 @@ class Status { public $brewServicesUserOutput; public $brewServicesSudoOutput; + public $debugInstructions = []; public function __construct(public Configuration $config, public Brew $brew, public CommandLine $cli, public Filesystem $files) { @@ -21,6 +22,7 @@ public function check(): array $output = collect($this->checks())->map(function (array $check) use (&$isValid) { if (! $thisIsValid = $check['check']()) { + $this->debugInstructions[] = $check['debug']; $isValid = false; } @@ -30,6 +32,7 @@ public function check(): array return [ 'success' => $isValid, 'output' => $output->all(), + 'debug' => $this->debugInstructions(), ]; } @@ -40,70 +43,87 @@ public function checks(): array { return [ [ - 'description' => 'Is Valet installed?', + 'description' => 'Is Valet fully installed?', 'check' => function () { - return is_dir(VALET_HOME_PATH) && file_exists($this->config->path()); + return $this->valetInstalled(); }, + 'debug' => 'Run `composer require laravel/valet` and `valet install`.', ], [ 'description' => 'Is Valet config valid?', 'check' => function () { try { - $this->config->read(); + $config = $this->config->read(); + + foreach (['tld', 'loopback', 'paths'] as $key) { + if (!array_key_exists($key, $config)) { + $this->debugInstructions[] = 'Your Valet config is missing the "'.$key.'" key. Re-add this manually, or delete your config file and re-install.'; + return false; + } + } return true; } catch (\JsonException $e) { return false; } }, + 'debug' => 'Run `valet install` to update your configuration.', ], [ 'description' => 'Is Homebrew installed?', 'check' => function () { return $this->cli->run('which brew') !== ''; }, + 'debug' => 'Visit https://brew.sh/ for instructions on installing Homebrew.', ], [ 'description' => 'Is DnsMasq installed?', 'check' => function () { return $this->brew->installed('dnsmasq'); }, + 'debug' => 'Run `valet install`.', ], [ 'description' => 'Is Dnsmasq running?', 'check' => function () { return $this->isBrewServiceRunning('dnsmasq'); }, + 'debug' => 'Run `valet restart`.', ], [ 'description' => 'Is Nginx installed?', 'check' => function () { return $this->brew->installed('nginx'); }, + 'debug' => 'Run `valet install`.', ], [ 'description' => 'Is Nginx running?', 'check' => function () { return $this->isBrewServiceRunning('nginx'); }, + 'debug' => 'Run `valet restart`.', ], [ 'description' => 'Is PHP installed?', 'check' => function () { return $this->brew->hasInstalledPhp(); }, + 'debug' => 'Run `valet install`.', ], [ 'description' => 'Is PHP running?', 'check' => function () { return $this->isBrewServiceRunning('php', exactMatch: false); }, + 'debug' => 'Run `valet restart`.', ], [ 'description' => 'Is valet.sock present?', 'check' => function () { return $this->files->exists(VALET_HOME_PATH.'/valet.sock'); }, + 'debug' => 'Run `valet install`.', ], ]; } @@ -132,4 +152,19 @@ public function isBrewServiceRunning(string $name, bool $exactMatch = true): boo return false; } + + public function valetInstalled(): bool + { + return is_dir(VALET_HOME_PATH) + && file_exists($this->config->path()) + && is_dir(VALET_HOME_PATH.'/Drivers') + && is_dir(VALET_HOME_PATH.'/Sites') + && is_dir(VALET_HOME_PATH.'/Log') + && is_dir(VALET_HOME_PATH.'/Certificates'); + } + + public function debugInstructions(): string + { + return collect($this->debugInstructions)->unique()->join(PHP_EOL); + } } diff --git a/cli/app.php b/cli/app.php index 2626869..0c696a8 100644 --- a/cli/app.php +++ b/cli/app.php @@ -52,17 +52,16 @@ function (ConsoleCommandEvent $event) { $app->command('install', function (OutputInterface $output) { Nginx::stop(); - output(PHP_EOL); Configuration::install(); - output(PHP_EOL); + output(); Nginx::install(); - output(PHP_EOL); + output(); PhpFpm::install(); - output(PHP_EOL); + output(); DnsMasq::install(Configuration::read()['tld']); - output(PHP_EOL); + output(); Nginx::restart(); - output(PHP_EOL); + output(); Valet::symlinkToUsersBin(); output(PHP_EOL.'Valet installed successfully!'); @@ -88,6 +87,9 @@ function (ConsoleCommandEvent $event) { return Command::SUCCESS; } + info(PHP_EOL.'Debug suggestions:'); + info($status['debug']); + return Command::FAILURE; })->descriptions('Output the status of Valet and its installed services and config.'); diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index 0d16ec7..bcdf978 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -88,7 +88,7 @@ function testing(): bool /** * Output the given text to the console. */ -function output(string $output): void +function output(string $output = ''): void { writer()->writeln($output); } diff --git a/tests/BaseApplicationTestCase.php b/tests/BaseApplicationTestCase.php index 0d4fe93..ac5f087 100644 --- a/tests/BaseApplicationTestCase.php +++ b/tests/BaseApplicationTestCase.php @@ -35,6 +35,8 @@ public function prepTestConfig() Configuration::createConfigurationDirectory(); Configuration::createDriversDirectory(); + Configuration::createLogDirectory(); + Configuration::createCertificatesDirectory(); Configuration::writeBaseConfiguration(); }