Skip to content
Permalink
Browse files

Add "overwrite" option for plugin assets symlink/copy operation.

  • Loading branch information...
ADmad committed Jan 21, 2018
1 parent 3ca9ad0 commit 45f4a398b88635f9976e6975d6a2651a56041969
Showing with 82 additions and 14 deletions.
  1. +33 −13 src/Shell/Task/AssetsTask.php
  2. +49 −1 tests/TestCase/Shell/Task/AssetsTaskTest.php
@@ -139,6 +139,8 @@ protected function _list($name = null)
*/
protected function _process($plugins, $copy = false)
{
$overwrite = (bool)$this->param('overwrite');
foreach ($plugins as $plugin => $config) {
$this->out();
$this->out('For plugin: ' . $plugin);
@@ -151,18 +153,25 @@ protected function _process($plugins, $copy = false)
continue;
}
if (file_exists($config['destDir'] . $config['link'])) {
$this->verbose(
$config['destDir'] . $config['link'] . ' already exists',
1
);
continue;
$dest = $config['destDir'] . $config['link'];
if (file_exists($dest)) {
if ($overwrite && !$this->_remove($config)) {
continue;
} elseif (!$overwrite) {
$this->verbose(
$dest . ' already exists',
1
);
continue;
}
}
if (!$copy) {
$result = $this->_createSymlink(
$config['srcPath'],
$config['destDir'] . $config['link']
$dest
);
if ($result) {
continue;
@@ -171,7 +180,7 @@ protected function _process($plugins, $copy = false)
$this->_copyDirectory(
$config['srcPath'],
$config['destDir'] . $config['link']
$dest
);
}
@@ -183,7 +192,7 @@ protected function _process($plugins, $copy = false)
* Remove folder/symlink.
*
* @param array $config Plugin config.
* @return void
* @return bool
*/
protected function _remove($config)
{
@@ -193,7 +202,7 @@ protected function _remove($config)
1
);
return;
return false;
}
$dest = $config['destDir'] . $config['link'];
@@ -204,25 +213,31 @@ protected function _remove($config)
1
);
return;
return false;
}
if (is_link($dest)) {
// @codingStandardsIgnoreLine
if (@unlink($dest)) {
$this->out('Unlinked ' . $dest);
return true;
} else {
$this->err('Failed to unlink ' . $dest);
}
return;
return false;
}
}
$folder = new Folder($dest);
if ($folder->delete()) {
$this->out('Deleted ' . $dest);
return true;
} else {
$this->err('Failed to delete ' . $dest);
return false;
}
}
@@ -312,6 +327,11 @@ public function getOptionParser()
])->addArgument('name', [
'help' => 'A specific plugin you want to symlink assets for.',
'optional' => true,
])
->addOption('overwrite', [
'boolean' => true,
'default' => false,
'help' => 'Overwrite existing symlink / folder.'
]);
return $parser;
@@ -216,7 +216,6 @@ public function testRemoveSymlink()
Plugin::load('Company/TestPluginThree');
mkdir(WWW_ROOT . 'company');
clearstatcache();
$this->Task->symlink();
@@ -258,4 +257,53 @@ public function testRemoveFolder()
rmdir(WWW_ROOT . 'company');
}
/**
* testOverwrite
*
* @return void
*/
public function testOverwrite()
{
Plugin::load('TestPlugin');
Plugin::load('Company/TestPluginThree');
$path = WWW_ROOT . 'test_plugin';
mkdir($path);
$filectime = filectime($path);
sleep(1);
$this->Task->params['overwrite'] = true;
$this->Task->symlink('TestPlugin');
if (DS === '\\') {
$this->assertDirectoryExists($path);
} else {
$this->assertTrue(is_link($path));
}
$newfilectime = filectime($path);
$this->assertTrue($newfilectime !== $filectime);
if (DS === '\\') {
$folder = new Folder($path);
$folder->delete();
} else {
unlink($path);
}
$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
mkdir($path, 0777, true);
$filectime = filectime($path);
sleep(1);
$this->Task->params['overwrite'] = true;
$this->Task->copy('Company/TestPluginThree');
$newfilectime = filectime($path);
$this->assertTrue($newfilectime > $filectime);
$folder = new Folder(WWW_ROOT . 'company');
$folder->delete();
}
}

0 comments on commit 45f4a39

Please sign in to comment.
You can’t perform that action at this time.