From dc61c1a286697277f9be4ec4499cc9c89da65c66 Mon Sep 17 00:00:00 2001 From: Peter Gasser Date: Mon, 2 Jun 2014 13:50:29 +0200 Subject: [PATCH 1/3] add RsyncTask --- src/Task/Rsync.php | 306 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 src/Task/Rsync.php diff --git a/src/Task/Rsync.php b/src/Task/Rsync.php new file mode 100644 index 000000000..9c933037f --- /dev/null +++ b/src/Task/Rsync.php @@ -0,0 +1,306 @@ +taskRsync() + * ->fromPath('src/') + * ->toHost('localhost') + * ->toUser('dev') + * ->toPath('/var/www/html/app/') + * ->recursive() + * ->excludeVcs() + * ->checksum() + * ->wholeFile() + * ->verbose() + * ->progress() + * ->humanReadable() + * ->stats() + * ->run(); + * ``` + * + * You could also clone the task and do a dry-run first: + * + * ``` php + * $rsync = $this->taskRsync() + * ->fromPath('src/') + * ->toPath('example.com:/var/www/html/app/') + * ->archive() + * ->excludeVcs() + * ->progress() + * ->stats(); + * + * $dryRun = clone $rsync; + * $dryRun->dryRun()->run(); + * if ('y' === $this->ask('Do you want to run (y/n)')) { + * $rsync->run(); + * } + * ``` + * + * @method RsyncTask fromUser(string $user) + * @method RsyncTask fromHost(string $hostname) + * @method RsyncTask toUser(string $user) + * @method RsyncTask toHost(string $hostname) + */ +class RsyncTask implements TaskInterface, CommandInterface +{ + use Executable; + use Output; + use DynamicConfig; + + protected $fromUser; + + protected $fromHost; + + protected $fromPath; + + protected $toUser; + + protected $toHost; + + protected $toPath; + + public function __construct() + { + $this->command = 'rsync'; + } + + /** + * This can either be a full rsync path spec (user@host:path) or just a path. + * In case of the former do not specify host and user. + * + * @param string $path + * @return $this + */ + public function fromPath($path) + { + $this->fromPath = $path; + + return $this; + } + + /** + * This can either be a full rsync path spec (user@host:path) or just a path. + * In case of the former do not specify host and user. + * + * @param string $path + * @return $this + */ + public function toPath($path) + { + $this->toPath = $path; + + return $this; + } + + public function progress() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function stats() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function recursive() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function verbose() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function checksum() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function archive() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function compress() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function owner() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function group() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function times() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function delete() + { + $this->option(__FUNCTION__); + + return $this; + } + + public function timeout($seconds) + { + $this->option(__FUNCTION__, $seconds); + + return $this; + } + + public function humanReadable() + { + $this->option('human-readable'); + + return $this; + } + + public function wholeFile() + { + $this->option('whole-file'); + + return $this; + } + + public function dryRun() + { + $this->option('dry-run'); + + return $this; + } + + public function itemizeChanges() + { + $this->option('itemize-changes'); + + return $this; + } + + /** + * Excludes .git/, .svn/ and .hg/ folders. + * + * @return $this + */ + public function excludeVcs() + { + $this->exclude('.git/') + ->exclude('.svn/') + ->exclude('.hg/'); + + return $this; + } + + public function exclude($pattern) + { + return $this->option('exclude', $pattern); + } + + public function excludeFrom($file) + { + if (!is_readable($file)) { + throw new TaskException($this, "Exclude file $file is not readable"); + } + + return $this->option('exclude-from', $file); + } + + public function filesFrom($file) + { + if (!is_readable($file)) { + throw new TaskException($this, "Files-from file $file is not readable"); + } + + return $this->option('files-from', $file); + } + + /** + * @return \Robo\Result + */ + public function run() + { + $command = $this->getCommand(); + $this->printTaskInfo("running {$command}"); + + return $this->executeCommand($command); + } + + /** + * Returns command that can be executed. + * This method is used to pass generated command from one task to another. + * + * @return string + */ + public function getCommand() + { + $this->option(null, $this->getPathSpec('from')) + ->option(null, $this->getPathSpec('to')); + + return $this->command . $this->arguments; + } + + protected function getPathSpec($type) + { + if ($type !== 'from' && $type !== 'to') { + throw new TaskException($this, 'Type must be "from" or "to".'); + } + foreach (['host', 'user', 'path'] as $part) { + $varName = $type . ucfirst($part); + $$part = $this->$varName; + } + $spec = isset($path) ? $path : ''; + if (!empty($host)) { + $spec = "{$host}:{$spec}"; + } + if (!empty($user)) { + $spec = "{$user}@{$spec}"; + } + + return $spec; + } +} From 971bfc530be5f0ed2e17c9507ea22b4b3173cca0 Mon Sep 17 00:00:00 2001 From: Peter Gasser Date: Mon, 2 Jun 2014 13:50:56 +0200 Subject: [PATCH 2/3] add Task\RsyncTask to Tasks.php --- src/Tasks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tasks.php b/src/Tasks.php index 55bfa5bf4..0ae6932ed 100644 --- a/src/Tasks.php +++ b/src/Tasks.php @@ -18,6 +18,7 @@ class Tasks use Task\ParallelExec; use Task\Concat; use Task\Bower; + use Task\Rsync; use Output; protected function stopOnFail() From 35c48b0112deae7bb5b4bf40162c87f757c6c5f5 Mon Sep 17 00:00:00 2001 From: Davert Date: Wed, 4 Jun 2014 21:30:48 +0300 Subject: [PATCH 3/3] global stopOnFail should be switched on and off --- src/Task/Development.php | 2 +- src/Tasks.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Task/Development.php b/src/Task/Development.php index bd9814cae..299a2979a 100644 --- a/src/Task/Development.php +++ b/src/Task/Development.php @@ -463,7 +463,7 @@ protected function documentMethodDocBlock(\ReflectionMethod $reflectedMethod) $methodDoc = call_user_func($this->processMethodDocBlock, $reflectedMethod, $methodDoc); } - return trim($methodDoc); + return $methodDoc; } } diff --git a/src/Tasks.php b/src/Tasks.php index 661ec6052..795ba686d 100644 --- a/src/Tasks.php +++ b/src/Tasks.php @@ -22,8 +22,8 @@ class Tasks use Task\Rsync; use Output; - protected function stopOnFail() + protected function stopOnFail($stopOnFail = true) { - Result::$stopOnFail = true; + Result::$stopOnFail = $stopOnFail; } }