Skip to content

Commit

Permalink
Merge pull request #511 from hydephp/482-skip-generating-searchjson-a…
Browse files Browse the repository at this point in the history
…nd-searchhtml-if-there-are-no-documentation-pages

Skip generating auxiliary files in the main built loop when there is no underlying content
  • Loading branch information
caendesilva committed Jun 4, 2022
2 parents 70d1dd9 + 8b38896 commit 2648e78
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/Commands/HydeBuildStaticSiteCommand.php
Expand Up @@ -12,6 +12,7 @@
use Hyde\Framework\Models\DocumentationPage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Framework\Services\RssFeedService;
use Hyde\Framework\Services\SitemapService;
Expand Down Expand Up @@ -136,15 +137,15 @@ public function runPostBuildActions(): void
$this->runNodeCommand('npm run prod', 'Building frontend assets for production!');
}

if (SitemapService::canGenerateSitemap()) {
if ($this->canGenerateSitemap()) {
Artisan::call('build:sitemap', outputBuffer: $this->output);
}

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

if (Features::hasDocumentationSearch()) {
if ($this->canGenerateSearch()) {
Artisan::call('build:search', outputBuffer: $this->output);
}
}
Expand All @@ -155,14 +156,14 @@ 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,
2
).' seconds. ('.number_format(($execution_time * 1000), 2).'ms)');

$this->info('Congratulations! 🎉 Your static site has been built!');
$this->line(
'Your new homepage is stored here -> '.
DiscoveryService::createClickableFilepath(Hyde::getSiteOutputPath('index.html'))
DiscoveryService::createClickableFilepath(Hyde::getSiteOutputPath('index.html'))
);
}

Expand Down Expand Up @@ -206,4 +207,21 @@ private function runNodeCommand(string $command, string $message, ?string $actio
$output ?? '<fg=red>Could not '.($actionMessage ?? 'run script').'! Is NPM installed?</>'
);
}

protected function canGenerateSitemap(): bool
{
return SitemapService::canGenerateSitemap();
}

protected function canGenerateFeed(): bool
{
return RssFeedService::canGenerateFeed()
&& count(CollectionService::getMarkdownPostList()) > 0;
}

protected function canGenerateSearch(): bool
{
return Features::hasDocumentationSearch()
&& count(CollectionService::getDocumentationPageList()) > 0;
}
}
20 changes: 20 additions & 0 deletions tests/Feature/Commands/BuildStaticSiteCommandTest.php
Expand Up @@ -129,13 +129,33 @@ public function test_rss_feed_is_generated_when_conditions_are_met()
config(['hyde.site_url' => 'https://example.com']);
config(['hyde.generate_rss_feed' => true]);

touch(Hyde::path('_posts/foo.md'));

$this->artisan('build')
->expectsOutput('Generating RSS feed...')
->assertExitCode(0);

unlink(Hyde::path('_site/feed.xml'));
}

public function test_does_not_generate_search_files_when_conditions_are_not_met()
{
$this->artisan('build')
->doesntExpectOutput('Generating documentation site search index...')
->doesntExpectOutput('Generating search page...')
->assertExitCode(0);
}

public function test_generates_search_files_when_conditions_are_met()
{
touch(Hyde::path('_docs/foo.md'));

$this->artisan('build')
->expectsOutput('Generating documentation site search index...')
->expectsOutput('Generating search page...')
->assertExitCode(0);
}

/**
* Added for code coverage, deprecated as the pretty flag is deprecated.
*
Expand Down

0 comments on commit 2648e78

Please sign in to comment.