Skip to content

Commit 8f381de

Browse files
committed
Merge PluginShell and AssetsTask into PluginAssetsShell.
1 parent 7d38183 commit 8f381de

File tree

3 files changed

+108
-92
lines changed

3 files changed

+108
-92
lines changed

src/Shell/Task/AssetsTask.php renamed to src/Shell/PluginAssetsShell.php

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,41 @@
1212
* @since 3.0.0
1313
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1414
*/
15-
namespace Cake\Shell\Task;
15+
namespace Cake\Shell;
1616

1717
use Cake\Console\Shell;
18-
use Cake\Core\App;
1918
use Cake\Core\Plugin;
2019
use Cake\Filesystem\Folder;
2120
use Cake\Utility\Inflector;
2221

2322
/**
24-
* Task for symlinking / copying plugin assets to app's webroot.
23+
* Shell for symlinking / copying plugin assets to app's webroot.
2524
*
2625
*/
27-
class AssetsTask extends Shell {
26+
class PluginAssetsShell extends Shell {
2827

2928
/**
30-
* Execution method always used for tasks
29+
* Attempt to symlink plugin assets to app's webroot. If symlinking fails it
30+
* fallback to copying the assets. For vendor namespaced plugin, parent folder
31+
* for vendor name are created if required.
3132
*
3233
* @return void
3334
*/
34-
public function main() {
35-
$this->_process();
35+
public function symlink() {
36+
$this->_process($this->_list());
3637
}
3738

3839
/**
39-
* Process plugins
40+
* Get list of plugins to process. Plugins without a webroot directory are skipped.
4041
*
41-
* @return void
42+
* @return array
4243
*/
43-
protected function _process() {
44-
$plugins = Plugin::loaded();
45-
foreach ($plugins as $plugin) {
44+
protected function _list() {
45+
$plugins = [];
46+
foreach (Plugin::loaded() as $plugin) {
4647
$path = Plugin::path($plugin) . 'webroot';
4748
if (!is_dir($path)) {
48-
$this->out();
49+
$this->out('', 1, Shell::VERBOSE);
4950
$this->out(
5051
sprintf('Skipping plugin %s. It does not have webroot folder.', $plugin),
5152
2,
@@ -54,32 +55,67 @@ protected function _process() {
5455
continue;
5556
}
5657

57-
$this->out();
58-
$this->out('For plugin: ' . $plugin);
59-
$this->hr();
60-
6158
$link = Inflector::underscore($plugin);
6259
$dir = WWW_ROOT;
63-
60+
$namespaced = false;
6461
if (strpos('/', $link) !== false) {
62+
$namespaced = true;
6563
$parts = explode('/', $link);
6664
$link = array_pop($parts);
6765
$dir = WWW_ROOT . implode(DS, $parts) . DS;
68-
if (!is_dir($dir) && !$this->_createDirectory($dir)) {
69-
continue;
70-
}
7166
}
7267

73-
if (file_exists($dir . $link)) {
74-
$this->out($link . ' already exists', 1, Shell::VERBOSE);
68+
$plugins[$plugin] = [
69+
'srcPath' => Plugin::path($plugin) . 'webroot',
70+
'destDir' => $dir,
71+
'link' => $link,
72+
'namespaced' => $namespaced
73+
];
74+
}
75+
return $plugins;
76+
}
77+
78+
/**
79+
* Process plugins
80+
*
81+
* @return void
82+
*/
83+
protected function _process($plugins) {
84+
foreach ($plugins as $plugin => $config) {
85+
$path = Plugin::path($plugin) . 'webroot';
86+
87+
$this->out();
88+
$this->out('For plugin: ' . $plugin);
89+
$this->hr();
90+
91+
if ($config['namespaced'] &&
92+
!is_dir($config['destDir']) &&
93+
!$this->_createDirectory($config['destDir'])
94+
) {
95+
continue;
96+
}
97+
98+
if (file_exists($config['destDir'] . $config['link'])) {
99+
$this->out(
100+
$config['destDir'] . $config['link'] . ' already exists',
101+
1,
102+
Shell::VERBOSE
103+
);
75104
continue;
76105
}
77106

78-
if ($this->_createSymlink($path, $dir . $link)) {
107+
$result = $this->_createSymlink(
108+
$config['srcPath'],
109+
$config['destDir'] . $config['link']
110+
);
111+
if ($result) {
79112
continue;
80113
}
81114

82-
$this->_copyDirectory($path, $dir . $link);
115+
$this->_copyDirectory(
116+
$config['srcPath'],
117+
$config['destDir'] . $config['link']
118+
);
83119
}
84120

85121
$this->out();
@@ -146,4 +182,19 @@ protected function _copyDirectory($source, $destination) {
146182
return false;
147183
}
148184

185+
/**
186+
* Gets the option parser instance and configures it.
187+
*
188+
* @return \Cake\Console\ConsoleOptionParser
189+
*/
190+
public function getOptionParser() {
191+
$parser = parent::getOptionParser();
192+
193+
$parser->addSubcommand('symlink', [
194+
'help' => 'Symlink / copy assets to app\'s webroot'
195+
]);
196+
197+
return $parser;
198+
}
199+
149200
}

src/Shell/PluginShell.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

tests/TestCase/Shell/Task/AssetsTaskTest.php renamed to tests/TestCase/Shell/PluginAssetsShellTest.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
* @since 3.0.0
1313
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1414
*/
15-
namespace Cake\Test\TestCase\Shell\Task;
15+
namespace Cake\Test\TestCase\Shell;
1616

1717
use Cake\Core\App;
1818
use Cake\Core\Configure;
1919
use Cake\Core\Plugin;
2020
use Cake\Filesystem\Folder;
21-
use Cake\Shell\Task\AssetsTask;
21+
use Cake\Shell\PluginAssetsTask;
2222
use Cake\TestSuite\TestCase;
2323

2424
/**
25-
* SymlinkAssetsTask class
25+
* PluginAssetsShellTest class
2626
*
2727
*/
28-
class SymlinkAssetsTaskTest extends TestCase {
28+
class PluginAssetsShellTest extends TestCase {
2929

3030
/**
3131
* setUp method
@@ -36,8 +36,8 @@ public function setUp() {
3636
parent::setUp();
3737
$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
3838

39-
$this->Task = $this->getMock(
40-
'Cake\Shell\Task\AssetsTask',
39+
$this->shell = $this->getMock(
40+
'Cake\Shell\PluginAssetsShell',
4141
array('in', 'out', 'err', '_stop'),
4242
array($this->io)
4343
);
@@ -50,27 +50,48 @@ public function setUp() {
5050
*/
5151
public function tearDown() {
5252
parent::tearDown();
53-
unset($this->Task);
53+
unset($this->shell);
5454
Plugin::unload();
5555
}
5656

5757
/**
58-
* testExecute method
58+
* testSymlink method
5959
*
6060
* @return void
6161
*/
62-
public function testExecute() {
62+
public function testSymlink() {
6363
Plugin::load('TestPlugin');
6464
Plugin::load('Company/TestPluginThree');
6565

66-
$this->Task->main();
66+
$this->shell->symlink();
6767

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

74+
$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
75+
$link = new \SplFileInfo($path);
76+
// If "company" directory exists beforehand "test_plugin_three" would
77+
// be a link. But if the directory is created by the shell itself
78+
// symlinking fails and the assets folder is copied as fallback.
79+
$this->assertTrue($link->isDir());
80+
$this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
81+
$folder = new Folder(WWW_ROOT . 'company');
82+
$folder->delete();
83+
}
84+
85+
/**
86+
* testSymlinkWhenVendorDirectoryExits
87+
*
88+
* @return void
89+
*/
90+
public function testSymlinkWhenVendorDirectoryExits() {
91+
Plugin::load('Company/TestPluginThree');
92+
mkdir(WWW_ROOT . 'company');
93+
94+
$this->shell->symlink();
7495
$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
7596
$link = new \SplFileInfo($path);
7697
$this->assertTrue($link->isLink());
@@ -87,7 +108,7 @@ public function testExecute() {
87108
public function testForPluginWithoutWebroot() {
88109
Plugin::load('TestPluginTwo');
89110

90-
$this->Task->main();
111+
$this->shell->symlink();
91112
$this->assertFalse(file_exists(WWW_ROOT . 'test_plugin_two'));
92113
}
93114

0 commit comments

Comments
 (0)