Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the backend structure of the static page builder command process #72

Merged
merged 28 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e980477
Rename getSourceSlugsOfModels method
caendesilva Apr 7, 2022
4adec4e
Add the media asset service method
caendesilva Apr 7, 2022
801597d
Move shared logic to helper method
caendesilva Apr 8, 2022
3fae508
Move post build actions to method
caendesilva Apr 8, 2022
1f5d842
Move createClickableFilepath method to BuildService
caendesilva Apr 8, 2022
7a8234c
Extract output logic to helper methods
caendesilva Apr 8, 2022
3891817
Update visibility scopes
caendesilva Apr 8, 2022
350dcf4
Move shared output comment to check helper method
caendesilva Apr 8, 2022
0a0d8df
Seperate rebuild service from build service
caendesilva Apr 8, 2022
b643cf7
Add get() method for compatability
caendesilva Apr 8, 2022
289df1f
Refactor code to be more dynamic and reduce boilerplate
caendesilva Apr 8, 2022
b9cc1a8
Add getParserInstance method
caendesilva Apr 8, 2022
b54bddf
Add return missing value
caendesilva Apr 8, 2022
89a0271
Move stub to vendor
caendesilva Apr 8, 2022
122372d
Extract the node command runners
caendesilva Apr 8, 2022
1633e55
Remove HydeMakeValidatorCommand
caendesilva Apr 8, 2022
e8feae7
Restructure command signatures
caendesilva Apr 8, 2022
77d464c
Refactor the build loop to reduce repeating code
caendesilva Apr 8, 2022
0d36a27
Remove verbose output
caendesilva Apr 8, 2022
3c8bc73
Don't actually run the command when testing
caendesilva Apr 8, 2022
0f8b0e8
Simplify trait and integrate it
caendesilva Apr 8, 2022
613f9cb
Refactor code to extract shared logic
caendesilva Apr 8, 2022
42f1cb0
Deprecate command
caendesilva Apr 8, 2022
0c7301d
Remove unnecessary try-catch block
caendesilva Apr 8, 2022
e211070
Seperate the output message from the test condition
caendesilva Apr 8, 2022
ac3126d
Rework how shell_exec commands are mocked
caendesilva Apr 8, 2022
9970af5
Apply fixes from StyleCI
Apr 8, 2022
2378a2a
Merge pull request #73 from hydephp/analysis-AD0W5N
caendesilva Apr 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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