zap/app/Commands/BuildCommand.php

72 lines
1.9 KiB
PHP

<?php
namespace App\Commands;
use App\Build\SiteParser;
use Exception;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Str;
use Parsedown;
use function Illuminate\Filesystem\join_paths;
class BuildCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'build';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Build your site. Local is default';
/**
* Execute the console command.
*/
public function handle()
{
// Clear the build directory
// Storage::delete(Storage::disk('build_local')->files());
// Storage::delete(Storage::disk('build_local')->directories());
$siteParser = new SiteParser;
$pages = $siteParser->getPages();
// Parse and output
foreach ($pages as $p) {
View::share('page', $p);
try {
$rendered = $p->render();
} catch (Exception $e) {
$this->error("Failed on: " . $p->path);
throw $e;
}
Storage::disk('build_local')->put($p->outputPath(), $rendered);
}
// Copy static assets folder
foreach (Storage::disk('source')->allFiles('assets/static') as $asset) {
$targetPath = str($asset)->replace("assets/static", "assets")->toString();
// Use Storage facade for both source and target
$content = Storage::disk('source')->get($asset);
Storage::disk('build_local')->put($targetPath, $content);
}
$this->info("Site built");
$this->notify("Site Zapped", "Your site has been built");
}
}