Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to move command list out to a separate class so ShellDispatc…
…her can stop having stderr/stdout connections.
- Loading branch information
Showing
3 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |