Skip to content

Commit

Permalink
Merge pull request #174 from hydephp/create-build-service
Browse files Browse the repository at this point in the history
Create build service to utilize the router and move logic away from command
  • Loading branch information
caendesilva authored Jul 4, 2022
2 parents e044386 + 20b0218 commit 0beb905
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 143 deletions.
89 changes: 21 additions & 68 deletions packages/framework/src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,25 @@

use Exception;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap;
use Hyde\Framework\Concerns\Internal\BuildActionRunner;
use Hyde\Framework\Concerns\Internal\TransfersMediaAssetsForBuildCommands;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Services\BuildHookService;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Framework\Services\RssFeedService;
use Hyde\Framework\Services\SitemapService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to run the Build Process.
*
* @see \Hyde\Framework\Testing\Feature\Commands\BuildStaticSiteCommandTest
* @see \Hyde\Framework\Testing\Feature\StaticSiteServiceTest
*/
class HydeBuildStaticSiteCommand extends Command
{
use BuildActionRunner;
use TransfersMediaAssetsForBuildCommands;

/**
* The signature of the command.
*
Expand All @@ -51,6 +42,8 @@ class HydeBuildStaticSiteCommand extends Command
*/
protected $description = 'Build the static site';

protected BuildService $service;

/**
* Execute the console command.
*
Expand All @@ -64,27 +57,15 @@ public function handle(): int

$this->title('Building your static site!');

$this->service = new BuildService($this->output);

$this->runPreBuildActions();

$this->cleanOutputDirectory();
$this->service->cleanOutputDirectory();

$this->transferMediaAssets();
$this->service->transferMediaAssets();

if (Features::hasBladePages()) {
$this->runBuildAction(BladePage::class);
}

if (Features::hasMarkdownPages()) {
$this->runBuildAction(MarkdownPage::class);
}

if (Features::hasBlogPosts()) {
$this->runBuildAction(MarkdownPost::class);
}

if (Features::hasDocumentationPages()) {
$this->runBuildAction(DocumentationPage::class);
}
$this->service->compileStaticPages();

$this->runPostBuildActions();

Expand Down Expand Up @@ -152,12 +133,10 @@ public function runPostBuildActions(): void
/** @internal */
protected function printFinishMessage(float $time_start): void
{
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
$this->info('All done! Finished in '.number_format(
$execution_time,
2
).' seconds. ('.number_format(($execution_time * 1000), 2).'ms)');
$execution_time = (microtime(true) - $time_start);
$this->info(sprintf('All done! Finished in %s seconds. (%sms)',
number_format($execution_time, 2), number_format($execution_time * 1000, 2)
));

$this->info('Congratulations! 🎉 Your static site has been built!');
$this->line(
Expand All @@ -166,46 +145,20 @@ protected function printFinishMessage(float $time_start): void
);
}

/**
* Clear the entire output directory before running the build.
*
* @return void
*/
public function cleanOutputDirectory(): void
{
if (config('hyde.empty_output_directory', true)) {
$this->warn('Removing all files from build directory.');
if (! in_array(basename(Hyde::getSiteOutputPath()), config('hyde.safe_output_directories', ['_site', 'docs', 'build']))) {
if (! $this->confirm('The configured output directory ('.Hyde::getSiteOutputPath().') is potentially unsafe to empty. Are you sure you want to continue?')) {
$this->info('Output directory will not be emptied.');

return;
}
}
array_map('unlink', glob(Hyde::getSiteOutputPath('*.{html,json}'), GLOB_BRACE));
File::cleanDirectory(Hyde::getSiteOutputPath('media'));
}
}

/** @internal */
protected function getModelPluralName(string $model): string
{
return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($model)).'s';
}

/* @internal */
protected function runNodeCommand(string $command, string $message, ?string $actionMessage = null): void
{
$this->info($message.' This may take a second.');

if (app()->environment() === 'testing') {
$command = 'echo '.$command;
}
$output = shell_exec($command);

$this->line(
$output ?? '<fg=red>Could not '.($actionMessage ?? 'run script').'! Is NPM installed?</>'
$output = shell_exec(sprintf('%s%s',
app()->environment() === 'testing' ? 'echo ' : '',
$command)
);

$this->line($output ?? sprintf(
'<fg=red>Could not %s! Is NPM installed?</>',
$actionMessage ?? 'run script'
));
}

protected function canGenerateSitemap(): bool
Expand Down
54 changes: 0 additions & 54 deletions packages/framework/src/Concerns/Internal/BuildActionRunner.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
*
* @see \Hyde\Framework\Commands\HydeBuildStaticSiteCommand
* @see \Hyde\Framework\Commands\HydeRebuildStaticSiteCommand
*
* @internal
* @deprecated Use BuildService instead
*/
trait TransfersMediaAssetsForBuildCommands
{
use BuildActionRunner;
use InteractsWithDirectories;

/** @internal */
/** @deprecated */
protected function transferMediaAssets(): void
{
$this->needsDirectory(Hyde::getSiteOutputPath('media'));
Expand All @@ -35,4 +33,9 @@ function ($filepath) {
$this->newLine(2);
}
}

protected function canRunBuildAction(): bool
{
return true;
}
}
6 changes: 6 additions & 0 deletions packages/framework/src/Modules/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public function __construct(PageContract $sourceModel)
$this->routeKey = $this->constructRouteKey();
}

/** @inheritDoc */
public function getPageType(): string
{
return $this->sourceModel::class;
}

/** @inheritDoc */
public function getSourceModel(): PageContract
{
Expand Down
7 changes: 7 additions & 0 deletions packages/framework/src/Modules/Routing/RouteContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ interface RouteContract
*/
public function __construct(PageContract $sourceModel);

/**
* Get the page type for the route.
*
* @return class-string<\Hyde\Framework\Contracts\PageContract>
*/
public function getPageType(): string;

/**
* Get the source model for the route.
*
Expand Down
32 changes: 23 additions & 9 deletions packages/framework/src/Modules/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework\Modules\Routing;

use Hyde\Framework\Contracts\PageContract;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct()
}

/** @inheritDoc */
public static function getInstance(): RouterContract
public static function getInstance(): static
{
return new static();
}
Expand All @@ -63,6 +64,14 @@ public function getRoutes(): Collection
return $this->routes;
}

/** @inheritDoc */
public function getRoutesForModel(string $pageClass): Collection
{
return $this->routes->filter(function (RouteContract $route) use ($pageClass) {
return $route->getSourceModel() instanceof $pageClass;
});
}

protected function discover(PageContract $page): self
{
$route = new Route($page);
Expand All @@ -75,15 +84,20 @@ protected function discoverRoutes(): self
{
$this->routes = new Collection();

$pages = [
BladePage::class,
MarkdownPage::class,
MarkdownPost::class,
DocumentationPage::class,
];
if (Features::hasBladePages()) {
$this->discoverPageRoutes(BladePage::class);
}

if (Features::hasMarkdownPages()) {
$this->discoverPageRoutes(MarkdownPage::class);
}

if (Features::hasBlogPosts()) {
$this->discoverPageRoutes(MarkdownPost::class);
}

foreach ($pages as $page) {
$this->discoverPageRoutes($page);
if (Features::hasDocumentationPages()) {
$this->discoverPageRoutes(DocumentationPage::class);
}

return $this;
Expand Down
8 changes: 8 additions & 0 deletions packages/framework/src/Modules/Routing/RouterContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public static function getInstance(): RouterContract;
* @return \Illuminate\Support\Collection<\Hyde\Framework\Modules\Routing\RouteContract>
*/
public function getRoutes(): Collection;

/**
* Get all discovered routes for the given page class.
*
* @param class-string<\Hyde\Framework\Contracts\PageContract> $pageClass
* @return \Illuminate\Support\Collection<\Hyde\Framework\Modules\Routing\RouteContract>
*/
public function getRoutesForModel(string $pageClass): Collection;
}
Loading

0 comments on commit 0beb905

Please sign in to comment.