Skip to content

Commit

Permalink
Adding a --sort param to view the command_list sorted in a different …
Browse files Browse the repository at this point in the history
…way.

Test cases added.
Fixes #574
  • Loading branch information
markstory committed Oct 27, 2010
1 parent 02ade65 commit 558bd7e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
47 changes: 46 additions & 1 deletion cake/console/shells/command_list.php
Expand Up @@ -62,7 +62,11 @@ public function main() {
if ($shellList) {
ksort($shellList);
if (empty($this->params['xml'])) {
$this->_asText($shellList);
if (!empty($this->params['sort'])) {
$this->_asSorted($shellList);
} else {
$this->_asText($shellList);
}
} else {
$this->_asXml($shellList);
}
Expand Down Expand Up @@ -151,6 +155,44 @@ protected function _asText($shellList) {
$this->out("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.
*
* @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
*
Expand Down Expand Up @@ -185,6 +227,9 @@ public function getOptionParser() {
->addOption('xml', array(
'help' => __('Get the listing as XML.'),
'boolean' => true
))->addOption('sort', array(
'help' => __('Sorts the commands by where they are located.'),
'boolean' => true
));
}
}
25 changes: 24 additions & 1 deletion cake/tests/cases/console/shells/command_list.test.php
Expand Up @@ -19,7 +19,6 @@
*/
App::import('Shell', 'CommandList', false);

require_once CAKE . 'console' . DS . 'shell_dispatcher.php';

class TestStringOutput extends ConsoleOutput {
public $output = '';
Expand Down Expand Up @@ -110,6 +109,30 @@ function testMain() {
$this->assertPattern($expected, $output);
}

/**
* Test the sort param
*
* @return void
*/
function testSortPlugin() {
$this->Shell->params['sort'] = true;
$this->Shell->main();

$output = $this->Shell->stdout->output;

$expected = "/\[.*App.*\]\n[ ]+sample/";
$this->assertPattern($expected, $output);

$expected = "/\[.*TestPluginTwo.*\]\n[ ]+example, welcome/";
$this->assertPattern($expected, $output);

$expected = "/\[.*TestPlugin.*\]\n[ ]+example/";
$this->assertPattern($expected, $output);

$expected = "/\[.*Core.*\]\n[ ]+acl, api, bake, command_list, console, i18n, schema, testsuite/";
$this->assertPattern($expected, $output);
}

/**
* test xml output.
*
Expand Down

0 comments on commit 558bd7e

Please sign in to comment.