Browse Source

Add "copy" command.

ADmad 11 years ago
parent
commit
01c4d3dd9c
2 changed files with 55 additions and 9 deletions
  1. 26 9
      src/Shell/PluginAssetsShell.php
  2. 29 0
      tests/TestCase/Shell/PluginAssetsShellTest.php

+ 26 - 9
src/Shell/PluginAssetsShell.php

@@ -27,7 +27,7 @@ class PluginAssetsShell extends Shell {
 
 /**
  * Attempt to symlink plugin assets to app's webroot. If symlinking fails it
- * fallback to copying the assets. For vendor namespaced plugin, parent folder
+ * fallbacks to copying the assets. For vendor namespaced plugin, parent folder
  * for vendor name are created if required.
  *
  * @param string|string $name Name of plugin for which to symlink assets.
@@ -39,6 +39,18 @@ class PluginAssetsShell extends Shell {
 	}
 
 /**
+ * Copying plugin assets to app's webroot. For vendor namespaced plugin,
+ * parent folder for vendor name are created if required.
+ *
+ * @param string|string $name Name of plugin for which to symlink assets.
+ *   If null all plugins will be processed.
+ * @return void
+ */
+	public function copy($name = null) {
+		$this->_process($this->_list($name), true);
+	}
+
+/**
  * Get list of plugins to process. Plugins without a webroot directory are skipped.
  *
  * @param string|string $name Name of plugin for which to symlink assets.
@@ -95,9 +107,10 @@ class PluginAssetsShell extends Shell {
  * Process plugins
  *
  * @param array $plugins List of plugins to process
+ * @param bool $copy Force copy mode. Default false.
  * @return void
  */
-	protected function _process($plugins) {
+	protected function _process($plugins, $copy = false) {
 		foreach ($plugins as $plugin => $config) {
 			$path = Plugin::path($plugin) . 'webroot';
 
@@ -121,12 +134,14 @@ class PluginAssetsShell extends Shell {
 				continue;
 			}
 
-			$result = $this->_createSymlink(
-				$config['srcPath'],
-				$config['destDir'] . $config['link']
-			);
-			if ($result) {
-				continue;
+			if (!$copy) {
+				$result = $this->_createSymlink(
+					$config['srcPath'],
+					$config['destDir'] . $config['link']
+				);
+				if ($result) {
+					continue;
+				}
 			}
 
 			$this->_copyDirectory(
@@ -208,7 +223,9 @@ class PluginAssetsShell extends Shell {
 		$parser = parent::getOptionParser();
 
 		$parser->addSubcommand('symlink', [
-			'help' => 'Symlink / copy assets to app\'s webroot.'
+			'help' => 'Symlink (copy as fallback) plugin assets to app\'s webroot.'
+		])->addSubcommand('copy', [
+			'help' => 'Copy plugin assets to app\'s webroot.'
 		])->addArgument('name', [
 			'help' => 'A specific plugin you want to symlink assets for.',
 			'optional' => true,

+ 29 - 0
tests/TestCase/Shell/PluginAssetsShellTest.php

@@ -164,6 +164,7 @@ class PluginAssetsShellTest extends TestCase {
 		$path = WWW_ROOT . 'test_plugin';
 		$link = new \SplFileInfo($path);
 		$this->assertTrue(file_exists($path . DS . 'root.js'));
+		unlink($path);
 
 		$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
 		$link = new \SplFileInfo($path);
@@ -171,4 +172,32 @@ class PluginAssetsShellTest extends TestCase {
 		$this->assertFalse($link->isLink());
 	}
 
+/**
+ * testCopy
+ *
+ * @return void
+ */
+	public function testCopy() {
+		Plugin::load('TestPlugin');
+		Plugin::load('Company/TestPluginThree');
+
+		$this->shell->copy();
+
+		$path = WWW_ROOT . 'test_plugin';
+		$dir = new \SplFileInfo($path);
+		$this->assertTrue($dir->isDir());
+		$this->assertTrue(file_exists($path . DS . 'root.js'));
+
+		$folder = new Folder($path);
+		$folder->delete();
+
+		$path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
+		$link = new \SplFileInfo($path);
+		$this->assertTrue($link->isDir());
+		$this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
+
+		$folder = new Folder(WWW_ROOT . 'company');
+		$folder->delete();
+	}
+
 }