/
GenerateCommands.php
162 lines (140 loc) · 5.86 KB
/
GenerateCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Drush\Commands\generate;
use DrupalCodeGenerator\GeneratorDiscovery;
use DrupalCodeGenerator\Helper\Dumper;
use DrupalCodeGenerator\Helper\Renderer;
use DrupalCodeGenerator\TwigEnvironment;
use Drush\Commands\DrushCommands;
use Drush\Commands\generate\Helper\InputHandler;
use Drush\Commands\generate\Helper\OutputHandler;
use Drush\Commands\help\ListCommands;
use Drush\Drush;
use Drush\Drupal\DrushServiceModifier;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Filesystem\Filesystem;
/**
* Drush generate command.
*/
class GenerateCommands extends DrushCommands
{
/**
* Generate boilerplate code for modules/plugins/services etc.
*
* Drush asks questions so that the generated code is as polished as possible. After
* generating, Drush lists the files that were created.
*
* @command generate
* @aliases gen
* @param string $generator A generator name. Omit to pick from available Generators.
* @option answers A JSON string containing pairs of question and answers.
* @option directory Absolute path to a base directory for file writing.
* @usage drush generate
* Pick from available generators and then run it.
* @usage drush generate controller
* Generate a controller class for your module.
* @usage drush generate drush-command-file
* Generate a Drush commandfile for your module.
* @topics docs:generators
* @bootstrap max
*/
public function generate($generator = '', $options = ['answers' => self::REQ, 'directory' => self::REQ])
{
// Disallow default Symfony console commands.
if ($generator == 'help' || $generator == 'list') {
$generator = null;
}
$application = $this->createApplication();
if (!$generator) {
$all = $application->all();
unset($all['help'], $all['list']);
$namespaced = ListCommands::categorize($all, '-');
$preamble = dt('Run `drush generate [command]` and answer a few questions in order to write starter code to your project.');
ListCommands::renderListCLI($application, $namespaced, $this->output(), $preamble);
return null;
} else {
// Symfony console cannot recognize the command by alias when
// multiple commands have the same prefix.
if ($generator == 'module') {
$generator = 'module-standard';
}
// Create an isolated input.
$argv = [
$generator,
'--answers=' . escapeshellarg($options['answers']),
'--directory=' . $options['directory']
];
if ($options['ansi']) {
$argv[] = '--ansi';
}
if ($options['no-ansi']) {
$argv[] = '--no-ansi';
}
return $application->run(new StringInput(implode(' ', $argv)));
}
}
/**
* Creates Drush generate application.
*
* @return \Symfony\Component\Console\Application
* Symfony console application.
*/
protected function createApplication()
{
$application = new Application('Drush generate', Drush::getVersion());
$helperSet = $application->getHelperSet();
$override = null;
if (Drush::affirmative()) {
$override = true;
} elseif (Drush::negative()) {
$override = false;
}
$dumper = new Dumper(new Filesystem(), $override);
$helperSet->set($dumper);
$twig_loader = new \Twig_Loader_Filesystem();
$renderer = new Renderer(new TwigEnvironment($twig_loader));
$helperSet->set($renderer);
$helperSet->set(new InputHandler());
$helperSet->set(new OutputHandler());
// Discover generators.
$discovery = new GeneratorDiscovery(new Filesystem());
/**
* Discover generators.
*/
$dcg_generators = $discovery->getGenerators([DCG_ROOT . '/src/Command/Drupal_8'], '\DrupalCodeGenerator\Command\Drupal_8');
$drush_generators = $discovery->getGenerators([__DIR__ . '/Generators'], '\Drush\Commands\generate\Generators');
$config_paths = $this->getConfig()->get('runtime.commandfile.paths', []);
$global_paths = [];
foreach ($config_paths as $path) {
$global_paths[] = $path . '/Generators';
$global_paths[] = $path . '/src/Generators';
}
$global_paths = array_filter($global_paths, 'file_exists');
$global_generators = $discovery->getGenerators($global_paths, '\Drush\Generators');
$module_generators = [];
if (Drush::bootstrapManager()->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
$container = \Drupal::getContainer();
if ($container->has(DrushServiceModifier::DRUSH_GENERATOR_SERVICES)) {
$module_generators = $container->get(DrushServiceModifier::DRUSH_GENERATOR_SERVICES)->getCommandList();
}
}
/** @var \Symfony\Component\Console\Command\Command[] $generators */
$generators = array_merge($dcg_generators, $drush_generators, $global_generators, $module_generators);
foreach ($generators as $generator) {
$sub_names = explode(':', $generator->getName());
if ($sub_names[0] == 'd8') {
// Remove d8 namespace.
array_shift($sub_names);
}
$new_name = implode('-', $sub_names);
$generator->setName($new_name);
// Remove alias if it is same as new name.
if ($aliases = $generator->getAliases()) {
$generator->setAliases(array_diff($aliases, [$new_name]));
}
}
$application->addCommands($generators);
$application->setAutoExit(false);
return $application;
}
}