Permalink
Browse files

Merge branch '2.2-shell-command-sort' into 2.2

Closes #GH-695
  • Loading branch information...
2 parents 2f5f1b2 + eb40566 commit 5c29c4d6e7bcec66913e7730fe305616f035044a @markstory markstory committed Jun 23, 2012
@@ -55,18 +55,14 @@ public function main() {
}
$shellList = $this->_getShellList();
+ if (empty($shellList)) {
+ return;
+ }
- if ($shellList) {
- ksort($shellList);
- if (empty($this->params['xml'])) {
- if (!empty($this->params['sort'])) {
- $this->_asSorted($shellList);
- } else {
- $this->_asText($shellList);
- }
- } else {
- $this->_asXml($shellList);
- }
+ if (empty($this->params['xml'])) {
+ $this->_asText($shellList);
+ } else {
+ $this->_asXml($shellList);
}
}
@@ -76,25 +72,26 @@ public function main() {
* @return array
*/
protected function _getShellList() {
- $shellList = array();
$skipFiles = array('AppShell');
+ $plugins = CakePlugin::loaded();
+ $shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
+
$corePath = App::core('Console/Command');
$shells = App::objects('file', $corePath[0]);
$shells = array_diff($shells, $skipFiles);
- $shellList = $this->_appendShells('CORE', $shells, $shellList);
+ $this->_appendShells('CORE', $shells, $shellList);
$appShells = App::objects('Console/Command', null, false);
$appShells = array_diff($appShells, $shells, $skipFiles);
- $shellList = $this->_appendShells('app', $appShells, $shellList);
+ $this->_appendShells('app', $appShells, $shellList);
- $plugins = CakePlugin::loaded();
foreach ($plugins as $plugin) {
$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 @@ protected function _getShellList() {
* @param array $shellList
* @return array
*/
- protected function _appendShells($type, $shells, $shellList) {
+ protected function _appendShells($type, $shells, &$shellList) {
foreach ($shells as $shell) {
- $shell = Inflector::underscore(str_replace('Shell', '', $shell));
- $shellList[$shell][$type] = $type;
+ $shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
}
- return $shellList;
}
/**
@@ -120,76 +115,18 @@ protected function _appendShells($type, $shells, $shellList) {
* @return void
*/
protected function _asText($shellList) {
- if (DS === '/') {
- $width = exec('tput cols') - 2;
- }
- if (empty($width)) {
- $width = 80;
+ foreach ($shellList as $plugin => $commands) {
+ sort($commands);
+ $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
+ $this->out();
}
- $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 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);
}
/**
- * 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
*
* @param array $shellList
@@ -198,17 +135,19 @@ protected function _asSorted($shellList) {
protected function _asXml($shellList) {
$plugins = CakePlugin::loaded();
$shells = new SimpleXmlElement('<shells></shells>');
- foreach ($shellList as $name => $location) {
- $source = current($location);
- $callable = $name;
- if (in_array($source, $plugins)) {
- $callable = Inflector::camelize($source) . '.' . $name;
+ foreach ($shellList as $plugin => $commands) {
+ foreach ($commands as $command) {
+ $callable = $command;
+ if (in_array($plugin, $plugins)) {
+ $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->out($shells->saveXml());
@@ -222,12 +161,13 @@ protected function _asXml($shellList) {
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
+ ->addOption('sort', array(
+ 'help' => __d('cake_console', 'Does nothing (deprecated)'),
+ 'boolean' => true
+ ))
->addOption('xml', array(
'help' => __d('cake_console', 'Get the listing as XML.'),
'boolean' => true
- ))->addOption('sort', array(
- 'help' => __d('cake_console', 'Sorts the commands by where they are located.'),
- 'boolean' => true
));
}
@@ -82,58 +82,16 @@ public function testMain() {
$this->Shell->main();
$output = $this->Shell->stdout->output;
- $expected = "/example \[.*TestPlugin, TestPluginTwo.*\]/";
+ $expected = "/\[.*TestPlugin.*\] example/";
$this->assertRegExp($expected, $output);
- $expected = "/welcome \[.*TestPluginTwo.*\]/";
+ $expected = "/\[.*TestPluginTwo.*\] example, welcome/";
$this->assertRegExp($expected, $output);
- $expected = "/acl \[.*CORE.*\]/";
+ $expected = "/\[.*CORE.*\] acl, api, bake, command_list, console, i18n, schema, test, testsuite, upgrade/";
$this->assertRegExp($expected, $output);
- $expected = "/api \[.*CORE.*\]/";
- $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/";
+ $expected = "/\[.*app.*\] sample/";
$this->assertRegExp($expected, $output);
}

0 comments on commit 5c29c4d

Please sign in to comment.