Skip to content

Commit

Permalink
Merge pull request #453 from hydephp/update-build-hooks
Browse files Browse the repository at this point in the history
Refactor auxiliary build services and tasks
  • Loading branch information
caendesilva authored Aug 31, 2022
2 parents 1afa4de + fe20c62 commit c3dc3d0
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 270 deletions.
11 changes: 4 additions & 7 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

### About

Keep an Unreleased section at the top to track upcoming changes.

This serves two purposes:

1. People can see what changes they might expect in upcoming releases
2. At release time, you can move the Unreleased section changes into a new release version section.
This release contains breaking changes regarding the PostBuildTasks that may require your attention if you have created custom tasks.

### Added
- Added the option to define some site configuration settings in a `hyde.yml` file. See [#449](https://github.com/hydephp/develop/pull/449)
Expand All @@ -18,9 +13,11 @@ This serves two purposes:
- This means that no feed.xml will be generated, nor will there be any references (like meta tags) to it when there are no blog posts
- The documentation search related generators are now only enabled when there are documentation pages
- This means that no search.json nor search.html nor any references to them will be generated when there are no documentation pages
- AbstractBuildTask::handle and BuildTaskContract::handle now returns null by default instead of void. It can also return an exit code.
- The way auxiliary build actions are handled internally has been changed, see [PR #453](https://github.com/hydephp/develop/pull/453)

### Deprecated
- for soon-to-be removed features.
- Deprecated ActionCommand.php as it is no longer used

### Removed
- for now removed features.
Expand Down
28 changes: 28 additions & 0 deletions packages/framework/src/Actions/PostBuildTasks/GenerateRssFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Hyde\Framework\Actions\PostBuildTasks;

use Hyde\Framework\Contracts\AbstractBuildTask;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\RssFeedService;

class GenerateRssFeed extends AbstractBuildTask
{
public static string $description = 'Generating RSS feed';

public function run(): void
{
file_put_contents(
Hyde::getSiteOutputPath(RssFeedService::getDefaultOutputFilename()),
RssFeedService::generateFeed()
);
}

public function then(): void
{
$this->writeln(sprintf("\n > Created <info>%s</info> in %s",
RssFeedService::getDefaultOutputFilename(),
$this->getExecutionTime()
));
}
}
56 changes: 56 additions & 0 deletions packages/framework/src/Actions/PostBuildTasks/GenerateSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Hyde\Framework\Actions\PostBuildTasks;

use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Contracts\AbstractBuildTask;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Services\DiscoveryService;

class GenerateSearch extends AbstractBuildTask
{
use InteractsWithDirectories;

public static string $description = 'Generating search index';

public function run(): void
{
$expected = $this->guesstimateGenerationTime();
if ($expected > 1) {
$this->line("<fg=gray> > This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
}

GeneratesDocumentationSearchIndexFile::run();

if (config('docs.create_search_page', true)) {
$outputDirectory = Hyde::pathToRelative(Hyde::getSiteOutputPath(DocumentationPage::getOutputDirectory()));
$this->needsDirectory(Hyde::path($outputDirectory));
file_put_contents(
Hyde::path($outputDirectory.'/search.html'),
view('hyde::pages.documentation-search')->render()
);
$this->write(sprintf(
"\n > Created <info>_site/%s/search.html</info>",
config('docs.output_directory', 'docs')
));
}
}

public function then(): void
{
$this->writeln(sprintf("\n > Created <info>%s</info> in %s",
GeneratesDocumentationSearchIndexFile::$filePath,
$this->getExecutionTime()
));
}

/** @internal Estimated processing time per file in ms */
public static float $guesstimationFactor = 52.5;

protected function guesstimateGenerationTime(): int|float
{
return (int) round(count(DiscoveryService::getDocumentationPageFiles()) * static::$guesstimationFactor) / 1000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
namespace Hyde\Framework\Actions\PostBuildTasks;

use Hyde\Framework\Contracts\AbstractBuildTask;
use Illuminate\Support\Facades\Artisan;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\SitemapService;

class GenerateSitemap extends AbstractBuildTask
{
public static string $description = 'Generating sitemap';

public function run(): void
{
Artisan::call('build:sitemap');
file_put_contents(
Hyde::getSiteOutputPath('sitemap.xml'),
SitemapService::generateSitemap()
);
}

public function then(): void
{
$this->writeln("\n".' > Created <info>sitemap.xml</info> in '.$this->getExecutionTime());
$this->writeln(sprintf("\n > Created <info>sitemap.xml</info> in %s",
$this->getExecutionTime()
));
}
}
43 changes: 6 additions & 37 deletions packages/framework/src/Commands/HydeBuildRssFeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,21 @@

namespace Hyde\Framework\Commands;

use Hyde\Framework\Concerns\ActionCommand;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\RssFeedService;
use Hyde\Framework\Actions\PostBuildTasks\GenerateRssFeed;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to run the Build Process for the RSS Feed.
* Hyde command to run the build process for the RSS feed.
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildRssFeedCommandTest
*/
class HydeBuildRssFeedCommand extends ActionCommand
class HydeBuildRssFeedCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'build:rss {--no-api : (Not yet supported)}';

/**
* The description of the command.
*
* @var string
*/
protected $signature = 'build:rss';
protected $description = 'Generate the RSS feed';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
if (! Features::rss()) {
$this->error('Cannot generate an RSS feed, please check your configuration.');

return 1;
}

$this->action('Generating RSS feed', function () {
file_put_contents(
Hyde::getSiteOutputPath(RssFeedService::getDefaultOutputFilename()),
RssFeedService::generateFeed()
);
}, sprintf('Created <info>%s</info>', RssFeedService::getDefaultOutputFilename()));

return 0;
return (new GenerateRssFeed($this->output))->handle() ?? 0;
}
}
63 changes: 5 additions & 58 deletions packages/framework/src/Commands/HydeBuildSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,21 @@

namespace Hyde\Framework\Commands;

use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
use Hyde\Framework\Concerns\ActionCommand;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSearch;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to run the Build Process for the DocumentationSearchIndex.
* Hyde command to run the build process for the documentation search index.
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildSearchCommandTest
*/
class HydeBuildSearchCommand extends ActionCommand
class HydeBuildSearchCommand extends Command
{
use InteractsWithDirectories;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'build:search';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Generate the docs/search.json';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->action('Generating documentation site search index', function () {
$expected = $this->guesstimateGenerationTime();
if ($expected > 0) {
$this->line("<fg=gray> > This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
}

GeneratesDocumentationSearchIndexFile::run();
}, sprintf('Created <info>%s</info>', GeneratesDocumentationSearchIndexFile::$filePath));

if (config('docs.create_search_page', true)) {
$this->action('Generating search page', function () {
$outputDirectory = Hyde::pathToRelative(Hyde::getSiteOutputPath(DocumentationPage::getOutputDirectory()));
$this->needsDirectory(Hyde::path($outputDirectory));
file_put_contents(
Hyde::path($outputDirectory.'/search.html'),
view('hyde::pages.documentation-search')->render()
);
}, sprintf(
'Created <info>_site/%s/search.html</info>',
config('docs.output_directory', 'docs')
));
}

return 0;
}

/** @internal Estimated processing time per file in ms */
public static float $guesstimationFactor = 52.5;

protected function guesstimateGenerationTime(): int|float
{
return (int) round(count(DiscoveryService::getDocumentationPageFiles()) * static::$guesstimationFactor) / 1000;
return (new GenerateSearch($this->output))->handle() ?? 0;
}
}
61 changes: 5 additions & 56 deletions packages/framework/src/Commands/HydeBuildSitemapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,21 @@

namespace Hyde\Framework\Commands;

use Hyde\Framework\Concerns\ActionCommand;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\SitemapService;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to run the Build Process for the Sitemap.
* Hyde command to run the build process for the sitemap.
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildSitemapCommandTest
*/
class HydeBuildSitemapCommand extends ActionCommand
class HydeBuildSitemapCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'build:sitemap';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Generate the sitemap.xml';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
if (! $this->runPreflightCheck()) {
return 1;
}

$this->action('Generating sitemap', function () {
file_put_contents(
Hyde::getSiteOutputPath('sitemap.xml'),
SitemapService::generateSitemap()
);
}, 'Created <info>sitemap.xml</info>');

return 0;
}

protected function runPreflightCheck(): bool
{
if (! Features::sitemap()) {
$this->error('Cannot generate sitemap.xml, please check your configuration.');

if (! Hyde::hasSiteUrl()) {
$this->warn('Hint: You don\'t have a site URL configured. Check config/hyde.php');
}
if (config('site.generate_sitemap', true) !== true) {
$this->warn('Hint: You have disabled sitemap generation in config/hyde.php');
$this->line(' > You can enable sitemap generation by setting <info>`site.generate_sitemap`</> to <info>`true`</>');
}
if (! extension_loaded('simplexml') || config('testing.mock_disabled_extensions', false) === true) {
$this->warn('Hint: You don\'t have the <info>`simplexml`</> extension installed. Check your PHP installation.');
}

return false;
}

return true;
return (new GenerateSitemap($this->output))->handle() ?? 0;
}
}
13 changes: 4 additions & 9 deletions packages/framework/src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Hyde\Framework\Commands;

use Exception;
use Hyde\Framework\Actions\PostBuildTasks\GenerateRssFeed;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSearch;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\BuildHookService;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Services\DiscoveryService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use LaravelZero\Framework\Commands\Command;

Expand Down Expand Up @@ -115,14 +116,8 @@ public function runPostBuildActions(): void
}

$service->runIf(GenerateSitemap::class, $this->canGenerateSitemap());

if ($this->canGenerateFeed()) {
Artisan::call('build:rss', outputBuffer: $this->output);
}

if ($this->canGenerateSearch()) {
Artisan::call('build:search', outputBuffer: $this->output);
}
$service->runIf(GenerateRssFeed::class, $this->canGenerateFeed());
$service->runIf(GenerateSearch::class, $this->canGenerateSearch());

$service->runPostBuildTasks();
}
Expand Down
Loading

0 comments on commit c3dc3d0

Please sign in to comment.