Skip to content

Commit e2caf5b

Browse files
authored
Merge pull request #928 from hydephp/refactor-RebuildStaticSiteCommand-class
Refactor the RebuildStaticSiteCommand class
2 parents 3031907 + dd52aa2 commit e2caf5b

File tree

4 files changed

+106
-116
lines changed

4 files changed

+106
-116
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Console\Commands;
6+
7+
use Exception;
8+
use Hyde\Console\Concerns\Command;
9+
use Hyde\Foundation\Facades\PageCollection;
10+
use Hyde\Framework\Features\BuildTasks\BuildTask;
11+
use Hyde\Framework\Services\BuildService;
12+
use Hyde\Framework\Services\RebuildService;
13+
use Hyde\Hyde;
14+
use Illuminate\Console\OutputStyle;
15+
use function dirname;
16+
use function file_exists;
17+
use function in_array;
18+
use function str_replace;
19+
use function unslash;
20+
21+
/**
22+
* Hyde Command to build a single static site file.
23+
*
24+
* @see \Hyde\Framework\Testing\Feature\Commands\RebuildStaticSiteCommandTest
25+
*/
26+
class RebuildStaticPageCommand extends Command
27+
{
28+
/** @var string */
29+
protected $signature = 'rebuild {path : The relative file path (example: _posts/hello-world.md)}';
30+
31+
/** @var string */
32+
protected $description = 'Run the static site builder for a single file';
33+
34+
public function handle(): int
35+
{
36+
if ($this->argument('path') === Hyde::getMediaDirectory()) {
37+
(new BuildService($this->getOutput()))->transferMediaAssets();
38+
39+
$this->info('All done!');
40+
41+
return Command::SUCCESS;
42+
}
43+
44+
return $this->makeBuildTask($this->output, $this->getNormalizedPathString())->handle() ?? Command::SUCCESS;
45+
}
46+
47+
protected function getNormalizedPathString(): string
48+
{
49+
return str_replace('\\', '/', unslash($this->argument('path')));
50+
}
51+
52+
protected function makeBuildTask(OutputStyle $output, string $path): BuildTask
53+
{
54+
return new class($output, $path) extends BuildTask
55+
{
56+
public static string $description = 'Rebuilding page';
57+
58+
protected string $path;
59+
60+
public function __construct(OutputStyle $output, string $path)
61+
{
62+
parent::__construct($output);
63+
$this->path = $path;
64+
}
65+
66+
public function run(): void
67+
{
68+
$this->validate();
69+
70+
(new RebuildService($this->path))->execute();
71+
}
72+
73+
public function then(): void
74+
{
75+
$this->createdSiteFile(Command::createClickableFilepath(
76+
PageCollection::getPage($this->path)->getOutputPath()
77+
))->withExecutionTime();
78+
}
79+
80+
protected function validate(): void
81+
{
82+
$directory = Hyde::pathToRelative(dirname($this->path));
83+
84+
$directories = [
85+
Hyde::pathToRelative(Hyde::getBladePagePath()),
86+
Hyde::pathToRelative(Hyde::getBladePagePath()),
87+
Hyde::pathToRelative(Hyde::getMarkdownPagePath()),
88+
Hyde::pathToRelative(Hyde::getMarkdownPostPath()),
89+
Hyde::pathToRelative(Hyde::getDocumentationPagePath()),
90+
];
91+
92+
if (! in_array($directory, $directories)) {
93+
throw new Exception("Path [$this->path] is not in a valid source directory.", 400);
94+
}
95+
96+
if (! file_exists(Hyde::path($this->path))) {
97+
throw new Exception("File [$this->path] not found.", 404);
98+
}
99+
}
100+
};
101+
}
102+
}

packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php

Lines changed: 0 additions & 112 deletions
This file was deleted.

packages/framework/src/Console/HydeConsoleServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function register(): void
2121
Commands\BuildSearchCommand::class,
2222
Commands\BuildSiteCommand::class,
2323
Commands\BuildSitemapCommand::class,
24-
Commands\RebuildStaticSiteCommand::class,
24+
Commands\RebuildStaticPageCommand::class,
2525

2626
Commands\MakePageCommand::class,
2727
Commands\MakePostCommand::class,

packages/framework/tests/Feature/Commands/RebuildStaticSiteCommandTest.php renamed to packages/framework/tests/Feature/Commands/RebuildStaticPageCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
use Hyde\Testing\TestCase;
99

1010
/**
11-
* @covers \Hyde\Console\Commands\RebuildStaticSiteCommand
11+
* @covers \Hyde\Console\Commands\RebuildStaticPageCommand
1212
*/
13-
class RebuildStaticSiteCommandTest extends TestCase
13+
class RebuildStaticPageCommandTest extends TestCase
1414
{
1515
public function test_handle_is_successful_with_valid_path()
1616
{
1717
$this->file('_pages/test-page.md', 'foo');
1818

19-
$this->artisan('rebuild '.'_pages/test-page.md')->assertExitCode(0);
19+
$this->artisan('rebuild _pages/test-page.md')->assertExitCode(0);
2020

2121
$this->assertFileExists(Hyde::path('_site/test-page.html'));
2222

0 commit comments

Comments
 (0)