Skip to content

Commit

Permalink
0.3.1 Move commands to framework
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 22, 2022
1 parent a8ff6ad commit 70dd8df
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 0 deletions.
147 changes: 147 additions & 0 deletions src/Commands/BuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Hyde\Framework\Commands;

use Exception;
use LaravelZero\Framework\Commands\Command;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\DocumentationPageParser;
use Hyde\Framework\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\MarkdownPostParser;
use Hyde\Framework\MarkdownPageParser;
use Hyde\Framework\StaticPageBuilder;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Models\DocumentationPage;

class BuildStaticSiteCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'build {--pretty : Should the build files be prettified?}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Build the static site';


private function debug(array $output)
{
if ($this->getOutput()->isVeryVerbose()) {
$this->newLine();
$this->line("<fg=gray>Created {$output['createdFileSize']} byte file {$output['createdFilePath']}</>");
$this->newLine();
}
}

/**
* Execute the console command.
*
* @return int
* @throws Exception
*/
public function handle(): int
{
$time_start = microtime(true);

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

if ($this->getOutput()->isVeryVerbose()) {
$this->warn('Running with high verbosity');
}

$this->line('Transferring Media Assets...');
$this->withProgressBar(
glob(Hyde::path('_media/*.{png,svg,jpg,jpeg,gif,ico}'), GLOB_BRACE),
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)));
}
);

if (Features::hasBlogPosts()) {
$this->newLine(2);
$this->line('Creating Markdown Posts...');
$this->withProgressBar(
CollectionService::getSourceSlugsOfModels(MarkdownPost::class),
function ($slug) {
$this->debug((new StaticPageBuilder((new MarkdownPostParser($slug))->get(), true))
->getDebugOutput());
}
);
}

if (Features::hasMarkdownPages()) {
$this->newLine(2);
$this->line('Creating Markdown Pages...');
$this->withProgressBar(
CollectionService::getSourceSlugsOfModels(MarkdownPage::class),
function ($slug) {
$this->debug((new StaticPageBuilder((new MarkdownPageParser($slug))->get(), true))
->getDebugOutput());
}
);
}

if (Features::hasDocumentationPages()) {
$this->newLine(2);
$this->line('Creating Documentation Pages...');
$this->withProgressBar(
CollectionService::getSourceSlugsOfModels(DocumentationPage::class),
function ($slug) {
$this->debug((new StaticPageBuilder((new DocumentationPageParser($slug))->get(), true))
->getDebugOutput());
}
);
}

if (Features::hasBladePages()) {
$this->newLine(2);
$this->line('Creating Blade Pages...');
$this->withProgressBar(CollectionService::getSourceSlugsOfModels(BladePage::class), function ($slug) {
$this->debug((new StaticPageBuilder((new BladePage($slug)), true))->getDebugOutput());
});
}

$this->newLine(2);

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

$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)');

$this->info('Congratulations! 🎉 Your static site has been built!');
echo(
"Your new homepage is stored here -> file://" . str_replace(
'\\',
'/',
realpath(Hyde::path('_site/index.html'))
)
);

return 0;
}
}
47 changes: 47 additions & 0 deletions src/Commands/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Hyde\Framework\Commands;

use App\Actions\Installer\Installer;
use Hyde\Framework\Hyde;
use LaravelZero\Framework\Commands\Command;

class Debug extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'debug';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Print debug info';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->info('HydePHP Debug Screen');

$this->newLine();
$this->line('Project directory:');
$this->line(' > ' . Hyde::path());

$this->newLine();

$this->line('Enabled features:');
foreach (config('hyde.features') as $feature) {
$this->line(" - $feature");
}

return 0;
}
}
52 changes: 52 additions & 0 deletions src/Commands/InspireCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Hyde\Framework\Commands;

use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use function Termwind\render;

class InspireCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'inspire {name=Artisan}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Display an inspiring quote';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
render(<<<'HTML'
<div class="py-1 ml-2">
<div class="px-1 bg-blue-300 text-black">Laravel Zero</div>
<em class="ml-1">
Simplicity is the ultimate sophistication.
</em>
</div>
HTML);
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule)
{
// $schedule->command(static::class)->everyMinute();
}
}
81 changes: 81 additions & 0 deletions src/Commands/MakePostCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Hyde\Framework\Commands;

use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Exception;
use LaravelZero\Framework\Commands\Command;

class MakePostCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'make:post
{--force : Should the generated file overwrite existing posts with the same slug?}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Scaffold a new Markdown blog post file';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->title('Creating a new post!');

$this->line('Please enter the title of the post, it will be used to generate the slug.');
$title = $this->ask('What is the title of the post?') ?? 'My New Post';

$this->line('Tip: You can just hit return to use the defaults.');
$description = $this->ask('Write a short post excerpt/description');
$author = $this->ask('What is your (the author\'s) name?');
$category = $this->ask('What is the primary category of the post?');

$this->info('Creating a post with the following details:');
$creator = new CreatesNewMarkdownPostFile(
title: $title,
description: $description,
category: $category,
author: $author
);

$this->line("Title: $creator->title");
$this->line("Description: $creator->description");
$this->line("Author: $creator->author");
$this->line("Category: $creator->category");
$this->line("Date: $creator->date");
$this->line("Slug: $creator->slug");

if (!$this->confirm('Do you wish to continue?', true)) {
$this->info('Aborting.');
return 0;
}

try {
if ($path = $creator->save($this->option('force'))) {
$this->info("Post created! File is saved to $path");
return 0;
} else {
$this->error('Something went wrong when trying to save the file!');
return 1;
}
} catch (Exception $exception) {
$this->error('Something went wrong when trying to save the file!');
$this->warn($exception->getMessage());
if ($exception->getCode() === 409) {
$this->comment('If you want to overwrite the file supply the --force flag.');
return 409;
}
return 1;
}
}
}
59 changes: 59 additions & 0 deletions src/Commands/MakeValidatorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Hyde\Framework\Commands;

use Hyde\Framework\Hyde;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Str;

class MakeValidatorCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'make:validator {--name= : The name of the test} {--force : Overwrite existing files}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Create a validator test';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->info('Creating new Validation Test!');
$name = $this->option('name') ?? $this->ask('What does the validator do?');
$testName = strtolower($name);
$slug = str_replace(' ', '', Str::title($name)) . 'Test.php';

$content =
"<?php
test('{$testName}', function () {
//
})->group('validators');
";

$path = Hyde::path("tests/Validators/$slug");

if (file_exists($path) && !$this->option('force')) {
$this->error('Validator already exists!');
return 409;
}


file_put_contents($path, $content);

$this->line("Created file $path");

return 0;
}
}
Loading

0 comments on commit 70dd8df

Please sign in to comment.