Skip to content

Commit

Permalink
add --overwrite option to plugin assets copy shell task
Browse files Browse the repository at this point in the history
refs #10151
  • Loading branch information
andrej-griniuk committed Oct 10, 2017
1 parent 73ac195 commit a3697dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Shell/Task/AssetsTask.php
Expand Up @@ -49,7 +49,7 @@ public function symlink($name = null)
*/
public function copy($name = null)
{
$this->_process($this->_list($name), true);
$this->_process($this->_list($name), true, $this->param('overwrite'));
}

/**
Expand Down Expand Up @@ -112,9 +112,10 @@ protected function _list($name = null)
*
* @param array $plugins List of plugins to process
* @param bool $copy Force copy mode. Default false.
* @param bool $overwrite Overwrite existing files.
* @return void
*/
protected function _process($plugins, $copy = false)
protected function _process($plugins, $copy = false, $overwrite = false)
{
foreach ($plugins as $plugin => $config) {
$this->out();
Expand All @@ -128,7 +129,7 @@ protected function _process($plugins, $copy = false)
continue;
}

if (file_exists($config['destDir'] . $config['link'])) {
if (file_exists($config['destDir'] . $config['link']) && !$overwrite) {
$this->out(
$config['destDir'] . $config['link'] . ' already exists',
1,
Expand Down Expand Up @@ -241,6 +242,9 @@ public function getOptionParser()
])->addArgument('name', [
'help' => 'A specific plugin you want to symlink assets for.',
'optional' => true,
])->addOption('overwrite', [
'help' => 'Overwrite existing files.',
'boolean' => true
]);

return $parser;
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/Shell/Task/AssetsTaskTest.php
Expand Up @@ -204,4 +204,35 @@ public function testCopy()
$folder = new Folder(WWW_ROOT . 'company');
$folder->delete();
}

/**
* testCopyOverwrite
*
* @return void
*/
public function testCopyOverwrite()
{
Plugin::load('TestPlugin');

$this->Task->copy();

$path = WWW_ROOT . 'test_plugin';
$dir = new \SplFileInfo($path);
$this->assertTrue($dir->isDir());
$this->assertFileExists($path . DS . 'root.js');

unlink($path . DS . 'root.js');

$this->Task->copy();

$this->assertFileNotExists($path . DS . 'root.js');

$this->Task->params['overwrite'] = true;
$this->Task->copy();

$this->assertFileExists($path . DS . 'root.js');

$folder = new Folder($path);
$folder->delete();
}
}

0 comments on commit a3697dd

Please sign in to comment.