From 87c5909d86179c01ba33f0a86e39c6787abc682b Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Thu, 13 Jul 2023 21:01:59 +0100 Subject: [PATCH] Add `--disable-clear` option (#136) Co-authored-by: Jesse Leite --- src/Commands/StaticSiteGenerate.php | 5 ++++- src/Generator.php | 12 ++++++++++++ src/ServiceProvider.php | 4 +++- tests/Concerns/RunsGeneratorCommand.php | 4 ++-- tests/GenerateTest.php | 25 +++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index 91c7b5a..3815955 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -22,7 +22,9 @@ class StaticSiteGenerate extends Command * * @var string */ - protected $signature = 'statamic:ssg:generate {--workers=}'; + protected $signature = 'statamic:ssg:generate + {--workers= : Speed up site generation significantly by installing spatie/fork and using multiple workers } + {--disable-clear : Disable clearing the destination directory when generating whole site }'; /** * The console command description. @@ -59,6 +61,7 @@ public function handle() try { $this->generator ->workers($workers ?? 1) + ->disableClear($this->option('disable-clear') ?? false) ->generate(); } catch (GenerationFailedException $e) { $this->line($e->getConsoleMessage()); diff --git a/src/Generator.php b/src/Generator.php index 195dae4..c80f566 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -36,6 +36,7 @@ class Generator protected $extraUrls; protected $workers = 1; protected $taskResults; + protected $disableClear = false; public function __construct(Application $app, Filesystem $files, Router $router, Tasks $tasks) { @@ -77,6 +78,13 @@ public function addUrls($closure) $this->extraUrls[] = $closure; } + public function disableClear(bool $disableClear = false) + { + $this->disableClear = $disableClear; + + return $this; + } + public function generate() { $this->checkConcurrencySupport(); @@ -128,6 +136,10 @@ public function bindGlide() public function clearDirectory() { + if ($this->disableClear) { + return $this; + } + $this->files->deleteDirectory($this->config['destination'], true); return $this; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index ec883c0..c10720f 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,6 +2,8 @@ namespace Statamic\StaticSite; +use Illuminate\Filesystem\Filesystem; +use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; use Spatie\Fork\Fork; use Statamic\Extensions\Pagination\LengthAwarePaginator as StatamicLengthAwarePaginator; @@ -19,7 +21,7 @@ public function register() }); $this->app->singleton(Generator::class, function ($app) { - return new Generator($app, $app['files'], $app['router'], $app[Tasks::class]); + return new Generator($app, $app[Filesystem::class], $app[Router::class], $app[Tasks::class]); }); } diff --git a/tests/Concerns/RunsGeneratorCommand.php b/tests/Concerns/RunsGeneratorCommand.php index afe5ebe..eb7da72 100644 --- a/tests/Concerns/RunsGeneratorCommand.php +++ b/tests/Concerns/RunsGeneratorCommand.php @@ -22,12 +22,12 @@ public function tearDown(): void parent::tearDown(); } - protected function generate() + protected function generate($options = []) { $this->assertFalse($this->files->exists($this->destination)); $this - ->artisan('statamic:ssg:generate') + ->artisan('statamic:ssg:generate', $options) ->doesntExpectOutputToContain('pages not generated'); $this->assertTrue($this->files->exists($this->destination)); diff --git a/tests/GenerateTest.php b/tests/GenerateTest.php index 1f8bd02..7fce21f 100644 --- a/tests/GenerateTest.php +++ b/tests/GenerateTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Illuminate\Filesystem\Filesystem; use Statamic\Facades\Config; use Tests\Concerns\RunsGeneratorCommand; @@ -71,6 +72,30 @@ public function it_generates_pages_to_custom_destination() $this->cleanUpDestination(); } + /** @test */ + public function it_clears_destination_directory_when_generating_site() + { + $this + ->partialMock(Filesystem::class) + ->shouldReceive('deleteDirectory') + ->with(config('statamic.ssg.destination'), true) + ->once(); + + $this->generate(); + } + + /** @test */ + public function it_can_generate_site_without_clearing_destination_directory() + { + $this + ->partialMock(Filesystem::class) + ->shouldReceive('deleteDirectory') + ->with(config('statamic.ssg.destination'), true) + ->never(); + + $this->generate(['--disable-clear' => true]); + } + /** @test */ public function it_generates_paginated_pages() {