/
DumpCommand.php
156 lines (133 loc) · 5.75 KB
/
DumpCommand.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
<?php
namespace Bamarni\Symfony\Console\Autocomplete;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class DumpCommand extends Command
{
protected function configure()
{
$this
->setName('dump')
->setDefinition(array(
new InputArgument('script', InputArgument::OPTIONAL, "The script to generate completion for."),
new InputOption('script-options', null, InputOption::VALUE_REQUIRED, "Options to be passed to the script."),
new InputOption('aliases', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, "Extra aliases to be used."),
new InputOption('disable-default-tools', null, InputOption::VALUE_NONE),
new InputOption('shell', null, InputOption::VALUE_REQUIRED, 'Shell type ("bash", "fish" or "zsh")', isset($_SERVER['SHELL']) ? basename($_SERVER['SHELL'], '.exe') : null),
))
->setDescription('Dumps shell autocompletion for any executable based on a Symfony Console Application.')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$shell = $input->getOption('shell');
$script = $input->getArgument('script');
if (!in_array($shell, array('bash', 'zsh', 'fish'))) {
throw new \InvalidArgumentException(sprintf(
'Completion is only available for Bash, Fish and Zsh, "%s" given.',
$shell
));
}
/* =====================================
* DEFAULT SCRIPT
* ===========================================
*/
if (!$script) {
if ($input->getOption('disable-default-tools')) {
$tools = array();
} else {
$tools = array(
'console',
'composer',
'php-cs-fixer',
'phpspec',
'behat',
'robo',
'dep',
'artisan',
);
}
if ($extraTools = $input->getOption('aliases')) {
$extraTools = array_filter(preg_split('/\s+/', implode(' ', $extraTools)));
$tools = array_unique(array_merge($tools, $extraTools));
}
$output->write($this->render($shell . '/default', compact('tools')));
return;
}
/* =====================================
* STATIC SCRIPT
* ===========================================
*/
$scriptOptions = $input->getOption('script-options');
// find all commands
$process = new Process($script . ' list ' . $scriptOptions . ' --format=xml');
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
$xmlCommands = $process->getOutput();
$xml = simplexml_load_string($xmlCommands);
$commands = array();
$commandsDescriptions = array();
$commandsOptionsDescriptions = array();
// find all options
$commandsOptions = array();
$globalOptions = array();
$optionsDescriptions = array();
foreach ($xml->xpath('/symfony/commands/command') as $xmlCommand) {
$command = (string) $xmlCommand['name'];
$commands[] = $command;
$commandsDescriptions[$command] = !empty($xmlCommand->description) ? strip_tags($xmlCommand->description) : null;
$commandsOptionsDescriptions[$command] = array();
$commandOptions = array();
foreach ($xmlCommand->xpath('options/option') as $commandOption) {
$name = (string)$commandOption['name'];
$commandOptions[] = $name;
$optionsDescriptions[$name] = $commandsOptionsDescriptions[$command][$name] = (string)$commandOption->description;
}
$commandsOptions[$command] = $commandOptions;
if ('list' !== $command) {
if (empty($globalOptions)) {
$globalOptions = $commandOptions;
}
$globalOptions = array_intersect($globalOptions, $commandOptions);
}
}
$script = explode('/', $script);
$script = array_pop($script);
$tools = array($script);
if ($extraTools = $input->getOption('aliases')) {
$extraTools = array_filter(preg_split('/\s+/', implode(' ', $extraTools)));
$tools = array_unique(array_merge($tools, $extraTools));
}
$output->write($this->render($shell . '/cached', array(
'script' => $script,
'options_global' => $globalOptions,
'options_command' => $commandsOptions,
'options_descriptions' => $optionsDescriptions,
'commands' => $commands,
'commands_descriptions' => $commandsDescriptions,
'commands_options_descriptions' => $commandsOptionsDescriptions,
'tools' => $tools,
)));
}
private function render($template, $vars)
{
$helpers = array(
'zsh_describe' => function($value, $description = null) {
$value = '"'.str_replace(':', '\\:', $value);
if (!empty($description)) {
$value .= ':'.escapeshellcmd($description);
}
return $value.'"';
}
);
ob_start();
include __DIR__.'/../resources/'.$template.'.php';
return ob_get_clean();
}
}