From 1449c7b822d9611f777d7c0bedf13e6b641b7387 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 25 Jun 2023 12:48:30 +0100 Subject: [PATCH 1/9] Add force option --- src/Commands/StaticSiteGenerate.php | 4 ++-- src/Generator.php | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index a944a8d..30773d1 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -22,7 +22,7 @@ class StaticSiteGenerate extends Command * * @var string */ - protected $signature = 'statamic:ssg:generate {--workers=}'; + protected $signature = 'statamic:ssg:generate {--workers=} {--force}'; /** * The console command description. @@ -59,7 +59,7 @@ public function handle() try { $this->generator ->workers($workers ?? 1) - ->generate(); + ->generate($this->option('force')); } catch (GenerationFailedException $e) { $this->line($e->getConsoleMessage()); $this->error('Static site generation failed.'); diff --git a/src/Generator.php b/src/Generator.php index 3584c4f..5ca322a 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -78,15 +78,18 @@ public function addUrls($closure) $this->extraUrls[] = $closure; } - public function generate() + public function generate($fresh = false) { $this->checkConcurrencySupport(); Site::setCurrent(Site::default()->handle()); + if ($fresh) { + $this->clearDirectory(); + } + $this ->bindGlide() - ->clearDirectory() ->createContentFiles() ->createSymlinks() ->copyFiles() From 3564ce535188082e1de8ff7d81e0697cf9007352 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 25 Jun 2023 13:15:20 +0100 Subject: [PATCH 2/9] Rename option Keep it all consistent --- src/Commands/StaticSiteGenerate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index 30773d1..515dd1c 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -22,7 +22,7 @@ class StaticSiteGenerate extends Command * * @var string */ - protected $signature = 'statamic:ssg:generate {--workers=} {--force}'; + protected $signature = 'statamic:ssg:generate {--workers=} {--fresh}'; /** * The console command description. @@ -59,7 +59,7 @@ public function handle() try { $this->generator ->workers($workers ?? 1) - ->generate($this->option('force')); + ->generate($this->option('fresh')); } catch (GenerationFailedException $e) { $this->line($e->getConsoleMessage()); $this->error('Static site generation failed.'); From 9f26a2cba76d4f114a29b5a4e7da2f6ee8d29fff Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 25 Jun 2023 13:33:11 +0100 Subject: [PATCH 3/9] Move to methods Save polluting the generate method parameters. Should make merging easier --- src/Commands/StaticSiteGenerate.php | 3 ++- src/Generator.php | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index 515dd1c..38563a1 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -59,7 +59,8 @@ public function handle() try { $this->generator ->workers($workers ?? 1) - ->generate($this->option('fresh')); + ->fresh($this->option('fresh') ?? false) + ->generate(); } catch (GenerationFailedException $e) { $this->line($e->getConsoleMessage()); $this->error('Static site generation failed.'); diff --git a/src/Generator.php b/src/Generator.php index 5ca322a..899d6e4 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -34,6 +34,7 @@ class Generator protected $config; protected $request; protected $after; + protected $fresh = false; protected $extraUrls; protected $workers = 1; protected $taskResults; @@ -66,6 +67,13 @@ public function workers(int $workers) return $this; } + public function fresh(bool $fresh = true) + { + $this->fresh = $fresh; + + return $this; + } + public function after($after) { $this->after = $after; @@ -78,13 +86,13 @@ public function addUrls($closure) $this->extraUrls[] = $closure; } - public function generate($fresh = false) + public function generate() { $this->checkConcurrencySupport(); Site::setCurrent(Site::default()->handle()); - if ($fresh) { + if ($this->freshBuild()) { $this->clearDirectory(); } @@ -170,6 +178,11 @@ public function copyFiles() return $this; } + protected function freshBuild() + { + return $this->fresh; + } + protected function createContentFiles() { $request = tap(Request::capture(), function ($request) { From 046101d633654d61dcb24b43b2a8146744a2a74c Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Sun, 25 Jun 2023 13:38:19 +0100 Subject: [PATCH 4/9] Remove redundant method --- src/Generator.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 899d6e4..294258d 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -92,7 +92,7 @@ public function generate() Site::setCurrent(Site::default()->handle()); - if ($this->freshBuild()) { + if ($this->fresh) { $this->clearDirectory(); } @@ -178,11 +178,6 @@ public function copyFiles() return $this; } - protected function freshBuild() - { - return $this->fresh; - } - protected function createContentFiles() { $request = tap(Request::capture(), function ($request) { From 9f385e5bf1ab06a8e0cdf7002dc91c0fa21fa5a0 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 13 Jul 2023 09:57:08 -0400 Subject: [PATCH 5/9] =?UTF-8?q?Refactor=20to=20`=E2=80=94disable-clear`=20?= =?UTF-8?q?as=20discussed.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Commands/StaticSiteGenerate.php | 4 ++-- src/Generator.php | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index 635118f..324fd31 100644 --- a/src/Commands/StaticSiteGenerate.php +++ b/src/Commands/StaticSiteGenerate.php @@ -22,7 +22,7 @@ class StaticSiteGenerate extends Command * * @var string */ - protected $signature = 'statamic:ssg:generate {--workers=} {--fresh}'; + protected $signature = 'statamic:ssg:generate {--workers=} {--disable-clear}'; /** * The console command description. @@ -59,7 +59,7 @@ public function handle() try { $this->generator ->workers($workers ?? 1) - ->fresh($this->option('fresh') ?? false) + ->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 510b750..c80f566 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -33,10 +33,10 @@ class Generator protected $config; protected $request; protected $after; - protected $fresh = false; protected $extraUrls; protected $workers = 1; protected $taskResults; + protected $disableClear = false; public function __construct(Application $app, Filesystem $files, Router $router, Tasks $tasks) { @@ -66,13 +66,6 @@ public function workers(int $workers) return $this; } - public function fresh(bool $fresh = true) - { - $this->fresh = $fresh; - - return $this; - } - public function after($after) { $this->after = $after; @@ -85,18 +78,22 @@ public function addUrls($closure) $this->extraUrls[] = $closure; } + public function disableClear(bool $disableClear = false) + { + $this->disableClear = $disableClear; + + return $this; + } + public function generate() { $this->checkConcurrencySupport(); Site::setCurrent(Site::default()->handle()); - if ($this->fresh) { - $this->clearDirectory(); - } - $this ->bindGlide() + ->clearDirectory() ->createContentFiles() ->createSymlinks() ->copyFiles() @@ -139,6 +136,10 @@ public function bindGlide() public function clearDirectory() { + if ($this->disableClear) { + return $this; + } + $this->files->deleteDirectory($this->config['destination'], true); return $this; From bcf48009766c14c86cab19f74af914fdf792b490 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 13 Jul 2023 13:59:03 -0400 Subject: [PATCH 6/9] Add option descriptions. --- src/Commands/StaticSiteGenerate.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Commands/StaticSiteGenerate.php b/src/Commands/StaticSiteGenerate.php index 324fd31..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=} {--disable-clear}'; + 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. From f3f9a53a3c1ca0162de6961165913ad17bff9e9f Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 13 Jul 2023 14:28:04 -0400 Subject: [PATCH 7/9] =?UTF-8?q?Use=20fcqn=E2=80=99s=20for=20easier=20mocki?= =?UTF-8?q?ng.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ServiceProvider.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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]); }); } From 0554c338fccc85953218b07d1697bdb007ef61c3 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 13 Jul 2023 14:28:22 -0400 Subject: [PATCH 8/9] Add test coverage. --- tests/Concerns/RunsGeneratorCommand.php | 4 ++-- tests/GenerateTest.php | 27 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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..7ed5aca 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,32 @@ public function it_generates_pages_to_custom_destination() $this->cleanUpDestination(); } + /** @test */ + public function it_clears_destination_directory_when_generating_site() + { + $files = $this->partialMock(Filesystem::class); + + $files + ->shouldReceive('deleteDirectory') + ->with(config('statamic.ssg.destination'), true) + ->once(); + + $this->generate(); + } + + /** @test */ + public function it_can_generate_site_without_clearing_destination_directory() + { + $files = $this->partialMock(Filesystem::class); + + $files + ->shouldReceive('deleteDirectory') + ->with(config('statamic.ssg.destination'), true) + ->never(); + + $this->generate(['--disable-clear' => true]); + } + /** @test */ public function it_generates_paginated_pages() { From dbadc0579073fdbd087be3c0da75ed433216e057 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Thu, 13 Jul 2023 15:47:27 -0400 Subject: [PATCH 9/9] Remove unused temp var. --- tests/GenerateTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/GenerateTest.php b/tests/GenerateTest.php index 7ed5aca..7fce21f 100644 --- a/tests/GenerateTest.php +++ b/tests/GenerateTest.php @@ -75,9 +75,8 @@ public function it_generates_pages_to_custom_destination() /** @test */ public function it_clears_destination_directory_when_generating_site() { - $files = $this->partialMock(Filesystem::class); - - $files + $this + ->partialMock(Filesystem::class) ->shouldReceive('deleteDirectory') ->with(config('statamic.ssg.destination'), true) ->once(); @@ -88,9 +87,8 @@ public function it_clears_destination_directory_when_generating_site() /** @test */ public function it_can_generate_site_without_clearing_destination_directory() { - $files = $this->partialMock(Filesystem::class); - - $files + $this + ->partialMock(Filesystem::class) ->shouldReceive('deleteDirectory') ->with(config('statamic.ssg.destination'), true) ->never();