Skip to content

Commit

Permalink
Prepare files, refactor fieldset stub naming
Browse files Browse the repository at this point in the history
  • Loading branch information
robdekort committed Nov 4, 2021
1 parent 7e35527 commit c67377c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dev/app/Console/Commands/AddBlock.php
Expand Up @@ -94,7 +94,7 @@ protected function checkExistence($type, $path)
*/
protected function createFieldset()
{
$stub = File::get(__DIR__.'/stubs/fieldset.yaml.stub');
$stub = File::get(__DIR__.'/stubs/fieldset_block.yaml.stub');
$contents = Str::of($stub)
->replace('{{ name }}', $this->block_name);

Expand Down
139 changes: 139 additions & 0 deletions dev/app/Console/Commands/AddSet.php
@@ -0,0 +1,139 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Config;
use Statamic\Support\Arr;
use Stringy\StaticStringy as Stringy;
use Symfony\Component\Yaml\Yaml;

class AddSet extends Command
{
use RunsInPlease;

/**
* The name of the console command.
*
* @var string
*/
protected $name = 'peak:add-set';

/**
* The console command description.
*
* @var string
*/
protected $description = "Add an Article (Bard) set.";

/**
* The set name.
*
* @var string
*/
protected $set_name = '';

/**
* The set filename.
*
* @var string
*/
protected $filename = '';

/**
* Execute the console command.
*
* @return bool|null
*/
public function handle()
{
$this->set_name = $this->ask('What should be the name for this set?');
$this->filename = Stringy::slugify($this->set_name, '_', Config::getShortLocale());

try {
$this->checkExistence('Fieldset', "resources/fieldsets/{$this->filename}.yaml");
$this->checkExistence('Partial', "resources/views/page_builder/_{$this->filename}.antlers.html");

$this->createFieldset();
$this->createPartial();
$this->updatePageBuilder();
} catch (\Exception $e) {
return $this->error($e->getMessage());
}

$this->info("Peak page builder block '{$this->block_name}' added.");
}

/**
* Check if a file doesn't already exist.
*
* @return bool|null
*/
protected function checkExistence($type, $path)
{
if (File::exists(base_path($path))) {
throw new \Exception("{$type} '{$path}' already exists.");
}
}

/**
* Create fieldset.
*
* @return bool|null
*/
protected function createFieldset()
{
$stub = File::get(__DIR__.'/stubs/fieldset_set.yaml.stub');
$contents = Str::of($stub)
->replace('{{ name }}', $this->block_name);

File::put(base_path("resources/fieldsets/{$this->filename}.yaml"), $contents);
}

/**
* Create partial.
*
* @return bool|null
*/
protected function createPartial()
{
$stub = File::get(__DIR__.'/stubs/block.html.stub');
$contents = Str::of($stub)
->replace('{{ name }}', $this->block_name)
->replace('{{ filename }}', $this->filename);

File::put(base_path("resources/views/page_builder/_{$this->filename}.antlers.html"), $contents);
}

/**
* Update page_builder.yaml.
*
* @return bool|null
*/
protected function updatePageBuilder()
{
$fieldset = Yaml::parseFile(base_path('resources/fieldsets/page_builder.yaml'));
$newSet = [
'display' => $this->block_name,
'instructions' => $this->instructions,
'fields' => [
[
'import' => $this->filename
]
]
];

$existingSets = Arr::get($fieldset, 'fields.0.field.sets');
$existingSets[$this->filename] = $newSet;
$existingSets = collect($existingSets)->sortBy(function ($value, $key) {
return $key;
})->all();

Arr::set($fieldset, 'fields.0.field.sets', $existingSets);

File::put(base_path('resources/fieldsets/page_builder.yaml'), Yaml::dump($fieldset, 99, 2));
}
}
2 changes: 0 additions & 2 deletions dev/app/Console/Commands/stubs/fieldset.yaml.stub

This file was deleted.

2 changes: 2 additions & 0 deletions dev/app/Console/Commands/stubs/fieldset_block.yaml.stub
@@ -0,0 +1,2 @@
title: '{{ name }}'
fields: [ ]
7 changes: 7 additions & 0 deletions dev/app/Console/Commands/stubs/fieldset_set.yaml.stub
@@ -0,0 +1,7 @@
title: '{{ name }}'
fields:
-
handle: size
field: common.size
config:
instructions: 'The size in which the quote should be displayed.'
9 changes: 9 additions & 0 deletions dev/app/Console/Commands/stubs/set.html.stub
@@ -0,0 +1,9 @@
{{#
@name {{ name }}
@desc The {{ name }} component.
@set page.article.{{ filename }}
#}}

<div class="size-{{ size }} my-4">
<h2>🔧<br>{{ name }}</h2>
</div>
5 changes: 4 additions & 1 deletion dev/starter-kit.yaml
Expand Up @@ -5,15 +5,18 @@ export_paths:
- app/Console/Kernel.php
- app/Console/Commands/AddBlock.php
- app/Console/Commands/AddCollection.php
- app/Console/Commands/AddSet.php
- app/Console/Commands/ClearSite.php
- app/Console/Commands/stubs/block.html.stub
- app/Console/Commands/stubs/collection_blueprint_private_dated.yaml.stub
- app/Console/Commands/stubs/collection_blueprint_private.yaml.stub
- app/Console/Commands/stubs/collection_blueprint_public_dated.yaml.stub
- app/Console/Commands/stubs/collection_blueprint_public.yaml.stub
- app/Console/Commands/stubs/collection.yaml.stub
- app/Console/Commands/stubs/fieldset.yaml.stub
- app/Console/Commands/stubs/fieldset_block.yaml.stub
- app/Console/Commands/stubs/fieldset_set.yaml.stub
- app/Console/Commands/stubs/index.antlers.html.stub
- app/Console/Commands/stubs/set.html.stub
- app/Console/Commands/stubs/show.antlers.html.stub
- app/Http/Controllers/DynamicToken.php
- app/Http/Middleware/VerifyCsrfToken.php
Expand Down

0 comments on commit c67377c

Please sign in to comment.