Skip to content
Snippets Groups Projects
Commit 2c89f493 authored by Alejandro Madrigal's avatar Alejandro Madrigal
Browse files

Completed work for demo generators.

parent 0e95bc2c
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,9 @@ name: Card
props:
type: object
required:
- title
properties:
title:
description: The card title
......
......@@ -2,10 +2,11 @@
namespace Drupal\sdc_styleguide\Drush\Generators;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\sdc\ComponentPluginManager;
use Drupal\sdc\Plugin\Component;
use DrupalCodeGenerator\Asset\AssetCollection as Assets;
use DrupalCodeGenerator\Asset\AssetCollection;
use DrupalCodeGenerator\Attribute\Generator;
use DrupalCodeGenerator\Command\BaseGenerator;
use DrupalCodeGenerator\GeneratorType;
......@@ -33,6 +34,7 @@ class SDCStyleguideDemoGenerator extends BaseGenerator {
public function __construct(
#[Autowire(service: 'plugin.manager.sdc')]
protected readonly ComponentPluginManager $componentPluginManager,
protected readonly FileSystemInterface $fileSystem,
) {
parent::__construct();
}
......@@ -40,34 +42,83 @@ class SDCStyleguideDemoGenerator extends BaseGenerator {
/**
* {@inheritdoc}
*/
protected function generate(array &$vars, Assets $assets): void {
protected function generate(array &$vars, AssetCollection $assets): void {
$ir = $this->createInterviewer($vars);
$definitions = $this->componentPluginManager->getAllComponents();
$components = [];
$componentOptions = [];
foreach ($definitions as $componentDefinition) {
$definition = $componentDefinition->getPluginDefinition();
$components[$componentDefinition->getPluginId()] = [
$option = "{$definition['name']} ({$componentDefinition->getPluginId()})";
$components[$option] = [
'name' => $definition['name'],
'machineName' => $definition['machineName'],
'path' => $definition['path'],
'properties' => $definition['props']['properties'] ?? [],
'required' => $definition['props']['required'] ?? NULL,
'slots' => $definition['slots'] ?? NULL,
'type' => $definition['extension_type'],
'properties' => $definition['props']['properties'],
'slots' => $definition['slots'],
];
$componentOptions[] = $componentDefinition->getPluginId();
$componentOptions[] = $option;
}
sort($componentOptions);
// Ask stuff.
// Get the component to build the demo for.
$choice = $ir->choice($this->t('For what SDC do you want to create a demo?'), $componentOptions);
// @TODO: Validate if the name for the demo was set.
$demoName = $ir->ask($this->t('How would you like to name your demo?'));
$machineName = preg_replace('/[^a-z0-9]/', '_', strtolower($demoName));
$description = $ir->ask($this->t('Please add a description for your demo. (Optional)'));
$selectedComponent = $components[$componentOptions[$choice]];
$properties = [];
foreach ($selectedComponent['properties'] as $name => $property) {
$properties[$name] = $ir->ask("Please set the {$property['title']} value.");
$this->io()->title($this->t('New demo for @demo_name', [
'@demo_name' => $selectedComponent['name'],
]));
// Forces a demo name.
$demoFilename = NULL;
$demoMachineName = NULL;
$ir->ask($this->t('How would you like to name your demo?'), NULL, function ($name) use ($selectedComponent, &$demoMachineName, &$demoFilename) {
if (!$name) {
throw new \Exception($this->t('Please set a value.'));
}
// Confirms a demo with the same name on component folder does not exist.
$demoMachineName = preg_replace('/[^a-z0-9]/', '_', strtolower($name));
$demoFilename = "{$selectedComponent['path']}/{$selectedComponent['machineName']}.demo.{$demoMachineName}.yml";
if (file_exists($demoFilename)) {
throw new \Exception($this->t('A demo with that name already exists. Please use a different name.'));
}
});
// Prepares demo general structure.
$demo = [
'name' => $demoMachineName,
'description' => $ir->ask($this->t('Please add a description for your demo. (Optional)')),
'properties' => [],
'slots' => [],
];
// Fills property values.
if (isset($selectedComponent['properties'])) {
foreach ($selectedComponent['properties'] as $name => $property) {
$required = $selectedComponent['required'];
$demo['properties'][$name] = $ir->ask("Please set the {$property['title']} value. ({$property['description']})", NULL, function ($value) use ($name, $required) {
if (in_array($name, $required) && !$value) {
throw new \Exception($this->t('This property is required. Please set a value.'));
}
// @TODO: Check for type validation. (bool, number, string, attributes).
return $value;
});
}
}
}
// Fills demos.
if (isset($selectedComponent['slots'])) {
foreach ($selectedComponent['slots'] as $name => $slot) {
// @TODO: Ask the user if they want to use Drupal stuff (Nodes, Media, Views, Another SDC Demo) or if they want
// to use a free form string.
$demo['slots'][$name] = $ir->ask("Please set the {$slot['title']} value. ({$slot['description']})");
}
}
// Writes file.
$this->fileSystem->saveData(Yaml::encode($demo), $demoFilename, FileSystemInterface::EXISTS_ERROR);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment