Merge remote-tracking branch 'origin/master' into feature/pluginfactoryconfig
Conflicts: PHPCI/Builder.php PHPCI/Plugin/Util/Factory.php
This commit is contained in:
commit
cc71045e57
79 changed files with 1857 additions and 761 deletions
|
|
@ -9,21 +9,23 @@
|
|||
|
||||
namespace PHPCI;
|
||||
|
||||
use PHPCI\Helper\BuildInterpolator;
|
||||
use PHPCI\Helper\CommandExecutor;
|
||||
use PHPCI\Helper\MailerFactory;
|
||||
use PHPCI\Logging\BuildLogger;
|
||||
use PHPCI\Model\Build;
|
||||
use b8\Store;
|
||||
use b8\Config;
|
||||
use PHPCI\Plugin\Util\Factory;
|
||||
use b8\Store\Factory;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use PHPCI\Plugin\Util\Factory as PluginFactory;
|
||||
|
||||
/**
|
||||
* PHPCI Build Runner
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class Builder implements LoggerAwareInterface, BuildLogger
|
||||
class Builder implements LoggerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
|
@ -76,12 +78,9 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
protected $lastOutput;
|
||||
|
||||
/**
|
||||
* An array of key => value pairs that will be used for
|
||||
* interpolation and environment variables
|
||||
* @var array
|
||||
* @see setInterpolationVars()
|
||||
* @var BuildInterpolator
|
||||
*/
|
||||
protected $interpolation_vars = array();
|
||||
protected $interpolator;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Store\BuildStore
|
||||
|
|
@ -103,24 +102,36 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
*/
|
||||
protected $commandExecutor;
|
||||
|
||||
/**
|
||||
* @var Logging\BuildLogger
|
||||
*/
|
||||
protected $buildLogger;
|
||||
|
||||
/**
|
||||
* Set up the builder.
|
||||
* @param \PHPCI\Model\Build $build
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(Build $build, $logger = null)
|
||||
public function __construct(Build $build, LoggerInterface $logger = null)
|
||||
{
|
||||
if ($logger) {
|
||||
$this->setLogger($logger);
|
||||
}
|
||||
$this->build = $build;
|
||||
$this->store = Store\Factory::getStore('Build');
|
||||
$this->store = Factory::getStore('Build');
|
||||
|
||||
$this->buildLogger = new BuildLogger($logger, $build);
|
||||
|
||||
$pluginFactory = $this->buildPluginFactory($build);
|
||||
$pluginFactory->addConfigFromFile(PHPCI_DIR . "/pluginconfig.php");
|
||||
$this->pluginExecutor = new Plugin\Util\Executor($pluginFactory, $this);
|
||||
|
||||
$this->commandExecutor = new CommandExecutor($this, PHPCI_DIR, $this->quiet, $this->verbose);
|
||||
$this->commandExecutor = new CommandExecutor(
|
||||
$this->buildLogger,
|
||||
PHPCI_DIR,
|
||||
$this->quiet,
|
||||
$this->verbose
|
||||
);
|
||||
|
||||
$this->interpolator = new BuildInterpolator();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -189,8 +200,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
// stages.
|
||||
if ($this->success) {
|
||||
$this->build->setStatus(Build::STATUS_SUCCESS);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$this->build->setStatus(Build::STATUS_FAILED);
|
||||
}
|
||||
|
||||
|
|
@ -199,15 +209,14 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
|
||||
if ($this->success) {
|
||||
$this->pluginExecutor->executePlugins($this->config, 'success');
|
||||
$this->logSuccess('BUILD SUCCESSFUL!');
|
||||
}
|
||||
else {
|
||||
$this->buildLogger->logSuccess('BUILD SUCCESSFUL!');
|
||||
} else {
|
||||
$this->pluginExecutor->executePlugins($this->config, 'failure');
|
||||
$this->logFailure("BUILD FAILURE");
|
||||
$this->buildLogger->logFailure("BUILD FAILURE");
|
||||
}
|
||||
|
||||
// Clean up:
|
||||
$this->log('Removing build.');
|
||||
$this->buildLogger->log('Removing build.');
|
||||
shell_exec(sprintf('rm -Rf "%s"', $this->buildPath));
|
||||
|
||||
// Update the build in the database, ping any external services, etc.
|
||||
|
|
@ -243,107 +252,14 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
}
|
||||
|
||||
/**
|
||||
* Add an entry to the build log.
|
||||
* @param string|string[] $message
|
||||
* @param string $level
|
||||
* @param mixed[] $context
|
||||
*/
|
||||
public function log($message, $level = LogLevel::INFO, $context = array())
|
||||
{
|
||||
// Skip if no logger has been loaded.
|
||||
if (!$this->logger) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($message)) {
|
||||
$message = array($message);
|
||||
}
|
||||
|
||||
// The build is added to the context so the logger can use
|
||||
// details from it if required.
|
||||
$context['build'] = $this->build;
|
||||
|
||||
foreach ($message as $item) {
|
||||
$this->logger->log($level, $item, $context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a success-coloured message to the log.
|
||||
* @param string
|
||||
*/
|
||||
public function logSuccess($message)
|
||||
{
|
||||
$this->log("\033[0;32m" . $message . "\033[0m");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a failure-coloured message to the log.
|
||||
* @param string $message
|
||||
* @param \Exception $exception The exception that caused the error.
|
||||
*/
|
||||
public function logFailure($message, \Exception $exception = null)
|
||||
{
|
||||
$context = array();
|
||||
|
||||
// The psr3 log interface stipulates that exceptions should be passed
|
||||
// as the exception key in the context array.
|
||||
if ($exception) {
|
||||
$context['exception'] = $exception;
|
||||
}
|
||||
|
||||
$this->log(
|
||||
"\033[0;31m" . $message . "\033[0m",
|
||||
LogLevel::ERROR,
|
||||
$context
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace every occurance of the interpolation vars in the given string
|
||||
* Replace every occurrence of the interpolation vars in the given string
|
||||
* Example: "This is build %PHPCI_BUILD%" => "This is build 182"
|
||||
* @param string $input
|
||||
* @return string
|
||||
*/
|
||||
public function interpolate($input)
|
||||
{
|
||||
$keys = array_keys($this->interpolation_vars);
|
||||
$values = array_values($this->interpolation_vars);
|
||||
return str_replace($keys, $values, $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the variables that will be used for interpolation. This must be run
|
||||
* from setupBuild() because prior to that, we don't know the buildPath
|
||||
*/
|
||||
protected function setInterpolationVars()
|
||||
{
|
||||
$this->interpolation_vars = array();
|
||||
$this->interpolation_vars['%PHPCI%'] = 1;
|
||||
$this->interpolation_vars['%COMMIT%'] = $this->build->getCommitId();
|
||||
$this->interpolation_vars['%PROJECT%'] = $this->build->getProjectId();
|
||||
$this->interpolation_vars['%BUILD%'] = $this->build->getId();
|
||||
$this->interpolation_vars['%PROJECT_TITLE%'] = $this->getBuildProjectTitle(
|
||||
);
|
||||
$this->interpolation_vars['%BUILD_PATH%'] = $this->buildPath;
|
||||
$this->interpolation_vars['%BUILD_URI%'] = PHPCI_URL . "build/view/" . $this->build->getId(
|
||||
);
|
||||
$this->interpolation_vars['%PHPCI_COMMIT%'] = $this->interpolation_vars['%COMMIT%'];
|
||||
$this->interpolation_vars['%PHPCI_PROJECT%'] = $this->interpolation_vars['%PROJECT%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD%'] = $this->interpolation_vars['%BUILD%'];
|
||||
$this->interpolation_vars['%PHPCI_PROJECT_TITLE%'] = $this->interpolation_vars['%PROJECT_TITLE%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD_PATH%'] = $this->interpolation_vars['%BUILD_PATH%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD_URI%'] = $this->interpolation_vars['%BUILD_URI%'];
|
||||
|
||||
putenv('PHPCI=1');
|
||||
putenv('PHPCI_COMMIT=' . $this->interpolation_vars['%COMMIT%']);
|
||||
putenv('PHPCI_PROJECT=' . $this->interpolation_vars['%PROJECT%']);
|
||||
putenv('PHPCI_BUILD=' . $this->interpolation_vars['%BUILD%']);
|
||||
putenv(
|
||||
'PHPCI_PROJECT_TITLE=' . $this->interpolation_vars['%PROJECT_TITLE%']
|
||||
);
|
||||
putenv('PHPCI_BUILD_PATH=' . $this->interpolation_vars['%BUILD_PATH%']);
|
||||
putenv('PHPCI_BUILD_URI=' . $this->interpolation_vars['%BUILD_URI%']);
|
||||
return $this->interpolator->interpolate($input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -351,12 +267,17 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
*/
|
||||
protected function setupBuild()
|
||||
{
|
||||
$buildId = 'project' . $this->build->getProject()->getId(
|
||||
) . '-build' . $this->build->getId();
|
||||
$this->ciDir = dirname(__FILE__) . '/../';
|
||||
$buildId = 'project' . $this->build->getProject()->getId()
|
||||
. '-build' . $this->build->getId();
|
||||
$this->ciDir = dirname(dirname(__FILE__) . '/../') . '/';
|
||||
$this->buildPath = $this->ciDir . 'build/' . $buildId . '/';
|
||||
$this->build->currentBuildPath = $this->buildPath;
|
||||
|
||||
$this->setInterpolationVars();
|
||||
$this->interpolator->setupInterpolationVars(
|
||||
$this->build,
|
||||
$this->buildPath,
|
||||
PHPCI_URL
|
||||
);
|
||||
|
||||
// Create a working copy of the project:
|
||||
if (!$this->build->createWorkingCopy($this, $this->buildPath)) {
|
||||
|
|
@ -373,7 +294,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
$this->ignore = $this->config['build_settings']['ignore'];
|
||||
}
|
||||
|
||||
$this->logSuccess('Working copy created: ' . $this->buildPath);
|
||||
$this->buildLogger->logSuccess('Working copy created: ' . $this->buildPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -385,32 +306,45 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
*/
|
||||
public function setLogger(LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->buildLogger->setLogger($logger);
|
||||
}
|
||||
|
||||
public function log($message, $level = LogLevel::INFO, $context = array())
|
||||
{
|
||||
$this->buildLogger->log($message, $level, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a success-coloured message to the log.
|
||||
* @param string
|
||||
*/
|
||||
public function logSuccess($message)
|
||||
{
|
||||
$this->buildLogger->logSuccess($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the logger attached to this builder.
|
||||
*
|
||||
* @return LoggerInterface
|
||||
* Add a failure-coloured message to the log.
|
||||
* @param string $message
|
||||
* @param \Exception $exception The exception that caused the error.
|
||||
*/
|
||||
public function getLogger()
|
||||
public function logFailure($message, \Exception $exception = null)
|
||||
{
|
||||
return $this->logger;
|
||||
$this->buildLogger->logFailure($message, $exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a configured instance of the plugin factory.
|
||||
*
|
||||
* @param Build $build
|
||||
* @return Factory
|
||||
* @return PluginFactory
|
||||
*/
|
||||
private function buildPluginFactory(Build $build)
|
||||
{
|
||||
$pluginFactory = new Factory();
|
||||
$pluginFactory = new PluginFactory();
|
||||
|
||||
$self = $this;
|
||||
$pluginFactory->registerResource(
|
||||
function () use($self) {
|
||||
function () use ($self) {
|
||||
return $self;
|
||||
},
|
||||
null,
|
||||
|
|
@ -418,7 +352,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
|
|||
);
|
||||
|
||||
$pluginFactory->registerResource(
|
||||
function () use($build) {
|
||||
function () use ($build) {
|
||||
return $build;
|
||||
},
|
||||
null,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue