Showing the list of shells grouped by plugin

The option to sort has been removed and the list of shells is now
sorted and grouped by plugin and then by command.

Core and app shells are always listed last.
This commit is contained in:
dogmatic69 2012-06-18 21:47:01 +01:00 committed by mark_story
parent 2f5f1b28bc
commit 058f48fc7b
2 changed files with 36 additions and 142 deletions

View file

@ -55,18 +55,14 @@ class CommandListShell extends AppShell {
} }
$shellList = $this->_getShellList(); $shellList = $this->_getShellList();
if (empty($shellList)) {
return;
}
if ($shellList) { if (empty($this->params['xml'])) {
ksort($shellList); $this->_asText($shellList);
if (empty($this->params['xml'])) { } else {
if (!empty($this->params['sort'])) { $this->_asXml($shellList);
$this->_asSorted($shellList);
} else {
$this->_asText($shellList);
}
} else {
$this->_asXml($shellList);
}
} }
} }
@ -76,25 +72,26 @@ class CommandListShell extends AppShell {
* @return array * @return array
*/ */
protected function _getShellList() { protected function _getShellList() {
$shellList = array();
$skipFiles = array('AppShell'); $skipFiles = array('AppShell');
$plugins = CakePlugin::loaded();
$shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
$corePath = App::core('Console/Command'); $corePath = App::core('Console/Command');
$shells = App::objects('file', $corePath[0]); $shells = App::objects('file', $corePath[0]);
$shells = array_diff($shells, $skipFiles); $shells = array_diff($shells, $skipFiles);
$shellList = $this->_appendShells('CORE', $shells, $shellList); $this->_appendShells('CORE', $shells, $shellList);
$appShells = App::objects('Console/Command', null, false); $appShells = App::objects('Console/Command', null, false);
$appShells = array_diff($appShells, $shells, $skipFiles); $appShells = array_diff($appShells, $shells, $skipFiles);
$shellList = $this->_appendShells('app', $appShells, $shellList); $this->_appendShells('app', $appShells, $shellList);
$plugins = CakePlugin::loaded();
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
$pluginShells = App::objects($plugin . '.Console/Command'); $pluginShells = App::objects($plugin . '.Console/Command');
$shellList = $this->_appendShells($plugin, $pluginShells, $shellList); $this->_appendShells($plugin, $pluginShells, $shellList);
} }
return $shellList; return array_filter($shellList);
} }
/** /**
@ -105,12 +102,10 @@ class CommandListShell extends AppShell {
* @param array $shellList * @param array $shellList
* @return array * @return array
*/ */
protected function _appendShells($type, $shells, $shellList) { protected function _appendShells($type, $shells, &$shellList) {
foreach ($shells as $shell) { foreach ($shells as $shell) {
$shell = Inflector::underscore(str_replace('Shell', '', $shell)); $shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
$shellList[$shell][$type] = $type;
} }
return $shellList;
} }
/** /**
@ -120,75 +115,17 @@ class CommandListShell extends AppShell {
* @return void * @return void
*/ */
protected function _asText($shellList) { protected function _asText($shellList) {
if (DS === '/') { foreach ($shellList as $plugin => $commands) {
$width = exec('tput cols') - 2; sort($commands);
$this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
$this->out();
} }
if (empty($width)) {
$width = 80;
}
$columns = max(1, floor($width / 30));
$rows = ceil(count($shellList) / $columns);
foreach ($shellList as $shell => $types) {
sort($types);
$shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
}
$out = array_chunk($shellList, $rows);
for ($i = 0; $i < $rows; $i++) {
$row = '';
for ($j = 0; $j < $columns; $j++) {
if (!isset($out[$j][$i])) {
continue;
}
$row .= $out[$j][$i];
}
$this->out(" " . $row);
}
$this->out();
$this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>")); $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
$this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>")); $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
$this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2); $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
} }
/**
* Generates the shell list sorted by where the shells are found.
*
* @param array $shellList
* @return void
*/
protected function _asSorted($shellList) {
$grouped = array();
foreach ($shellList as $shell => $types) {
foreach ($types as $type) {
$type = Inflector::camelize($type);
if (empty($grouped[$type])) {
$grouped[$type] = array();
}
$grouped[$type][] = $shell;
}
}
if (!empty($grouped['App'])) {
sort($grouped['App'], SORT_STRING);
$this->out('[ App ]');
$this->out(' ' . implode(', ', $grouped['App']), 2);
unset($grouped['App']);
}
foreach ($grouped as $section => $shells) {
if ($section == 'CORE') {
continue;
}
sort($shells, SORT_STRING);
$this->out('[ ' . $section . ' ]');
$this->out(' ' . implode(', ', $shells), 2);
}
if (!empty($grouped['CORE'])) {
sort($grouped['CORE'], SORT_STRING);
$this->out('[ Core ]');
$this->out(' ' . implode(', ', $grouped['CORE']), 2);
}
$this->out();
}
/** /**
* Output as XML * Output as XML
* *
@ -198,17 +135,19 @@ class CommandListShell extends AppShell {
protected function _asXml($shellList) { protected function _asXml($shellList) {
$plugins = CakePlugin::loaded(); $plugins = CakePlugin::loaded();
$shells = new SimpleXmlElement('<shells></shells>'); $shells = new SimpleXmlElement('<shells></shells>');
foreach ($shellList as $name => $location) { foreach ($shellList as $plugin => $commands) {
$source = current($location); foreach ($commands as $command) {
$callable = $name; $callable = $command;
if (in_array($source, $plugins)) { if (in_array($plugin, $plugins)) {
$callable = Inflector::camelize($source) . '.' . $name; $callable = Inflector::camelize($plugin) . '.' . $command;
}
$shell = $shells->addChild('shell');
$shell->addAttribute('name', $command);
$shell->addAttribute('call_as', $callable);
$shell->addAttribute('provider', $plugin);
$shell->addAttribute('help', $callable . ' -h');
} }
$shell = $shells->addChild('shell');
$shell->addAttribute('name', $name);
$shell->addAttribute('call_as', $callable);
$shell->addAttribute('provider', $source);
$shell->addAttribute('help', $callable . ' -h');
} }
$this->stdout->outputAs(ConsoleOutput::RAW); $this->stdout->outputAs(ConsoleOutput::RAW);
$this->out($shells->saveXml()); $this->out($shells->saveXml());
@ -225,9 +164,6 @@ class CommandListShell extends AppShell {
->addOption('xml', array( ->addOption('xml', array(
'help' => __d('cake_console', 'Get the listing as XML.'), 'help' => __d('cake_console', 'Get the listing as XML.'),
'boolean' => true 'boolean' => true
))->addOption('sort', array(
'help' => __d('cake_console', 'Sorts the commands by where they are located.'),
'boolean' => true
)); ));
} }

View file

@ -82,58 +82,16 @@ class CommandListShellTest extends CakeTestCase {
$this->Shell->main(); $this->Shell->main();
$output = $this->Shell->stdout->output; $output = $this->Shell->stdout->output;
$expected = "/example \[.*TestPlugin, TestPluginTwo.*\]/"; $expected = "/\[.*TestPlugin.*\] example/";
$this->assertRegExp($expected, $output); $this->assertRegExp($expected, $output);
$expected = "/welcome \[.*TestPluginTwo.*\]/"; $expected = "/\[.*TestPluginTwo.*\] example, welcome/";
$this->assertRegExp($expected, $output); $this->assertRegExp($expected, $output);
$expected = "/acl \[.*CORE.*\]/"; $expected = "/\[.*CORE.*\] acl, api, bake, command_list, console, i18n, schema, test, testsuite, upgrade/";
$this->assertRegExp($expected, $output); $this->assertRegExp($expected, $output);
$expected = "/api \[.*CORE.*\]/"; $expected = "/\[.*app.*\] sample/";
$this->assertRegExp($expected, $output);
$expected = "/bake \[.*CORE.*\]/";
$this->assertRegExp($expected, $output);
$expected = "/console \[.*CORE.*\]/";
$this->assertRegExp($expected, $output);
$expected = "/i18n \[.*CORE.*\]/";
$this->assertRegExp($expected, $output);
$expected = "/schema \[.*CORE.*\]/";
$this->assertRegExp($expected, $output);
$expected = "/testsuite \[.*CORE.*\]/";
$this->assertRegExp($expected, $output);
$expected = "/sample \[.*app.*\]/";
$this->assertRegExp($expected, $output);
}
/**
* Test the sort param
*
* @return void
*/
public function testSortPlugin() {
$this->Shell->params['sort'] = true;
$this->Shell->main();
$output = $this->Shell->stdout->output;
$expected = "/\[.*App.*\]\\v*[ ]+sample/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*TestPluginTwo.*\]\\v*[ ]+example, welcome/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*TestPlugin.*\]\\v*[ ]+example/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*Core.*\]\\v*[ ]+acl, api, bake, command_list, console, i18n, schema, test, testsuite/";
$this->assertRegExp($expected, $output); $this->assertRegExp($expected, $output);
} }