|
| 1 | +<?php |
| 2 | +namespace Robo\Task; |
| 3 | + |
| 4 | +use Robo\Output; |
| 5 | +use Robo\Result; |
| 6 | +use Robo\Task\Shared\CommandInterface; |
| 7 | +use Robo\Task\Shared\TaskInterface; |
| 8 | + |
| 9 | +trait Svn { |
| 10 | + |
| 11 | + protected function taskSvnStack($pathToSvn = 'svn', $username = '', $password = '') |
| 12 | + { |
| 13 | + return new SvnStackTask($pathToSvn, $username, $password); |
| 14 | + } |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Runs Svn commands in stack. You can use `stopOnFail()` to point that stack should be terminated on first fail. |
| 20 | + * |
| 21 | + * ``` php |
| 22 | + * <?php |
| 23 | + * $this->taskSvnStack() |
| 24 | + * ->stopOnFail() |
| 25 | + * ->add() |
| 26 | + * ->commit('adding everything') |
| 27 | + * ->run() |
| 28 | + * |
| 29 | + * $this->taskSvnStack() |
| 30 | + * ->stopOnFail() |
| 31 | + * ->update() |
| 32 | + * ->add('doc/*') |
| 33 | + * ->commit('doc updated') |
| 34 | + * ->run(); |
| 35 | + * ?> |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +class SvnStackTask implements TaskInterface, CommandInterface |
| 39 | +{ |
| 40 | + use Exec; |
| 41 | + use Output; |
| 42 | + |
| 43 | + protected $svn; |
| 44 | + protected $stackCommands = []; |
| 45 | + protected $stopOnFail = false; |
| 46 | + protected $result; |
| 47 | + |
| 48 | + public function __construct($pathToSvn='svn', $username='', $password='') |
| 49 | + { |
| 50 | + $this->svn = $pathToSvn; |
| 51 | + if (! empty($username)) { |
| 52 | + $this->svn .= " --username $username"; |
| 53 | + } |
| 54 | + if (! empty($password)) { |
| 55 | + $this->svn .= " --password $password"; |
| 56 | + } |
| 57 | + $this->result = Result::success($this); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Svn commands in stack will stop if any of commands were unsuccessful |
| 62 | + * |
| 63 | + * @return $this |
| 64 | + */ |
| 65 | + public function stopOnFail() |
| 66 | + { |
| 67 | + $this->stopOnFail = true; |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Updates `svn update` command |
| 73 | + * |
| 74 | + * @return $this; |
| 75 | + */ |
| 76 | + public function update($path='') |
| 77 | + { |
| 78 | + $this->stackCommands[] = "update $path"; |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Executes `svn add` command with files to add pattern |
| 84 | + * |
| 85 | + * @param $pattern |
| 86 | + * @return $this |
| 87 | + */ |
| 88 | + public function add($pattern='') |
| 89 | + { |
| 90 | + $this->stackCommands[]= "add $pattern"; |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Executes `svn commit` command with a message |
| 96 | + * |
| 97 | + * @param $message |
| 98 | + * @param string $options |
| 99 | + * @return $this |
| 100 | + */ |
| 101 | + public function commit($message, $options = "") |
| 102 | + { |
| 103 | + $this->stackCommands[] = "commit -m '$message' $options"; |
| 104 | + return $this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Executes `svn checkout` command |
| 109 | + * |
| 110 | + * @param $branch |
| 111 | + * @return $this |
| 112 | + */ |
| 113 | + public function checkout($branch) |
| 114 | + { |
| 115 | + $this->stackCommands[] = "checkout $branch"; |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + public function getCommand() |
| 120 | + { |
| 121 | + $commands = array_map(function($c) { return $this->svn .' '. $c; }, $this->stackCommands); |
| 122 | + return implode(' && ', $commands); |
| 123 | + } |
| 124 | + |
| 125 | + public function run() |
| 126 | + { |
| 127 | + $this->printTaskInfo("Running svn commands..."); |
| 128 | + foreach ($this->stackCommands as $command) { |
| 129 | + $this->result = $this->taskExec($this->svn .' '.$command)->run(); |
| 130 | + if (!$this->result->wasSuccessful() and $this->stopOnFail) { |
| 131 | + return $this->result; |
| 132 | + } |
| 133 | + } |
| 134 | + return Result::success($this); |
| 135 | + } |
| 136 | +} |
0 commit comments