Browse Source

move plugin assets from shell to command

saeid 6 years ago
parent
commit
385a8398f0

+ 73 - 0
src/Command/PluginAssetsCopyCommand.php

@@ -0,0 +1,73 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * Command for copying plugin assets to app's webroot.
+ */
+class PluginAssetsCopyCommand extends Command
+{
+    use PluginAssetsTrait;
+
+    /**
+     * Execute the command
+     *
+     * Copying plugin assets to app's webroot. For vendor namespaced plugin,
+     * parent folder for vendor name are created if required.
+     * 
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $this->io = $io;
+        $this->args = $args;
+
+        $name = $args->getArgument('name');
+        $overwrite = $args->getOption('overwrite');
+        $this->_process($this->_list($name), true, $overwrite);
+        return static::CODE_SUCCESS;
+    }
+
+    /**
+     * Get the option parser.
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The option parser to update
+     * @return \Cake\Console\ConsoleOptionParser
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser->setDescription([
+            'Copy plugin assets to app\'s webroot.',
+        ])->addArgument('name', [
+            'help' => 'A specific plugin you want to symlink assets for.',
+            'optional' => true,
+        ])->addOption('overwrite', [
+            'help' => 'Overwrite existing symlink / folder / files.',
+            'default' => false,
+            'boolean' => true,
+        ]);
+
+        return $parser;
+    }
+}

+ 78 - 0
src/Command/PluginAssetsRemoveCommand.php

@@ -0,0 +1,78 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * Command for removing plugin assets from app's webroot.
+ */
+class PluginAssetsRemoveCommand extends Command
+{
+    use PluginAssetsTrait;
+
+    /**
+     * Execute the command
+     *
+     * Remove plugin assets from app's webroot.
+     * 
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $this->io = $io;
+        $this->args = $args;
+
+        $name = $args->getArgument('name');
+        $plugins = $this->_list($name);
+
+        foreach ($plugins as $plugin => $config) {
+            $this->io->out();
+            $this->io->out('For plugin: ' . $plugin);
+            $this->io->hr();
+
+            $this->_remove($config);
+        }
+
+        $this->io->out();
+        $this->io->out('Done');
+        return static::CODE_SUCCESS;
+    }
+
+    /**
+     * Get the option parser.
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The option parser to update
+     * @return \Cake\Console\ConsoleOptionParser
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser->setDescription([
+            'Remove plugin assets from app\'s webroot.',
+        ])->addArgument('name', [
+            'help' => 'A specific plugin you want to symlink assets for.',
+            'optional' => true,
+        ]);
+
+        return $parser;
+    }
+}

+ 74 - 0
src/Command/PluginAssetsSymlinkCommand.php

@@ -0,0 +1,74 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * Command for symlinking / copying plugin assets to app's webroot.
+ */
+class PluginAssetsSymlinkCommand extends Command
+{
+    use PluginAssetsTrait;
+
+    /**
+     * Execute the command
+     *
+     * Attempt to symlink plugin assets to app's webroot. If symlinking fails it
+     * fallbacks to copying the assets. For vendor namespaced plugin, parent folder
+     * for vendor name are created if required.
+     * 
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $this->io = $io;
+        $this->args = $args;
+
+        $name = $args->getArgument('name');
+        $overwrite = $args->getOption('overwrite');
+        $this->_process($this->_list($name), false, $overwrite);
+        return static::CODE_SUCCESS;
+    }
+
+    /**
+     * Get the option parser.
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The option parser to update
+     * @return \Cake\Console\ConsoleOptionParser
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser->setDescription([
+            'Symlink (copy as fallback) plugin assets to app\'s webroot.',
+        ])->addArgument('name', [
+            'help' => 'A specific plugin you want to symlink assets for.',
+            'optional' => true,
+        ])->addOption('overwrite', [
+            'help' => 'Overwrite existing symlink / folder / files.',
+            'default' => false,
+            'boolean' => true,
+        ]);
+
+        return $parser;
+    }
+}

+ 30 - 95
src/Shell/Task/AssetsTask.php

@@ -14,7 +14,7 @@ declare(strict_types=1);
  * @since         3.0.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
-namespace Cake\Shell\Task;
+namespace Cake\Command;
 
 use Cake\Console\ConsoleOptionParser;
 use Cake\Console\Shell;
@@ -24,60 +24,24 @@ use Cake\Filesystem\Filesystem;
 use Cake\Utility\Inflector;
 
 /**
- * Task for symlinking / copying plugin assets to app's webroot.
+ * trait for symlinking / copying plugin assets to app's webroot.
+ * @internal
  */
-class AssetsTask extends Shell
+trait PluginAssetsTrait
 {
     /**
-     * Attempt to symlink plugin assets to app's webroot. If symlinking fails it
-     * fallbacks to copying the assets. For vendor namespaced plugin, parent folder
-     * for vendor name are created if required.
+     * Arguments
      *
-     * @param string|null $name Name of plugin for which to symlink assets.
-     *   If null all plugins will be processed.
-     * @return void
+     * @var \Cake\Console\Arguments
      */
-    public function symlink(?string $name = null): void
-    {
-        $this->_process($this->_list($name));
-    }
+    protected $args;
 
     /**
-     * Copying plugin assets to app's webroot. For vendor namespaced plugin,
-     * parent folder for vendor name are created if required.
+     * Console IO
      *
-     * @param string|null $name Name of plugin for which to symlink assets.
-     *   If null all plugins will be processed.
-     * @return void
-     */
-    public function copy(?string $name = null): void
-    {
-        $this->_process($this->_list($name), true, (bool)$this->param('overwrite'));
-    }
-
-    /**
-     * 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.12
+     * @var \Cake\Console\ConsoleIo
      */
-    public function remove(?string $name = null): void
-    {
-        $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');
-    }
+    protected $io;
 
     /**
      * Get list of plugins to process. Plugins without a webroot directory are skipped.
@@ -92,7 +56,7 @@ class AssetsTask extends Shell
             $pluginsList = Plugin::loaded();
         } else {
             if (!Plugin::isLoaded($name)) {
-                $this->err(sprintf('Plugin %s is not loaded.', $name));
+                $this->io->err(sprintf('Plugin %s is not loaded.', $name));
 
                 return [];
             }
@@ -104,8 +68,8 @@ class AssetsTask extends Shell
         foreach ($pluginsList as $plugin) {
             $path = Plugin::path($plugin) . 'webroot';
             if (!is_dir($path)) {
-                $this->verbose('', 1);
-                $this->verbose(
+                $this->io->verbose('', 1);
+                $this->io->verbose(
                     sprintf('Skipping plugin %s. It does not have webroot folder.', $plugin),
                     2
                 );
@@ -144,12 +108,10 @@ class AssetsTask extends Shell
      */
     protected function _process(array $plugins, bool $copy = false, bool $overwrite = false): void
     {
-        $overwrite = (bool)$this->param('overwrite');
-
         foreach ($plugins as $plugin => $config) {
-            $this->out();
-            $this->out('For plugin: ' . $plugin);
-            $this->hr();
+            $this->io->out();
+            $this->io->out('For plugin: ' . $plugin);
+            $this->io->hr();
 
             if ($config['namespaced'] &&
                 !is_dir($config['destDir']) &&
@@ -164,7 +126,7 @@ class AssetsTask extends Shell
                 if ($overwrite && !$this->_remove($config)) {
                     continue;
                 } elseif (!$overwrite) {
-                    $this->verbose(
+                    $this->io->verbose(
                         $dest . ' already exists',
                         1
                     );
@@ -189,8 +151,8 @@ class AssetsTask extends Shell
             );
         }
 
-        $this->out();
-        $this->out('Done');
+        $this->io->out();
+        $this->io->out('Done');
     }
 
     /**
@@ -202,7 +164,7 @@ class AssetsTask extends Shell
     protected function _remove(array $config): bool
     {
         if ($config['namespaced'] && !is_dir($config['destDir'])) {
-            $this->verbose(
+            $this->io->verbose(
                 $config['destDir'] . $config['link'] . ' does not exist',
                 1
             );
@@ -213,7 +175,7 @@ class AssetsTask extends Shell
         $dest = $config['destDir'] . $config['link'];
 
         if (!file_exists($dest)) {
-            $this->verbose(
+            $this->io->verbose(
                 $dest . ' does not exist',
                 1
             );
@@ -224,11 +186,11 @@ class AssetsTask extends Shell
         if (is_link($dest)) {
             // phpcs:ignore
             if (@unlink($dest)) {
-                $this->out('Unlinked ' . $dest);
+                $this->io->out('Unlinked ' . $dest);
 
                 return true;
             } else {
-                $this->err('Failed to unlink  ' . $dest);
+                $this->io->err('Failed to unlink  ' . $dest);
 
                 return false;
             }
@@ -236,11 +198,11 @@ class AssetsTask extends Shell
 
         $fs = new Filesystem();
         if ($fs->deleteDir($dest)) {
-            $this->out('Deleted ' . $dest);
+            $this->io->out('Deleted ' . $dest);
 
             return true;
         } else {
-            $this->err('Failed to delete ' . $dest);
+            $this->io->err('Failed to delete ' . $dest);
 
             return false;
         }
@@ -261,12 +223,12 @@ class AssetsTask extends Shell
         umask($old);
 
         if ($result) {
-            $this->out('Created directory ' . $dir);
+            $this->io->out('Created directory ' . $dir);
 
             return true;
         }
 
-        $this->err('Failed creating directory ' . $dir);
+        $this->io->err('Failed creating directory ' . $dir);
 
         return false;
     }
@@ -285,7 +247,7 @@ class AssetsTask extends Shell
         // phpcs:enable
 
         if ($result) {
-            $this->out('Created symlink ' . $link);
+            $this->io->out('Created symlink ' . $link);
 
             return true;
         }
@@ -304,40 +266,13 @@ class AssetsTask extends Shell
     {
         $fs = new Filesystem();
         if ($fs->copyDir($source, $destination)) {
-            $this->out('Copied assets to directory ' . $destination);
+            $this->io->out('Copied assets to directory ' . $destination);
 
             return true;
         }
 
-        $this->err('Error copying assets to directory ' . $destination);
+        $this->io->err('Error copying assets to directory ' . $destination);
 
         return false;
     }
-
-    /**
-     * Gets the option parser instance and configures it.
-     *
-     * @return \Cake\Console\ConsoleOptionParser
-     */
-    public function getOptionParser(): ConsoleOptionParser
-    {
-        $parser = parent::getOptionParser();
-
-        $parser->addSubcommand('symlink', [
-            '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,
-        ])->addOption('overwrite', [
-            'help' => 'Overwrite existing symlink / folder / files.',
-            'default' => false,
-            'boolean' => true,
-        ]);
-
-        return $parser;
-    }
 }