Skip to content

Commit

Permalink
Add command to remove plugin assets from app's webroot.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 19, 2018
1 parent 82fc4b7 commit dc1ebe8
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Shell/Task/AssetsTask.php
Expand Up @@ -52,6 +52,29 @@ public function copy($name = null)
$this->_process($this->_list($name), true);
}

/**
* Remove plugin assets from app's webroot.
*
* @param string|null $name Name of plugin for which to remove assets.
* If null all plugins will be processed.
* @return void
* @since 3.5.11
*/
public function remove($name = null) {
$plugins = $this->_list($name);

foreach ($plugins as $plugin => $config) {
$this->out();
$this->out('For plugin: ' . $plugin);
$this->hr();

$this->_remove($config);
}

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

/**
* Get list of plugins to process. Plugins without a webroot directory are skipped.
*
Expand Down Expand Up @@ -157,6 +180,55 @@ protected function _process($plugins, $copy = false)
$this->out('Done');
}

/**
* Remove folder/symlink.
*
* @param array $config Plugin config.
* @return void
*/
protected function _remove($config)
{
if ($config['namespaced'] && !is_dir($config['destDir'])) {
$this->out(
$config['destDir'] . $config['link'] . ' does not exist',
1,
Shell::VERBOSE
);

return;
}

$dest = $config['destDir'] . $config['link'];

if (!file_exists($dest)) {
$this->out(
$dest . ' does not exist',
1,
Shell::VERBOSE
);

return;
}

if (is_link($dest)) {
// @codingStandardsIgnoreLine
if (@unlink($dest)) {
$this->out('Unlinked ' . $dest);
} else {
$this->err('Failed to unlink ' . $dest);
}

return;
}

$folder = new Folder($dest);
if ($folder->delete()) {
$this->out('Deleted ' . $dest);
} else {
$this->err('Failed to delete ' . $dest);
}
}

/**
* Create directory
*
Expand Down Expand Up @@ -238,6 +310,8 @@ public function getOptionParser()
'help' => 'Symlink (copy as fallback) plugin assets to app\'s webroot.'
])->addSubcommand('copy', [
'help' => 'Copy plugin assets to app\'s webroot.'
])->addSubcommand('remove', [
'help' => 'Remove plugin assets from app\'s webroot.'
])->addArgument('name', [
'help' => 'A specific plugin you want to symlink assets for.',
'optional' => true,
Expand Down
61 changes: 61 additions & 0 deletions tests/TestCase/Shell/Task/AssetsTaskTest.php
Expand Up @@ -204,4 +204,65 @@ public function testCopy()
$folder = new Folder(WWW_ROOT . 'company');
$folder->delete();
}

/**
* testRemoveSymlink method
*
* @return void
*/
public function testRemoveSymlink()
{
if (DS === '\\') {
$this->markTestSkipped(
"Can't test symlink removal on windows."
);
}

Plugin::load('TestPlugin');
Plugin::load('Company/TestPluginThree');

mkdir(WWW_ROOT . 'company');
clearstatcache();

$this->Task->symlink();

$link = new \SplFileInfo(WWW_ROOT . 'test_plugin');
$this->assertTrue($link->isLink());

$link = new \SplFileInfo(WWW_ROOT . 'company' . DS . 'test_plugin_three');
$this->assertTrue($link->isLink());

$this->Task->remove();

$this->assertFalse(is_link(WWW_ROOT . 'test_plugin'));
$this->assertFalse(is_link(WWW_ROOT . 'company' . DS . 'test_plugin_three'));
$this->assertTrue(is_dir(WWW_ROOT . 'company'), 'Ensure namespace folder isn\'t removed');

rmdir(WWW_ROOT . 'company');
}

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

$this->Task->copy();

$this->assertTrue(is_dir(WWW_ROOT . 'test_plugin'));

$this->assertTrue(is_dir(WWW_ROOT . 'company' . DS . 'test_plugin_three'));

$this->Task->remove();

$this->assertFalse(is_dir(WWW_ROOT . 'test_plugin'));
$this->assertFalse(is_dir(WWW_ROOT . 'company' . DS . 'test_plugin_three'));
$this->assertTrue(is_dir(WWW_ROOT . 'company'), 'Ensure namespace folder isn\'t removed');

rmdir(WWW_ROOT . 'company');
}
}

0 comments on commit dc1ebe8

Please sign in to comment.