Skip to content

Commit

Permalink
Starting to move command list out to a separate class so ShellDispatc…
Browse files Browse the repository at this point in the history
…her can stop having stderr/stdout connections.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 317e32f commit 02c4e00
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 1 deletion.
102 changes: 102 additions & 0 deletions cake/console/libs/command_list.php
@@ -0,0 +1,102 @@
<?php
/**
* CommandListTest file
*
* PHP 5
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2006-2010, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @package cake
* @subpackage cake.tests.cases.console.libs
* @since CakePHP v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

/**
* Shows a list of commands available from the console.
*
* @package cake.console.libs
*/
class CommandListShell extends Shell {
/**
* Main function Prints out the list of shells.
*
* @return void
*/
public function main() {
$this->out("\nWelcome to CakePHP v" . Configure::version() . " Console");
$this->out("---------------------------------------------------------------");
$this->out("Current Paths:");
$this->out(" -app: ". $this->params['app']);
$this->out(" -working: " . rtrim($this->params['working'], DS));
$this->out(" -root: " . rtrim($this->params['root'], DS));
$this->out(" -core: " . rtrim(CORE_PATH, DS));
$this->out("");
$this->out("Changing Paths:");
$this->out("your working path should be the same as your application path");
$this->out("to change your path use the '-app' param.");
$this->out("Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp");

$this->out("\nAvailable Shells:");
$shellList = array();
foreach ($this->Dispatch->shellPaths as $path) {
if (!is_dir($path)) {
continue;
}
$shells = App::objects('file', $path);
if (empty($shells)) {
continue;
}
if (preg_match('@plugins[\\\/]([^\\\/]*)@', $path, $matches)) {
$type = Inflector::camelize($matches[1]);
} elseif (preg_match('@([^\\\/]*)[\\\/]vendors[\\\/]@', $path, $matches)) {
$type = $matches[1];
} elseif (strpos($path, CAKE_CORE_INCLUDE_PATH . DS . 'cake') === 0) {
$type = 'CORE';
} else {
$type = 'app';
}
foreach ($shells as $shell) {
if ($shell !== 'shell.php') {
$shell = str_replace('.php', '', $shell);
$shellList[$shell][$type] = $type;
}
}
}
if ($shellList) {
ksort($shellList);
if (DS === '/') {
$width = exec('tput cols') - 2;
}
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("\nTo run a command, type 'cake shell_name [args]'");
$this->out("To get help on a specific command, type 'cake shell_name help'");
}
}
2 changes: 1 addition & 1 deletion cake/console/shell_dispatcher.php
Expand Up @@ -353,7 +353,6 @@ public function dispatch() {
return $Shell->main();
}
}

throw new MissingShellMethodException(array('shell' => $this->shell, 'method' => $arg));
}

Expand Down Expand Up @@ -548,6 +547,7 @@ public function shiftArgs() {
*
*/
public function help() {
// Make Command List and display it here.
$this->clear();
$this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
$this->stdout("---------------------------------------------------------------");
Expand Down
112 changes: 112 additions & 0 deletions cake/tests/cases/console/libs/command_list.test.php
@@ -0,0 +1,112 @@
<?php
/**
* CommandList file
*
* PHP 5
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2006-2010, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @package cake
* @subpackage cake.tests.cases.console.libs
* @since CakePHP v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Shell', 'CommandList', false);

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

class TestStringOutput extends ConsoleOutput {
public $output = '';

protected function _write($message) {
$this->output .= $message;
}
}

class CommandListTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
App::build(array(
'plugins' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
),
'shells' => array(
CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
)
), true);
App::objects('plugins', null, false);

$this->Dispatcher = $this->getMock(
'ShellDispatcher',
array('getInput', 'stderr', '_stop', '_initEnvironment', 'dispatch', 'clear')
);
$this->Dispatcher->stdout = new TestStringOutput();

$this->Shell = $this->getMock(
'CommandListShell',
array('in', '_stop'),
array(&$this->Dispatcher)
);
}

/**
* teardown
*
* @return void
*/
function tearDown() {
unset($this->Dispatcher, $this->Shell);
}

/**
* test that main finds core shells.
*
* @return void
*/
function testMain() {
$this->Shell->main();
$output = $this->Dispatcher->stdout->output;

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

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

$expected = "/acl \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/api \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/bake \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/console \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/i18n \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/schema \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

$expected = "/testsuite \[.*CORE.*\]/";
$this->assertPattern($expected, $output);

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

0 comments on commit 02c4e00

Please sign in to comment.