Skip to content

Commit a9f13bf

Browse files
committed
Merge pull request consolidation#50 from anvi/add-svn-task
Add svn task
2 parents 20d24bb + 7e4a3d0 commit a9f13bf

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

src/Task/Svn.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}

src/Tasks.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Tasks
2020
use Task\Bower;
2121
use Task\SshExec;
2222
use Task\Rsync;
23+
use Task\Svn;
2324
use Output;
2425

2526
protected function stopOnFail($stopOnFail = true)

tests/cli/CliGuy.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //[STAMP] 84758275ff7f230d0340f7dd5ed6b053
1+
<?php //[STAMP] 43037772b72b402c67566a315c64e482
22

33
// This class was automatically generated by build task
44
// You should not change it manually as it will be overwritten on next build

tests/unit/CodeGuy.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //[STAMP] 3e64f1d5738401a7f7987aed147d9e5b
1+
<?php //[STAMP] d084fbb943f2dce49cedc47b885f1de4
22

33
// This class was automatically generated by build task
44
// You should not change it manually as it will be overwritten on next build

tests/unit/SvnTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use AspectMock\Test as test;
4+
5+
class SvnTest extends \Codeception\TestCase\Test
6+
{
7+
use \Robo\Task\Svn;
8+
/**
9+
* @var \AspectMock\Proxy\ClassProxy
10+
*/
11+
protected $svn;
12+
13+
protected function _before()
14+
{
15+
$this->svn = test::double('Robo\Task\SvnStackTask', [
16+
'taskExec' => new \AspectMock\Proxy\Anything(),
17+
'getOutput' => new \Symfony\Component\Console\Output\NullOutput()
18+
]);
19+
}
20+
21+
// tests
22+
public function testSvnStackRun()
23+
{
24+
$this->taskSvnStack('svn')->update()->add()->run();
25+
$this->svn->verifyInvoked('taskExec', ['svn add ']);
26+
$this->svn->verifyInvoked('taskExec', ['svn update ']);
27+
}
28+
29+
public function testSvnStackCommands()
30+
{
31+
verify(
32+
$this->taskSvnStack('svn', 'guest', 'foo')
33+
->checkout('svn://server/trunk')
34+
->update()
35+
->add()
36+
->commit('changed')
37+
->getCommand()
38+
)->equals("svn --username guest --password foo checkout svn://server/trunk && svn --username guest --password foo update && svn --username guest --password foo add && svn --username guest --password foo commit -m 'changed' ");
39+
}
40+
41+
}

0 commit comments

Comments
 (0)