forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Svn Task is similar to Git Task but for Subversion repository
- Loading branch information
Showing
5 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' "); | ||
} | ||
|
||
} |