Skip to content

Commit

Permalink
Add --disable-clear option (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Leite <[email protected]>
  • Loading branch information
simonhamp and jesseleite committed Jul 13, 2023
1 parent 898c602 commit 87c5909
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Commands/StaticSiteGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -128,6 +136,10 @@ public function bindGlide()

public function clearDirectory()
{
if ($this->disableClear) {
return $this;
}

$this->files->deleteDirectory($this->config['destination'], true);

return $this;
Expand Down
4 changes: 3 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/RunsGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
25 changes: 25 additions & 0 deletions tests/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Illuminate\Filesystem\Filesystem;
use Statamic\Facades\Config;
use Tests\Concerns\RunsGeneratorCommand;

Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 87c5909

Please sign in to comment.