Skip to content

Commit

Permalink
Add "overwrite" option for plugin assets symlink/copy operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 21, 2018
1 parent 3ca9ad0 commit 45f4a39
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
46 changes: 33 additions & 13 deletions src/Shell/Task/AssetsTask.php
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -171,7 +180,7 @@ protected function _process($plugins, $copy = false)

$this->_copyDirectory(
$config['srcPath'],
$config['destDir'] . $config['link']
$dest
);
}

Expand All @@ -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)
{
Expand All @@ -193,7 +202,7 @@ protected function _remove($config)
1
);

return;
return false;
}

$dest = $config['destDir'] . $config['link'];
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down
50 changes: 49 additions & 1 deletion tests/TestCase/Shell/Task/AssetsTaskTest.php
Expand Up @@ -216,7 +216,6 @@ public function testRemoveSymlink()
Plugin::load('Company/TestPluginThree');

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

$this->Task->symlink();

Expand Down Expand Up @@ -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.