php-censor/src/Model/Build/SvnBuild.php

125 lines
3.5 KiB
PHP
Raw Normal View History

2015-01-26 19:12:48 +01:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Model\Build;
2015-01-26 19:12:48 +01:00
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\Build;
use PHPCensor\Builder;
2015-01-26 19:12:48 +01:00
/**
* Remote Subversion Build Model
*
* @author Nadir Dzhilkibaev <imam.sharif@gmail.com>
2015-01-26 19:12:48 +01:00
*/
class SvnBuild extends Build
2015-01-26 19:12:48 +01:00
{
protected $svnCommand = 'svn export -q --non-interactive ';
/**
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
$url = rtrim($this->getProject()->getReference(), '/') . '/';
$branch = ltrim($this->getBranch(), '/');
// For empty default branch or default branch name like "/trunk" or "trunk" (-> "trunk")
2015-01-26 19:12:48 +01:00
if (empty($branch) || $branch == 'trunk') {
$url .= 'trunk';
// For default branch with standard default branch directory ("branches") like "/branch-1" or "branch-1"
// (-> "branches/branch-1")
} elseif (false === strpos($branch, '/')) {
2015-01-26 19:12:48 +01:00
$url .= 'branches/' . $branch;
// For default branch with non-standard branch directory like "/branch/branch-1" or "branch/branch-1"
// (-> "branch/branch-1")
} else {
$url .= $branch;
2015-01-26 19:12:48 +01:00
}
return $url;
}
/**
* @param Builder $builder
*
* @return void
*/
protected function extendSvnCommandFromConfig(Builder $builder)
{
$cmd = $this->svnCommand;
$svn = $builder->getConfig('svn');
if ($svn) {
foreach ($svn as $key => $value) {
$cmd .= " --${key} ${value} ";
2015-01-26 19:12:48 +01:00
}
}
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings && isset($buildSettings['clone_depth'])) {
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
2015-01-26 19:12:48 +01:00
}
$this->svnCommand = $cmd;
}
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$this->extendSvnCommandFromConfig($builder);
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
$success = $this->cloneBySsh($builder, $buildPath);
} else {
$success = $this->cloneByHttp($builder, $buildPath);
}
if (!$success) {
$builder->logFailure('Failed to export remote subversion repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* Use an HTTP-based svn export.
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand;
if (!empty($this->getCommitId())) {
2015-01-26 19:12:48 +01:00
$cmd .= ' -r %s %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCommitId(), $this->getCloneUrl(), $cloneTo);
} else {
$cmd .= ' %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
}
return $success;
}
/**
* Use an SSH-based svn export.
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
2017-03-19 04:16:14 +01:00
$cmd = $this->svnCommand . ' %s "%s"';
$keyFile = $this->writeSshKey($cloneTo);
$sshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
$cmd = 'export SVN_SSH="' . $sshWrapper . '" && ' . $cmd;
2015-01-26 19:12:48 +01:00
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
// Remove the key file and svn wrapper:
unlink($keyFile);
unlink($sshWrapper);
2015-01-26 19:12:48 +01:00
return $success;
}
}