Skip to content

Commit

Permalink
Drupal 11 compatibility (#9)
Browse files Browse the repository at this point in the history
* Allow drush 11 or higher

* Allow drupal 11 or higher

* Adapt recipe generator to support new drupal code generator classes

* Restrict version to 13 or higher as API has changed
  • Loading branch information
omarlopesino authored Dec 30, 2024
1 parent 7f552ec commit b7ba43a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"psr-4": { "kevinquillen\\": "src" }
},
"require": {
"drupal/core": "^10",
"drush/drush": "^11 || ^12.4.3"
"drupal/core": ">=11",
"drush/drush": "^13.0"
}
}
54 changes: 31 additions & 23 deletions src/Drush/Generators/RecipeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

namespace kevinquillen\Drush\Generators;

use DrupalCodeGenerator\Command\DrupalGenerator;
use DrupalCodeGenerator\Asset\AssetCollection;
use DrupalCodeGenerator\Command\BaseGenerator;
use DrupalCodeGenerator\Attribute\Generator;
use DrupalCodeGenerator\Validator\Required;
use Symfony\Component\Console\Question\Question;
use DrupalCodeGenerator\GeneratorType;

/**
* Implements Recipe generator command.
*/
final class RecipeGenerator extends DrupalGenerator {
#[Generator(
name: 'recipe',
description: 'Generates a recipe',
hidden: true,
type: GeneratorType::OTHER,
templatePath: __DIR__ . '/../../../templates'
)]
final class RecipeGenerator extends BaseGenerator {

/**
* This is a temporary workaround until Drupal generator supports Recipes.
Expand All @@ -36,13 +47,6 @@ final class RecipeGenerator extends DrupalGenerator {
*/
protected string $description = 'Generates a Recipe for adding new functionality to Drupal.';

/**
* The path to the templates for the generator.
*
* @var string
*/
protected string $templatePath = __DIR__ . '/../../../templates';

/**
* {@inheritdoc}
*/
Expand All @@ -53,29 +57,30 @@ protected function getExtensionList(): array {
/**
* {@inheritdoc}
*/
protected function generate(array &$vars): void {
$vars['recipe_name'] = $this->ask('What is the name of this recipe?', 'My Custom Recipe', '::validateRequired');
$vars['recipe_directory'] = $this->ask('In what directory should this recipe be saved under /recipes (ex. "my-recipe")?', 'my-custom-recipe', '::validateRequired');
$vars['recipe_type'] = $this->ask('What type of recipe is this (Site, Content Type, Workflow, etc)?', NULL, '::validateRequired');
$vars['recipe_description'] = $this->ask('What does this recipe do?', NULL, '::validateRequired');
protected function generate(array &$vars, AssetCollection $assets): void {
$interviewer = $this->createInterviewer($vars);
$vars['recipe_name'] = $interviewer->ask('What is the name of this recipe?', 'My Custom Recipe', new Required());
$vars['recipe_directory'] = $interviewer->ask('In what directory should this recipe be saved under /recipes (ex. "my-recipe")?', 'my-custom-recipe', new Required());
$vars['recipe_type'] = $interviewer->ask('What type of recipe is this (Site, Content Type, Workflow, etc)?', NULL, new Required());
$vars['recipe_description'] = $interviewer->ask('What does this recipe do?', NULL, new Required());
$vars['composer'] = $this->collectComposerInfo($vars);
$vars['modules'] = $this->collectModules($vars);
$vars['config'] = $this->collectConfig($vars);

if (!empty($vars['composer'])) {
$this->addFile('composer.json', 'composer/composer.json');
$assets->addFile('composer.json', 'composer/composer.json.twig');
}

$this->addFile('recipe.yml', 'recipe/recipe.yml');
$assets->addFile('recipe.yml', 'recipe/recipe.yml.twig');
}

/**
* Returns destination for generated recipes.
*
* This is a temporary workaround until Drupal generator supports Recipes.
*/
public function getDestination(array $vars): ?string {
return $this->drupalContext->getDrupalRoot() . '/recipes/custom/' . $vars['recipe_directory'];
protected function getDestination(array $vars): string {
return \DRUPAL_ROOT . '/recipes/custom/' . $vars['recipe_directory'];
}

/**
Expand All @@ -89,9 +94,10 @@ public function getDestination(array $vars): ?string {
* @return array
*/
protected function collectComposerInfo(array &$vars, bool $default = TRUE): array {
$interviewer = $this->createInterviewer($vars);
$vars['composer'] = [];

if (!$this->confirm('Would you like to add a composer.json file for this recipe? This will let you declare dependencies.', $default)) {
if (!$interviewer->confirm('Would you like to add a composer.json file for this recipe? This will let you declare dependencies.', $default)) {
return $vars['composer'];
}

Expand Down Expand Up @@ -139,9 +145,10 @@ protected function collectComposerInfo(array &$vars, bool $default = TRUE): arra
* @return array
*/
protected function collectModules(array &$vars, bool $default = TRUE): array {
$interviewer = $this->createInterviewer($vars);
$vars['modules'] = [];

if (!$this->confirm('Would you like to add modules to install for this recipe?', $default)) {
if (!$interviewer->confirm('Would you like to add modules to install for this recipe?', $default)) {
return $vars['modules'];
}

Expand Down Expand Up @@ -170,9 +177,10 @@ protected function collectModules(array &$vars, bool $default = TRUE): array {
* @return array
*/
protected function collectConfig(array &$vars, bool $default = TRUE): array {
$interviewer = $this->createInterviewer($vars);
$vars['config'] = [];

if ($this->confirm('Would you like to run specific config imports for this recipe?', $default)) {
if ($interviewer->confirm('Would you like to run specific config imports for this recipe?', $default)) {
while (TRUE) {
$config = [];
$question = new Question('What module do you want to import config for (ex. node)?');
Expand All @@ -182,7 +190,7 @@ protected function collectConfig(array &$vars, bool $default = TRUE): array {
break;
}

if (!$this->confirm("Do you want to import all config for $module (including optional config)?", $default)) {
if (!$interviewer->confirm("Do you want to import all config for $module (including optional config)?", $default)) {
while (TRUE) {
$question = new Question("Enter the config file you want to import for $module (without the .yml extension).");
$filename = $this->io()->askQuestion($question);
Expand All @@ -202,7 +210,7 @@ protected function collectConfig(array &$vars, bool $default = TRUE): array {
}
}

if ($this->confirm('Would you like to run config actions for this recipe?', $default)) {
if ($interviewer->confirm('Would you like to run config actions for this recipe?', $default)) {
// ask for config actions
// ask for action type
}
Expand Down

0 comments on commit b7ba43a

Please sign in to comment.