Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dcf generator for Attributes and D10 #5442

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions src/Commands/generate/Generators/Drush/DrushCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,8 @@ protected function generate(array &$vars): void
}
return $path;
};
$vars['source'] = $this->ask('Absolute path to legacy Drush command file (optional - for porting)', null, $validator);
$vars['class'] = '{machine_name|camelize}Commands';

if ($vars['source']) {
require_once $vars['source'];
$filename = str_replace(['.drush.inc', '.drush8.inc'], '', basename($vars['source']));
$command_hook = $filename . '_drush_command';
if (!function_exists($command_hook)) {
throw new \InvalidArgumentException('Drush command hook "' . $command_hook . '" does not exist.');
}
$commands = call_user_func($filename . '_drush_command');
$vars['commands'] = $this->adjustCommands($commands);
}

$this->addFile('src/Commands/{class}.php', 'drush-command-file.php');

$json = $this->getComposerJson($vars);
Expand Down Expand Up @@ -89,51 +77,4 @@ protected function getOwningModulePath(array $vars): string
}
return $projects[$module_name]->getPath();
}

protected function adjustCommands(array $commands): array
{
foreach ($commands as $name => &$command) {
// Drush9 uses colons in command names. Replace first dash with colon.
$pos = strpos($name, '-');
if ($pos !== false) {
$command['name'] = substr_replace($name, ':', $pos, 1);
}

if ($command['name'] !== $name) {
$command['aliases'][] = $name;
}

$command['method'] = $name;
if (($pos = strpos($name, '-')) !== false) {
$command['method'] = substr($name, $pos + 1);
}
$command['method'] = Utils::camelize(str_replace('-', '_', $command['method']), false);
if ($command['arguments']) {
foreach ($command['arguments'] as $aName => $description) {
// Prepend name with a '$' and replace dashes.
$command['arguments']['$' . Utils::human2machine($aName)] = $description;
unset($command['arguments'][$aName]);
}
$command['argumentsConcat'] = implode(', ', array_keys($command['arguments']));
}
if ($command['options']) {
foreach ($command['options'] as $oName => &$option) {
// We only care about option description so make value a simple string.
if (is_array($option)) {
$option = $option['description'];
}
$oNames[] = "'$oName' => null";
}
$command['optionsConcat'] = 'array $options = [' . implode(', ', $oNames) . ']';
if (!empty($command['arguments'])) {
$command['optionsConcat'] = ', ' . $command['optionsConcat'];
}
unset($oNames);
}
if ($deps = $command['drupal dependencies']) {
$command['depsConcat'] = implode(',', $deps);
}
}
return $commands;
}
}
49 changes: 0 additions & 49 deletions src/Commands/generate/Generators/Drush/default-methods.php.twig

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Drupal\{{ machine_name }}\Commands;

{% if not source %}
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
{% endif %}
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;

/**
Expand All @@ -18,11 +17,41 @@ use Drush\Commands\DrushCommands;
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class {{ class }} extends DrushCommands {
final class {{ class }} extends DrushCommands {

{% if source %}
{% include 'ported-methods.php.twig' %}
{% else %}
{% include 'default-methods.php.twig' %}
{% endif %}
/**
* Command description here.
*/
#[CLI\Command(name: '{{ machine_name }}:commandName', aliases: ['foo'])]
#[CLI\Argument(name: 'arg1', description: 'Argument description.')]
#[CLI\Option(name: 'option-name', description: 'Option description')]
#[CLI\Usage(name: '{{ machine_name }}:commandName foo', description: 'Usage description')]
public function commandName($arg1, $options = ['option-name' => 'default']) {
$this->logger()->success(dt('Achievement unlocked.'));
}

/**
* An example of the table output format.
*/
#[CLI\Command(name: '{{ machine_name }}:token', aliases: ['token'])]
#[CLI\FieldLabels(labels: [
'group' => 'Group',
'token' => 'Token',
'name' => 'Name'
])]
#[CLI\DefaultTableFields(fields: ['group', 'token', 'name'])]
#[CLI\FilterDefaultField(field: 'name')]
public function token($options = ['format' => 'table']): RowsOfFields {
$all = \Drupal::token()->getInfo();
foreach ($all['tokens'] as $group => $tokens) {
foreach ($tokens as $key => $token) {
$rows[] = [
'group' => $group,
'token' => $key,
'name' => $token['name'],
];
}
}
return new RowsOfFields($rows);
}
}
36 changes: 0 additions & 36 deletions src/Commands/generate/Generators/Drush/ported-methods.php.twig

This file was deleted.