Skip to content

Commit 558bd7e

Browse files
committed
Adding a --sort param to view the command_list sorted in a different way.
Test cases added. Fixes #574
1 parent 02ade65 commit 558bd7e

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

cake/console/shells/command_list.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function main() {
6262
if ($shellList) {
6363
ksort($shellList);
6464
if (empty($this->params['xml'])) {
65-
$this->_asText($shellList);
65+
if (!empty($this->params['sort'])) {
66+
$this->_asSorted($shellList);
67+
} else {
68+
$this->_asText($shellList);
69+
}
6670
} else {
6771
$this->_asXml($shellList);
6872
}
@@ -151,6 +155,44 @@ protected function _asText($shellList) {
151155
$this->out("To get help on a specific command, type <info>cake shell_name --help</info>", 2);
152156
}
153157

158+
/**
159+
* Generates the shell list sorted by where the shells are found.
160+
*
161+
* @return void
162+
*/
163+
protected function _asSorted($shellList) {
164+
$grouped = array();
165+
foreach ($shellList as $shell => $types) {
166+
foreach ($types as $type) {
167+
$type = Inflector::camelize($type);
168+
if (empty($grouped[$type])) {
169+
$grouped[$type] = array();
170+
}
171+
$grouped[$type][] = $shell;
172+
}
173+
}
174+
if (!empty($grouped['App'])) {
175+
sort($grouped['App'], SORT_STRING);
176+
$this->out('[ App ]');
177+
$this->out(' ' . implode(', ', $grouped['App']), 2);
178+
unset($grouped['App']);
179+
}
180+
foreach ($grouped as $section => $shells) {
181+
if ($section == 'CORE') {
182+
continue;
183+
}
184+
sort($shells, SORT_STRING);
185+
$this->out('[ ' . $section . ' ]');
186+
$this->out(' ' . implode(', ', $shells), 2);
187+
}
188+
if (!empty($grouped['CORE'])) {
189+
sort($grouped['CORE'], SORT_STRING);
190+
$this->out('[ Core ]');
191+
$this->out(' ' . implode(', ', $grouped['CORE']), 2);
192+
}
193+
$this->out();
194+
}
195+
154196
/**
155197
* Output as XML
156198
*
@@ -185,6 +227,9 @@ public function getOptionParser() {
185227
->addOption('xml', array(
186228
'help' => __('Get the listing as XML.'),
187229
'boolean' => true
230+
))->addOption('sort', array(
231+
'help' => __('Sorts the commands by where they are located.'),
232+
'boolean' => true
188233
));
189234
}
190235
}

cake/tests/cases/console/shells/command_list.test.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
App::import('Shell', 'CommandList', false);
2121

22-
require_once CAKE . 'console' . DS . 'shell_dispatcher.php';
2322

2423
class TestStringOutput extends ConsoleOutput {
2524
public $output = '';
@@ -110,6 +109,30 @@ function testMain() {
110109
$this->assertPattern($expected, $output);
111110
}
112111

112+
/**
113+
* Test the sort param
114+
*
115+
* @return void
116+
*/
117+
function testSortPlugin() {
118+
$this->Shell->params['sort'] = true;
119+
$this->Shell->main();
120+
121+
$output = $this->Shell->stdout->output;
122+
123+
$expected = "/\[.*App.*\]\n[ ]+sample/";
124+
$this->assertPattern($expected, $output);
125+
126+
$expected = "/\[.*TestPluginTwo.*\]\n[ ]+example, welcome/";
127+
$this->assertPattern($expected, $output);
128+
129+
$expected = "/\[.*TestPlugin.*\]\n[ ]+example/";
130+
$this->assertPattern($expected, $output);
131+
132+
$expected = "/\[.*Core.*\]\n[ ]+acl, api, bake, command_list, console, i18n, schema, testsuite/";
133+
$this->assertPattern($expected, $output);
134+
}
135+
113136
/**
114137
* test xml output.
115138
*

0 commit comments

Comments
 (0)