Browse Source

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

ADmad 8 years ago
parent
commit
45f4a398b8
2 changed files with 82 additions and 14 deletions
  1. 33 13
      src/Shell/Task/AssetsTask.php
  2. 49 1
      tests/TestCase/Shell/Task/AssetsTaskTest.php

+ 33 - 13
src/Shell/Task/AssetsTask.php

@@ -139,6 +139,8 @@ class AssetsTask extends Shell
      */
     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 @@ class AssetsTask extends Shell
                 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 @@ class AssetsTask extends Shell
 
             $this->_copyDirectory(
                 $config['srcPath'],
-                $config['destDir'] . $config['link']
+                $dest
             );
         }
 
@@ -183,7 +192,7 @@ class AssetsTask extends Shell
      * Remove folder/symlink.
      *
      * @param array $config Plugin config.
-     * @return void
+     * @return bool
      */
     protected function _remove($config)
     {
@@ -193,7 +202,7 @@ class AssetsTask extends Shell
                 1
             );
 
-            return;
+            return false;
         }
 
         $dest = $config['destDir'] . $config['link'];
@@ -204,25 +213,31 @@ class AssetsTask extends Shell
                 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 @@ class AssetsTask extends Shell
         ])->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;

+ 49 - 1
tests/TestCase/Shell/Task/AssetsTaskTest.php

@@ -216,7 +216,6 @@ class AssetsTaskTest extends TestCase
         Plugin::load('Company/TestPluginThree');
 
         mkdir(WWW_ROOT . 'company');
-        clearstatcache();
 
         $this->Task->symlink();
 
@@ -258,4 +257,53 @@ class AssetsTaskTest extends TestCase
 
         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();
+    }
 }