2013-05-16 00:47:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2013-05-16 03:16:56 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 01:51:12 +02:00
|
|
|
namespace PHPCI\Model\Build;
|
2013-05-16 16:40:40 +02:00
|
|
|
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
use PHPCI\Builder;
|
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
2013-05-16 00:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-16 03:30:48 +02:00
|
|
|
* Local Build Model
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Core
|
|
|
|
*/
|
2013-05-16 00:47:37 +02:00
|
|
|
class LocalBuild extends Build
|
|
|
|
{
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Create a working copy by cloning, copying, or similar.
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function createWorkingCopy(Builder $builder, $buildPath)
|
2013-10-10 02:12:30 +02:00
|
|
|
{
|
2013-05-16 16:40:40 +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.
|
2013-10-10 02:12:30 +02:00
|
|
|
if (is_file($reference.'/config') && $this->handleBareRepository($builder, $reference, $buildPath) === true) {
|
2013-11-12 22:18:09 +01:00
|
|
|
return $this->handleConfig($builder, $buildPath) !== false;
|
2013-06-30 10:55:25 +02:00
|
|
|
}
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$buildSettings = $this->handleConfig($builder, $reference);
|
|
|
|
|
|
|
|
if ($buildSettings === false) {
|
2013-05-16 16:40:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (isset($buildSettings['prefer_symlink']) && $buildSettings['prefer_symlink'] === true) {
|
|
|
|
return $this->handleSymlink($builder, $reference, $buildPath);
|
|
|
|
} else {
|
|
|
|
$builder->executeCommand('cp -Rf "%s" "%s/"', $reference, $buildPath);
|
|
|
|
}
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function handleBareRepository(Builder $builder, $reference, $buildPath)
|
|
|
|
{
|
|
|
|
$gitConfig = parse_ini_file($reference.'/config', true);
|
2013-05-16 16:40:40 +02:00
|
|
|
|
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']) {
|
2013-12-11 08:33:59 +01:00
|
|
|
$builder->executeCommand('mkdir %2$s; git --git-dir="%1$s" archive %3$s | tar -x -C "%2$s"', $reference, $buildPath, $this->getBranch());
|
2013-10-10 02:01:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
|
|
|
|
protected function handleConfig(Builder $builder, $reference)
|
|
|
|
{
|
|
|
|
/** @todo Add support for database-based yml definition */
|
|
|
|
if (!is_file($reference . '/phpci.yml')) {
|
|
|
|
$builder->logFailure('Project does not contain a phpci.yml file.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$yamlParser = new YamlParser();
|
|
|
|
$yamlFile = file_get_contents($reference . '/phpci.yml');
|
|
|
|
$builder->setConfigArray($yamlParser->parse($yamlFile));
|
|
|
|
return $builder->getConfig('build_settings');
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
}
|