Skip to content

Commit

Permalink
Merge pull request #72 from hydephp/build-service-refactor
Browse files Browse the repository at this point in the history
Refactor the backend structure of the static page builder command process
  • Loading branch information
caendesilva authored Apr 8, 2022
2 parents 581d068 + 2378a2a commit ecc3748
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 349 deletions.
10 changes: 4 additions & 6 deletions config/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@
NunoMaduro\LaravelConsoleSummary\SummaryCommand::class,
Symfony\Component\Console\Command\DumpCompletionCommand::class,
Symfony\Component\Console\Command\HelpCommand::class,
Illuminate\Console\Scheduling\ScheduleRunCommand::class,
Illuminate\Console\Scheduling\ScheduleListCommand::class,
Illuminate\Console\Scheduling\ScheduleFinishCommand::class,
LaravelZero\Framework\Commands\StubPublishCommand::class,
Hyde\Framework\Commands\HydeMakeValidatorCommand::class,
Hyde\Framework\Commands\HydePublishStubsCommand::class,
Hyde\Framework\Commands\HydeDebugCommand::class,
],

Expand All @@ -80,6 +74,10 @@

'remove' => [
Illuminate\Console\Scheduling\ScheduleRunCommand::class,
Illuminate\Console\Scheduling\ScheduleListCommand::class,
Illuminate\Console\Scheduling\ScheduleFinishCommand::class,
Illuminate\Console\Scheduling\ScheduleRunCommand::class,
LaravelZero\Framework\Commands\StubPublishCommand::class,
],

];
2 changes: 1 addition & 1 deletion src/Actions/GeneratesDocumentationSidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function get(string $current = ''): array

$array = [];

foreach (CollectionService::getSourceSlugsOfModels(DocumentationPage::class) as $slug) {
foreach (CollectionService::getSourceFileListForModel(DocumentationPage::class) as $slug) {
if ($slug == 'index') {
continue;
}
Expand Down
233 changes: 101 additions & 132 deletions src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

use Exception;
use Hyde\Framework\Actions\CreatesDefaultDirectories;
use Hyde\Framework\DocumentationPageParser;
use Hyde\Framework\Commands\Traits\BuildActionRunner;
use Hyde\Framework\Commands\Traits\TransfersMediaAssetsForBuildCommands;
use Hyde\Framework\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\MarkdownPageParser;
use Hyde\Framework\MarkdownPostParser;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\StaticPageBuilder;
use Hyde\Framework\Services\BuildService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
Expand All @@ -24,6 +22,9 @@
*/
class HydeBuildStaticSiteCommand extends Command
{
use BuildActionRunner;
use TransfersMediaAssetsForBuildCommands;

/**
* The signature of the command.
*
Expand Down Expand Up @@ -57,148 +58,51 @@ public function handle(): int

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

if ($this->option('no-api')) {
$this->info('Disabling external API calls, such as Torchlight');
$config = config('hyde.features');
unset($config[array_search('torchlight', $config)]);
Config::set(['hyde.features' => $config]);
}
$this->printInitialInformation();

if ($this->option('clean')) {
if ($this->option('force')) {
$this->purge();
} else {
$this->warn('The --clean option will remove all files in the output directory before building.');
if ($this->confirm(' Are you sure?')) {
$this->purge();
} else {
$this->warn('Aborting.');

return 1;
}
}
if ($this->handleCleanOption() !== 0) {
return 1;
}

$collection = glob(Hyde::path('_media/*.{png,svg,jpg,jpeg,gif,ico,css,js}'), GLOB_BRACE);
$collection = array_merge($collection, [
Hyde::path('resources/frontend/hyde.css'),
Hyde::path('resources/frontend/hyde.js'),
]);
if (sizeof($collection) < 1) {
$this->line('No Media Assets found. Skipping...');
$this->newLine();
} else {
$this->comment('Transferring Media Assets...');
$this->withProgressBar(
$collection,
function ($filepath) {
if ($this->getOutput()->isVeryVerbose()) {
$this->line(' > Copying media file '
.basename($filepath).' to the output media directory');
}
copy($filepath, Hyde::path('_site/media/'.basename($filepath)));
}
);
$this->newLine(2);
}
$this->transferMediaAssets();

if (Features::hasBlogPosts()) {
$collection = CollectionService::getSourceSlugsOfModels(MarkdownPost::class);
if (sizeof($collection) < 1) {
$this->line('No Markdown Posts found. Skipping...');
$this->newLine();
} else {
$this->comment('Creating Markdown Posts...');
$this->withProgressBar(
$collection,
function ($slug) {
(new StaticPageBuilder((new MarkdownPostParser($slug))->get(), true));
}
);
$this->newLine(2);
}
$this->runBuildAction(MarkdownPost::class);
}

if (Features::hasMarkdownPages()) {
$collection = CollectionService::getSourceSlugsOfModels(MarkdownPage::class);
if (sizeof($collection) < 1) {
$this->line('No Markdown Pages found. Skipping...');
$this->newLine();
} else {
$this->comment('Creating Markdown Pages...');
$this->withProgressBar(
$collection,
function ($slug) {
(new StaticPageBuilder((new MarkdownPageParser($slug))->get(), true));
}
);
$this->newLine(2);
}
$this->runBuildAction(MarkdownPage::class);
}

if (Features::hasDocumentationPages()) {
$collection = CollectionService::getSourceSlugsOfModels(DocumentationPage::class);

if (sizeof($collection) < 1) {
$this->line('No Documentation Pages found. Skipping...');
$this->newLine();
} else {
$this->comment('Creating Documentation Pages...');
$this->withProgressBar(
$collection,
function ($slug) {
(new StaticPageBuilder((new DocumentationPageParser($slug))->get(), true));
}
);
$this->newLine(2);
}
$this->runBuildAction(DocumentationPage::class);
}

if (Features::hasBladePages()) {
$collection = CollectionService::getSourceSlugsOfModels(BladePage::class);

if (sizeof($collection) < 1) {
$this->line('No Blade Pages found. Skipping...');
$this->newLine();
} else {
$this->comment('Creating Custom Blade Pages...');
$this->withProgressBar(
$collection,
function ($slug) {
(new StaticPageBuilder((new BladePage($slug)), true));
}
);
$this->newLine(2);
}
$this->runBuildAction(BladePage::class);
}

if ($this->option('pretty')) {
$this->info('Prettifying code! This may take a second.');
try {
$this->line(shell_exec('npx prettier _site/ --write --bracket-same-line'));
} catch (Exception) {
$this->warn('Could not prettify code! Is NPM installed?');
}
}
$this->postBuildActions();

if ($this->option('run-dev')) {
$this->info('Building frontend assets for development! This may take a second.');
try {
$this->line(shell_exec('npm run dev'));
} catch (Exception) {
$this->warn('Could not run script! Is NPM installed?');
}
}
$this->printFinishMessage($time_start);

if ($this->option('run-prod')) {
$this->info('Building frontend assets for production! This may take a second.');
try {
$this->line(shell_exec('npm run prod'));
} catch (Exception) {
$this->warn('Could not run script! Is NPM installed?');
}
return 0;
}

/** @internal */
protected function printInitialInformation(): void
{
if ($this->option('no-api')) {
$this->info('Disabling external API calls');
$config = config('hyde.features');
unset($config[array_search('torchlight', $config)]);
Config::set(['hyde.features' => $config]);
}
}

/** @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(
Expand All @@ -207,18 +111,38 @@ function ($slug) {
).' 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 -> file://'.str_replace(
'\\',
'/',
realpath(Hyde::path('_site/index.html'))
));
$this->line(
'Your new homepage is stored here -> '.
BuildService::createClickableFilepath(Hyde::path('_site/index.html'))
);
}

/** @internal */
protected function handleCleanOption(): int
{
if ($this->option('clean')) {
if ($this->option('force')) {
$this->purge();
} else {
$this->warn('The --clean option will remove all files in the output directory before building.');
if ($this->confirm('Are you sure?')) {
$this->purge();
} else {
$this->warn('Aborting.');

return 1;
}
}
}

return 0;
}

/**
* Clear the entire _site directory before running the build.
*
* @internal
*
* @return void
*/
public function purge()
Expand All @@ -235,4 +159,49 @@ public function purge()

$this->line('</>');
}

/**
* Run any post-build actions.
*
* @return void
*/
public function postBuildActions()
{
if ($this->option('pretty')) {
$this->runNodeCommand(
'npx prettier _site/ --write --bracket-same-line',
'Prettifying code!',
'prettify code'
);
}

if ($this->option('run-dev')) {
$this->runNodeCommand('npm run dev', 'Building frontend assets for development!');
}

if ($this->option('run-prod')) {
$this->runNodeCommand('npm run prod', 'Building frontend assets for production!');
}
}

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

/* @internal */
private 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?</>'
);
}
}
9 changes: 9 additions & 0 deletions src/Commands/HydeDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class HydeDebugCommand extends Command
*/
protected $description = 'Print debug info';

public function __construct()
{
parent::__construct();

if (config('app.env', 'production') !== 'development') {
$this->setHidden(true);
}
}

/**
* Execute the console command.
*
Expand Down
Loading

0 comments on commit ecc3748

Please sign in to comment.