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

95 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Model\Build;
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\Build;
use PHPCensor\Builder;
/**
* Local Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class LocalBuild extends Build
{
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
2013-10-10 02:12:30 +02:00
{
$reference = $this->getProject()->getReference();
$reference = substr($reference, -1) == '/' ? substr($reference, 0, -1) : $reference;
$buildPath = substr($buildPath, 0, -1);
2013-10-10 02:01:06 +02:00
// If there's a /config file in the reference directory, it is probably a bare repository
// which we'll extract into our build path directly.
if (
is_file($reference . '/config') &&
true === $this->handleBareRepository($builder, $reference, $buildPath)
) {
return $this->handleConfig($builder, $buildPath);
2013-06-30 10:55:25 +02:00
}
$configHandled = $this->handleConfig($builder, $reference);
2013-10-10 02:01:06 +02:00
if ($configHandled === false) {
return false;
}
$buildSettings = $builder->getConfig('build_settings');
2013-10-10 02:01:06 +02:00
if (isset($buildSettings['prefer_symlink']) && $buildSettings['prefer_symlink'] === true) {
return $this->handleSymlink($builder, $reference, $buildPath);
} else {
$cmd = 'cp -Rf "%s" "%s/"';
$builder->executeCommand($cmd, $reference, $buildPath);
2013-10-10 02:01:06 +02:00
}
2013-10-10 02:01:06 +02:00
return true;
}
/**
* Check if this is a "bare" git repository, and if so, unarchive it.
* @param Builder $builder
* @param $reference
* @param $buildPath
* @return bool
*/
2013-10-10 02:01:06 +02:00
protected function handleBareRepository(Builder $builder, $reference, $buildPath)
{
$gitConfig = parse_ini_file($reference.'/config', true);
2013-10-10 02:01:06 +02:00
// If it is indeed a bare repository, then extract it into our build path:
2013-10-10 02:12:30 +02:00
if ($gitConfig['core']['bare']) {
$cmd = 'mkdir %2$s; git --git-dir="%1$s" archive %3$s | tar -x -C "%2$s"';
$builder->executeCommand($cmd, $reference, $buildPath, $this->getBranch());
2013-10-10 02:01:06 +02:00
return true;
}
2013-10-10 02:01:06 +02:00
return false;
}
/**
* Create a symlink if required.
* @param Builder $builder
* @param $reference
* @param $buildPath
* @return bool
*/
2013-10-10 02:01:06 +02:00
protected function handleSymlink(Builder $builder, $reference, $buildPath)
{
if (is_link($buildPath) && is_file($buildPath)) {
unlink($buildPath);
}
$builder->log(sprintf('Symlinking: %s to %s', $reference, $buildPath));
if (!symlink($reference, $buildPath)) {
$builder->logFailure('Failed to symlink.');
return false;
}
return true;
}
}