ソースを参照

Add command to remove plugin assets from app's webroot.

ADmad 8 年 前
コミット
dc1ebe89b5
2 ファイル変更135 行追加0 行削除
  1. 74 0
      src/Shell/Task/AssetsTask.php
  2. 61 0
      tests/TestCase/Shell/Task/AssetsTaskTest.php

+ 74 - 0
src/Shell/Task/AssetsTask.php

@@ -53,6 +53,29 @@ class AssetsTask extends Shell
     }
 
     /**
+     * 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.
      *
      * @param string|null $name Name of plugin for which to symlink assets.
@@ -158,6 +181,55 @@ class AssetsTask extends Shell
     }
 
     /**
+     * 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
      *
      * @param string $dir Directory name
@@ -238,6 +310,8 @@ class AssetsTask extends Shell
             '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,

+ 61 - 0
tests/TestCase/Shell/Task/AssetsTaskTest.php

@@ -204,4 +204,65 @@ class AssetsTaskTest extends TestCase
         $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');
+    }
 }