2 namespace Robo\Task\Remote;
4 use Robo\Contract\CommandInterface;
5 use Robo\Task\BaseTask;
6 use Robo\Exception\TaskException;
9 * Executes rsync in a flexible manner.
14 * ->toHost('localhost')
16 * ->toPath('/var/www/html/app/')
17 * ->remoteShell('ssh -i public_key')
29 * You could also clone the task and do a dry-run first:
32 * $rsync = $this->taskRsync()
34 * ->toPath('example.com:/var/www/html/app/')
40 * $dryRun = clone $rsync;
41 * $dryRun->dryRun()->run();
42 * if ('y' === $this->ask('Do you want to run (y/n)')) {
47 class Rsync extends BaseTask implements CommandInterface
49 use \Robo\Common\ExecOneCommand;
89 public static function init()
94 public function __construct()
96 $this->command = 'rsync';
100 * This can either be a full rsync path spec (user@host:path) or just a path.
101 * In case of the former do not specify host and user.
103 * @param string|array $path
107 public function fromPath($path)
109 $this->fromPath = $path;
115 * This can either be a full rsync path spec (user@host:path) or just a path.
116 * In case of the former do not specify host and user.
118 * @param string $path
122 public function toPath($path)
124 $this->toPath = $path;
130 * @param string $fromUser
134 public function fromUser($fromUser)
136 $this->fromUser = $fromUser;
141 * @param string $fromHost
145 public function fromHost($fromHost)
147 $this->fromHost = $fromHost;
152 * @param string $toUser
156 public function toUser($toUser)
158 $this->toUser = $toUser;
163 * @param string $toHost
167 public function toHost($toHost)
169 $this->toHost = $toHost;
176 public function progress()
178 $this->option(__FUNCTION__);
186 public function stats()
188 $this->option(__FUNCTION__);
196 public function recursive()
198 $this->option(__FUNCTION__);
206 public function verbose()
208 $this->option(__FUNCTION__);
216 public function checksum()
218 $this->option(__FUNCTION__);
226 public function archive()
228 $this->option(__FUNCTION__);
236 public function compress()
238 $this->option(__FUNCTION__);
246 public function owner()
248 $this->option(__FUNCTION__);
256 public function group()
258 $this->option(__FUNCTION__);
266 public function times()
268 $this->option(__FUNCTION__);
276 public function delete()
278 $this->option(__FUNCTION__);
284 * @param int $seconds
288 public function timeout($seconds)
290 $this->option(__FUNCTION__, $seconds);
298 public function humanReadable()
300 $this->option('human-readable');
308 public function wholeFile()
310 $this->option('whole-file');
318 public function dryRun()
320 $this->option('dry-run');
328 public function itemizeChanges()
330 $this->option('itemize-changes');
336 * Excludes .git, .svn and .hg items at any depth.
340 public function excludeVcs()
342 return $this->exclude([
350 * @param array|string $pattern
354 public function exclude($pattern)
356 return $this->optionList(__FUNCTION__, $pattern);
360 * @param string $file
364 * @throws \Robo\Exception\TaskException
366 public function excludeFrom($file)
368 if (!is_readable($file)) {
369 throw new TaskException($this, "Exclude file $file is not readable");
372 return $this->option('exclude-from', $file);
376 * @param array|string $pattern
380 public function includeFilter($pattern)
382 return $this->optionList('include', $pattern);
386 * @param array|string $pattern
390 public function filter($pattern)
392 return $this->optionList(__FUNCTION__, $pattern);
396 * @param string $file
400 * @throws \Robo\Exception\TaskException
402 public function filesFrom($file)
404 if (!is_readable($file)) {
405 throw new TaskException($this, "Files-from file $file is not readable");
408 return $this->option('files-from', $file);
412 * @param string $command
416 public function remoteShell($command)
418 $this->option('rsh', "$command");
426 public function run()
428 $command = $this->getCommand();
430 return $this->executeCommand($command);
434 * Returns command that can be executed.
435 * This method is used to pass generated command from one task to another.
439 public function getCommand()
441 foreach ((array)$this->fromPath as $from) {
442 $this->option(null, $this->getFromPathSpec($from));
444 $this->option(null, $this->getToPathSpec());
446 return $this->command . $this->arguments;
452 protected function getFromPathSpec($from)
454 return $this->getPathSpec($this->fromHost, $this->fromUser, $from);
460 protected function getToPathSpec()
462 return $this->getPathSpec($this->toHost, $this->toUser, $this->toPath);
466 * @param string $host
467 * @param string $user
468 * @param string $path
472 protected function getPathSpec($host, $user, $path)
474 $spec = isset($path) ? $path : '';
476 $spec = "{$host}:{$spec}";
479 $spec = "{$user}@{$spec}";