Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Add Manifest and SkipProvider solutions to Ignition (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Mar 18, 2024
2 parents 8643b41 + 1a11931 commit 7c16e26
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected function skipProvider($provider, Throwable $e): ServiceProvider
report($e);

if ($this->environment('development', 'testing', 'local') && ! $this->runningInConsole()) {
throw $e;
$this->booted(fn () => throw $e);
}

return is_object($provider) ? $provider : new class($this) extends ServiceProvider
Expand Down
2 changes: 1 addition & 1 deletion src/Roots/Acorn/Assets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function pipeline(array $config): array
protected function getJsonManifest(string $jsonManifest): array
{
if (! file_exists($jsonManifest)) {
throw new ManifestNotFoundException("The manifest [{$jsonManifest}] cannot be found.");
throw new ManifestNotFoundException("The asset manifest [{$jsonManifest}] cannot be found.");
}

return json_decode(file_get_contents($jsonManifest), true) ?? [];
Expand Down
1 change: 1 addition & 0 deletions src/Roots/Acorn/DefaultProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DefaultProviders extends DefaultProvidersBase
* @var array
*/
protected $acornProviders = [
\Roots\Acorn\Exceptions\ExceptionServiceProvider::class,
\Roots\Acorn\Assets\AssetsServiceProvider::class,
\Roots\Acorn\Filesystem\FilesystemServiceProvider::class,
\Roots\Acorn\Providers\AcornServiceProvider::class,
Expand Down
41 changes: 41 additions & 0 deletions src/Roots/Acorn/Exceptions/ExceptionServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Roots\Acorn\Exceptions;

use Illuminate\Support\ServiceProvider;
use Roots\Acorn\Exceptions\Solutions\ManifestSolutionProvider;
use Roots\Acorn\Exceptions\Solutions\SkipProviderSolutionProvider;
use Spatie\Ignition\Contracts\SolutionProviderRepository;

class ExceptionServiceProvider extends ServiceProvider
{
/**
* The solution providers that should be registered.
*/
protected array $solutions = [
ManifestSolutionProvider::class,
SkipProviderSolutionProvider::class,
];

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (! class_exists('Spatie\Ignition\Ignition')) {
return;
}

$this->registerSolutions();
}

/**
* Register the exception solutions.
*/
protected function registerSolutions()
{
$this->app->make(SolutionProviderRepository::class)->registerSolutionProviders($this->solutions);
}
}
71 changes: 71 additions & 0 deletions src/Roots/Acorn/Exceptions/Solutions/ManifestSolutionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Roots\Acorn\Exceptions\Solutions;

use Illuminate\Support\Str;
use Spatie\Ignition\Contracts\BaseSolution;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Spatie\Ignition\Contracts\Solution;
use Throwable;

class ManifestSolutionProvider implements HasSolutionsForThrowable
{
/**
* The documentation links.
*/
protected array $links = [
'Sage Documentation' => 'https://roots.io/sage/docs/installation/',
'Bud Documentation' => 'https://bud.js.org/learn/getting-started',
];

/**
* Determine if the provider can solve the given throwable.
*/
public function canSolve(Throwable $throwable): bool
{
return Str::startsWith($throwable->getMessage(), 'The asset manifest');
}

/**
* Get the solutions for the given throwable.
*/
public function getSolutions(Throwable $throwable): array
{
return [$this->getSolution()];
}

/**
* Get the solutions for the given throwable.
*/
public function getSolution(): Solution
{
$baseCommand = collect([
'pnpm-lock.yaml' => 'pnpm',
'yarn.lock' => 'yarn',
])->first(fn ($_, $lockfile) => file_exists(base_path($lockfile)), 'npm run');

return app()->environment('development', 'testing', 'local')
? $this->getLocalSolution($baseCommand)
: $this->getProductionSolution($baseCommand);
}

/**
* Get the local solution.
*/
protected function getLocalSolution(string $baseCommand): Solution
{
return BaseSolution::create('Start the development server')
->setSolutionDescription("Run `{$baseCommand} dev` in your terminal and refresh the page.")
->setDocumentationLinks($this->links);
}

/**
* Get the production solution.
*/
protected function getProductionSolution(string $baseCommand): Solution
{
return BaseSolution::create('Build the production assets')
->setSolutionDescription("Run `{$baseCommand} build` in your deployment script.")
->setDocumentationLinks($this->links);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Roots\Acorn\Exceptions\Solutions;

use Roots\Acorn\Exceptions\SkipProviderException;
use Spatie\Ignition\Contracts\BaseSolution;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Throwable;

class SkipProviderSolutionProvider implements HasSolutionsForThrowable
{
/**
* Determine if the provider can solve the given throwable.
*/
public function canSolve(Throwable $throwable): bool
{
return $throwable instanceof SkipProviderException;
}

/**
* Get the solutions for the given throwable.
*/
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Clear the provider cache')
->setSolutionDescription('Run `wp acorn optimize:clear` in your terminal and refresh the page.'),
];
}
}

0 comments on commit 7c16e26

Please sign in to comment.