Skip to content

Commit

Permalink
Add plugin shell.
Browse files Browse the repository at this point in the history
It symlinks / copies plugin's assests to app's webroot.
  • Loading branch information
ADmad committed Dec 20, 2014
1 parent 610c59e commit 37f542d
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Shell/PluginShell.php
@@ -0,0 +1,79 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Shell;

use Cake\Console\Shell;

/**
* Shell for Plugin management.
*
*/
class PluginShell extends Shell {

/**
* Contains tasks to load and instantiate
*
* @var array
*/
public $tasks = ['SymlinkAssets'];

/**
* Override main() for help message hook
*
* @return void
*/
public function main() {
$this->out('<info>Plugin Shell</info>');
$this->hr();
$this->out('[S]ymlink / copy assets to app\'s webroot');
$this->out('[H]elp');
$this->out('[Q]uit');

$choice = strtolower($this->in('What would you like to do?', ['S', 'H', 'Q']));
switch ($choice) {
case 's':
$this->SymlinkAssets->main();
break;
case 'h':
$this->out($this->OptionParser->help());
break;
case 'q':
return $this->_stop();
default:
$this->out('You have made an invalid selection. Please choose a command to execute by entering S, H, or Q.');
}
$this->hr();
$this->main();
}

/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();

$parser->description(
'Plugin Shell symlinks your plugin assets to app\'s webroot.'
)->addSubcommand('symlink_assets', [
'help' => 'Symlink assets to app\'s webroot',
'parser' => $this->SymlinkAssets->getOptionParser()
]);

return $parser;
}

}
94 changes: 94 additions & 0 deletions src/Shell/Task/SymlinkAssetsTask.php
@@ -0,0 +1,94 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Shell\Task;

use Cake\Console\Shell;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Utility\Inflector;

/**
* Language string extractor
*
*/
class SymlinkAssetsTask extends Shell {

/**
* Execution method always used for tasks
*
* @return void
*/
public function main() {
$this->_symlink();
}

/**
* Symlink folder
*
* @return void
*/
protected function _symlink() {
$this->out();
$this->out();
$this->out('Symlinking...');
$this->hr();
$plugins = Plugin::loaded();
foreach ($plugins as $plugin) {
$this->out('For plugin: ' . $plugin);
$this->out();

$link = Inflector::underscore($plugin);
$dir = WWW_ROOT;
if (file_exists($dir . $link)) {
$this->out($link . ' already exists');
$this->out();
continue;
}

if (strpos('/', $link) !== false) {
$parts = explode('/', $link);
$link = array_pop($parts);
$dir = WWW_ROOT . implode(DS, $parts) . DS;
if (!is_dir($dir)) {
$this->out('Creating directory: ' . $dir);
$this->out();
$old = umask(0);
mkdir($dir, 0755, true);
umask($old);
}
}

$path = Plugin::path($plugin) . 'webroot';
$this->out('Creating symlink: ' . $dir);
$this->out();
// @codingStandardsIgnoreStart
$result = @symlink($path, $dir . $link);
// @codingStandardsIgnoreEnd

if (!$result) {
$this->err('Symlink creation failed');
$this->out('Copying to directory:' . $dir);
$this->out();
$folder = new Folder($path);
$folder->copy(['to' => $dir . $link]);
}
}

$this->out();
$this->out('Done.');
}

}
82 changes: 82 additions & 0 deletions tests/TestCase/Shell/Task/SymlinkAssetsTaskTest.php
@@ -0,0 +1,82 @@
<?php
/**
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Shell\Task;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Shell\Task\SymlinkAssetsTask;
use Cake\TestSuite\TestCase;

/**
* SymlinkAssetsTask class
*
*/
class SymlinkAssetsTaskTest extends TestCase {

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock(
'Cake\Shell\Task\SymlinkAssetsTask',
array('in', 'out', 'err', '_stop'),
array($this->io)
);
}

/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Task);
Plugin::unload();
}

/**
* testExecute method
*
* @return void
*/
public function testExecute() {
Plugin::load('TestPlugin');
Plugin::load('Company/TestPluginThree');

$this->Task->main();

$path = WWW_ROOT . 'test_plugin';
$link = new \SplFileInfo($path);
$this->assertTrue($link->isLink());
$this->assertTrue(file_exists($path . DS . 'root.js'));
unlink($path);

$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
$link = new \SplFileInfo($path);
$this->assertTrue($link->isLink());
$this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
$folder = new Folder(WWW_ROOT . 'company');
$folder->delete();
}

}

0 comments on commit 37f542d

Please sign in to comment.