Skip to content

Commit

Permalink
Added Svn Task
Browse files Browse the repository at this point in the history
Svn Task is similar to Git Task but for Subversion repository
  • Loading branch information
anvi committed Aug 5, 2014
1 parent 12518fe commit ce290e7
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 2 deletions.
136 changes: 136 additions & 0 deletions src/Task/Svn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
namespace Robo\Task;

use Robo\Output;
use Robo\Result;
use Robo\Task\Shared\CommandInterface;
use Robo\Task\Shared\TaskInterface;

trait Svn {

protected function taskSvnStack($pathToSvn = 'svn', $username = '', $password = '')
{
return new SvnStackTask($pathToSvn, $username, $password);
}

}

/**
* Runs Svn commands in stack. You can use `stopOnFail()` to point that stack should be terminated on first fail.
*
* ``` php
* <?php
* $this->taskSvnStack()
* ->stopOnFail()
* ->add()
* ->commit('adding everything')
* ->run()
*
* $this->taskSvnStack()
* ->stopOnFail()
* ->update()
* ->add('doc/*')
* ->commit('doc updated')
* ->run();
* ?>
* ```
*/
class SvnStackTask implements TaskInterface, CommandInterface
{
use Exec;
use Output;

protected $svn;
protected $stackCommands = [];
protected $stopOnFail = false;
protected $result;

public function __construct($pathToSvn='svn', $username='', $password='')
{
$this->svn = $pathToSvn;
if (! empty($username)) {
$this->svn .= " --username $username";
}
if (! empty($password)) {
$this->svn .= " --password $password";
}
$this->result = Result::success($this);
}

/**
* Svn commands in stack will stop if any of commands were unsuccessful
*
* @return $this
*/
public function stopOnFail()
{
$this->stopOnFail = true;
return $this;
}

/**
* Updates `svn update` command
*
* @return $this;
*/
public function update($path='')
{
$this->stackCommands[] = "update $path";
return $this;
}

/**
* Executes `svn add` command with files to add pattern
*
* @param $pattern
* @return $this
*/
public function add($pattern='')
{
$this->stackCommands[]= "add $pattern";
return $this;
}

/**
* Executes `svn commit` command with a message
*
* @param $message
* @param string $options
* @return $this
*/
public function commit($message, $options = "")
{
$this->stackCommands[] = "commit -m '$message' $options";
return $this;
}

/**
* Executes `svn checkout` command
*
* @param $branch
* @return $this
*/
public function checkout($branch)
{
$this->stackCommands[] = "checkout $branch";
return $this;
}

public function getCommand()
{
$commands = array_map(function($c) { return $this->svn .' '. $c; }, $this->stackCommands);
return implode(' && ', $commands);
}

public function run()
{
$this->printTaskInfo("Running svn commands...");
foreach ($this->stackCommands as $command) {
$this->result = $this->taskExec($this->svn .' '.$command)->run();
if (!$this->result->wasSuccessful() and $this->stopOnFail) {
return $this->result;
}
}
return Result::success($this);
}
}
1 change: 1 addition & 0 deletions src/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Tasks
use Task\Bower;
use Task\SshExec;
use Task\Rsync;
use Task\Svn;
use Output;

protected function stopOnFail($stopOnFail = true)
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/CliGuy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] 84758275ff7f230d0340f7dd5ed6b053
<?php //[STAMP] 43037772b72b402c67566a315c64e482

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/CodeGuy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] 3e64f1d5738401a7f7987aed147d9e5b
<?php //[STAMP] d084fbb943f2dce49cedc47b885f1de4

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/SvnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use AspectMock\Test as test;

class SvnTest extends \Codeception\TestCase\Test
{
use \Robo\Task\Svn;
/**
* @var \AspectMock\Proxy\ClassProxy
*/
protected $svn;

protected function _before()
{
$this->svn = test::double('Robo\Task\SvnStackTask', [
'taskExec' => new \AspectMock\Proxy\Anything(),
'getOutput' => new \Symfony\Component\Console\Output\NullOutput()
]);
}

// tests
public function testSvnStackRun()
{
$this->taskSvnStack('svn')->update()->add()->run();
$this->svn->verifyInvoked('taskExec', ['svn add ']);
$this->svn->verifyInvoked('taskExec', ['svn update ']);
}

public function testSvnStackCommands()
{
verify(
$this->taskSvnStack('svn', 'guest', 'foo')
->checkout('svn://server/trunk')
->update()
->add()
->commit('changed')
->getCommand()
)->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' ");
}

}

0 comments on commit ce290e7

Please sign in to comment.