Skip to content

Commit

Permalink
Merge pull request #407 from hydephp/move-shared-logic-to-base-class
Browse files Browse the repository at this point in the history
Move shared command logic to base action command class hydephp/develop@28d4644
  • Loading branch information
github-actions committed Aug 11, 2022
1 parent bc37dee commit 6d71dc7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 66 deletions.
29 changes: 8 additions & 21 deletions src/Commands/HydeBuildRssFeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Hyde\Framework\Commands;

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

/**
* Hyde Command to run the Build Process for the RSS Feed.
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildRssFeedCommandTest
*/
class HydeBuildRssFeedCommand extends Command
class HydeBuildRssFeedCommand extends ActionCommand
{
/**
* The signature of the command.
Expand All @@ -35,32 +35,19 @@ class HydeBuildRssFeedCommand extends Command
*/
public function handle(): int
{
/** @var float $actionTime */
$actionTime = microtime(true);

if (! Features::rss()) {
$this->error('Cannot generate an RSS feed, please check your configuration.');

return 1;
}

$this->comment('Generating RSS feed...');

file_put_contents(
Hyde::getSiteOutputPath(RssFeedService::getDefaultOutputFilename()),
RssFeedService::generateFeed()
);

$this->line(sprintf(" > Created <info>%s</info> in %sms\n",
RssFeedService::getDefaultOutputFilename(),
$this->getExecutionTimeInMs($actionTime)
));
$this->action('Generating RSS feed', function () {
file_put_contents(
Hyde::getSiteOutputPath(RssFeedService::getDefaultOutputFilename()),
RssFeedService::generateFeed()
);
}, sprintf('Created <info>%s</info>', RssFeedService::getDefaultOutputFilename()));

return 0;
}

protected function getExecutionTimeInMs(float $timeStart): string
{
return number_format(((microtime(true) - $timeStart) * 1000), 2);
}
}
52 changes: 19 additions & 33 deletions src/Commands/HydeBuildSearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Hyde\Framework\Commands;

use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
use Hyde\Framework\Concerns\ActionCommand;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\DiscoveryService;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to run the Build Process for the DocumentationSearchIndex.
Expand All @@ -14,7 +14,7 @@
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildSearchCommandTest
*/
class HydeBuildSearchCommand extends Command
class HydeBuildSearchCommand extends ActionCommand
{
/**
* The signature of the command.
Expand All @@ -37,50 +37,36 @@ class HydeBuildSearchCommand extends Command
*/
public function handle(): int
{
$actionTime = microtime(true);
$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.</>");
}

$this->comment('Generating documentation site search index...');
$expected = $this->guesstimateGenerationTime();

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

$this->line(' > Created <info>'.GeneratesDocumentationSearchIndexFile::$filePath.'</> in '.
$this->getExecutionTimeInMs($actionTime)."ms\n");
GeneratesDocumentationSearchIndexFile::run();
}, sprintf('Created <info>%s</info>', GeneratesDocumentationSearchIndexFile::$filePath));

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

return 0;
}

protected function createSearchPage(): void
{
$actionTime = microtime(true);

$this->comment('Generating search page...');
file_put_contents(
Hyde::path('_site/'.config('docs.output_directory', 'docs').'/search.html'),
view('hyde::pages.documentation-search')->render()
);

$this->line(' > Created <info>_site/'.config('docs.output_directory', 'docs').'/search.html</> in '.
$this->getExecutionTimeInMs($actionTime)."ms\n");
}

/** @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;
}

protected function getExecutionTimeInMs(float $timeStart): string
{
return number_format(((microtime(true) - $timeStart) * 1000), 2);
}
}
20 changes: 8 additions & 12 deletions src/Commands/HydeBuildSitemapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Hyde\Framework\Commands;

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

/**
* Hyde Command to run the Build Process for the Sitemap.
*
* @see \Hyde\Framework\Testing\Feature\Commands\HydeBuildSitemapCommandTest
*/
class HydeBuildSitemapCommand extends Command
class HydeBuildSitemapCommand extends ActionCommand
{
/**
* The signature of the command.
Expand All @@ -35,15 +35,16 @@ class HydeBuildSitemapCommand extends Command
*/
public function handle(): int
{
$actionTime = microtime(true);

if (! $this->runPreflightCheck()) {
return 1;
}

$this->comment('Generating sitemap...');
file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap());
$this->line(' > Created <info>sitemap.xml</> in '.$this->getExecutionTimeInMs($actionTime)."ms\n");
$this->action('Generating sitemap', function () {
file_put_contents(
Hyde::getSiteOutputPath('sitemap.xml'),
SitemapService::generateSitemap()
);
}, 'Created <info>sitemap.xml</info>');

return 0;
}
Expand All @@ -69,9 +70,4 @@ protected function runPreflightCheck(): bool

return true;
}

protected function getExecutionTimeInMs(float $timeStart): string
{
return number_format(((microtime(true) - $timeStart) * 1000), 2);
}
}
30 changes: 30 additions & 0 deletions src/Concerns/ActionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Hyde\Framework\Concerns;

use LaravelZero\Framework\Commands\Command;

/**
* Base class for commands that run a simple action.
*/
abstract class ActionCommand extends Command
{
protected function action(string $title, \Closure $task, $resultMessage = 'Finished')
{
/** @var float $actionTime */
$actionTime = microtime(true);

$this->comment("$title...");

$result = $task();

$this->line(" > $resultMessage in ".$this->getExecutionTimeInMs($actionTime).'ms');

return $result;
}

protected function getExecutionTimeInMs(float $timeStart): string
{
return number_format(((microtime(true) - $timeStart) * 1000), 2);
}
}

0 comments on commit 6d71dc7

Please sign in to comment.