Skip to content

Commit

Permalink
Enhance dcf generator so it can port old Drush commandfiles (#2794)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman authored Jun 5, 2017
1 parent 3517765 commit e6badc0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 41 deletions.
44 changes: 42 additions & 2 deletions src/Commands/generate/Generators/Drush/DrushCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,52 @@ class DrushCommandFile extends BaseGenerator
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$questions = Utils::defaultQuestions();
$questions = Utils::defaultQuestions() + [
'source' => ['Absolute path to legacy Drush commandfile (optional - for porting)', null, ''],
];

$vars = $this->collectVars($input, $output, $questions);
$vars['class'] = Utils::camelize($vars['machine_name'] . 'Commands');

if ($vars['source']) {
require_once $vars['source'];
$filename = str_replace(['.drush.inc', '.drush8.inc'], '', basename($vars['source']));
$commands = call_user_func($filename . '_drush_command');
$vars['commands'] = $this->adjustCommands($commands);
}
$this->setFile('src/Commands/' . $vars['class'] . '.php', 'drush-command-file.twig', $vars);
$this->setServicesFile('drush.services.yml', 'drush.services.twig', $vars);
}

protected function adjustCommands($commands)
{
foreach ($commands as $name => &$command) {
$command['method'] = $name;
if (($pos = strpos($name, '-')) !== false) {
$command['method'] = substr($name, $pos + 1);
}
$command['method'] = Utils::camelize($command['method'], false);
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'] = '$options = [' . implode(', ', $oNames) . ']';
unset($oNames);
}
if ($command['arguments']) {
foreach ($command['arguments'] as $aName => $desciption) {
// Prepend name with a '$'.
$command['arguments']['$' . $aName] = $desciption;
unset($command['arguments'][$aName]);
}
if ($concat = implode(', ', array_keys($command['arguments']))) {
$command['argumentsConcat'] = $concat . ', ';
}
}
}
return $commands;
}
}
40 changes: 40 additions & 0 deletions src/Commands/generate/Generators/Drush/default-methods.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Command description here.
*
* @command {{ machine_name }}-commandName
* @param $arg1 Argument description.
* @option option-name option description
* @usage {{ machine_name }}-commandName foo
* Usage description
* @aliases foo
*/
public function commandName($arg1, $options = ['option-name' => 'default']) {
$this->logger()->success(dt('Achievement unlocked.'));
}

/**
* An example of the table output format.
*
* @command {{ machine_name }}-token
* @aliases token
* @field-labels
* group: Group
* token: Token
* name: Name
* @default-fields group,token,name
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
*/
public function token($options = ['format' => 'table']) {
$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);
}
46 changes: 7 additions & 39 deletions src/Commands/generate/Generators/Drush/drush-command-file.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Drupal\{{ machine_name }}\Commands;
{% if not source %}
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
{% endif %}
use Drush\Commands\DrushCommands;
/**
Expand All @@ -15,44 +17,10 @@ use Drush\Commands\DrushCommands;
*/
class {{ class }} extends DrushCommands {
/**
* Command description here.
*
* @command {{ machine_name }}-commandName
* @param $arg1 Argument description.
* @option option-name option description
* @usage {{ machine_name }}-commandName foo
* Usage description
* @aliases foo
*/
public function commandName($arg1, $options = ['option-name' => 'default']) {
$this->logger()->success(dt('Achievement unlocked.'));
}
{% if source %}
{% include 'ported-methods.twig' %}
{% else %}
{% include 'default-methods.twig' %}
{% endif %}
/**
* An example of the table output format.
*
* @command {{ machine_name }}-token
* @aliases token
* @field-labels
* group: Group
* token: Token
* name: Name
* @default-fields group,token,name
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
*/
public function token($options = ['format' => 'table']) {
$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);
}
}
22 changes: 22 additions & 0 deletions src/Commands/generate/Generators/Drush/ported-methods.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% for key, command in commands %}
/**
* {{ command.description|raw }}
*
* @command {{ key }}
{% for argName, argDescription in command.arguments %}
* @param {{ argName }} {{ argDescription|raw }}
{% endfor %}
{% for optionName, optionDescription in command.options %}
* @option {{ optionName }} {{ optionDescription|raw }}
{% endfor %}
{% for usageName, usageDescription in command.examples %}
* @usage {{ usageName|raw }}
* {{ usageDescription|raw }}
{% endfor %}
* @aliases {{ command.aliases|join(',') }}
*/
public function {{ command.method }}({{ command.argumentsConcat|raw }}{{ command.optionsConcat|raw }}) {

}

{% endfor %}

0 comments on commit e6badc0

Please sign in to comment.