Merge pull request #59 from kamermans/feature_variable_interpolation

Variable interpolation support
This commit is contained in:
Dan Cryer 2013-05-22 14:25:02 -07:00
commit c1bf4ab636
3 changed files with 64 additions and 14 deletions

View file

@ -73,6 +73,15 @@ class Builder
* @var array
*/
protected $config;
/**
* An array of key => value pairs that will be used for
* interpolation and environment variables
* @var array
* @see setInterpolationVars()
* @see getInterpolationVars()
*/
protected $interpolation_vars = array();
/**
* Set up the builder.
@ -236,7 +245,47 @@ class Builder
{
$this->log("\033[0;31m" . $message . "\033[0m");
}
/**
* Get an array key => value pairs that are used for interpolation
* @return array
*/
public function getInterpolationVars()
{
return $this->interpolation_vars;
}
/**
* Replace every occurance 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)
{
$trans_table = array();
foreach ($this->getInterpolationVars() as $key => $value) {
$trans_table['%'.$key.'%'] = $value;
}
return strtr($input, $trans_table);
}
/**
* 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(
'PHPCI' => 1,
'PHPCI_COMMIT' => $this->build->getCommitId(),
'PHPCI_PROJECT' => $this->build->getProject()->getId(),
'PHPCI_BUILD' => $this->build->getId(),
'PHPCI_PROJECT_TITLE' => $this->build->getProject()->getTitle(),
'PHPCI_BUILD_PATH' => $this->buildPath,
);
}
/**
* Set up a working copy of the project for building.
*/
@ -247,14 +296,13 @@ class Builder
$this->ciDir = realpath(dirname(__FILE__) . '/../') . '/';
$this->buildPath = $this->ciDir . 'build/' . $buildId . '/';
$this->setInterpolationVars();
// Setup environment vars that will be accessible during exec()
putenv("PHPCI=1");
putenv("PHPCI_COMMIT=".$commitId);
putenv("PHPCI_PROJECT=".$this->build->getProject()->getId());
putenv("PHPCI_BUILD=".$this->build->getId());
putenv("PHPCI_PROJECT_TITLE=".$this->build->getProject()->getTitle());
putenv("PHPCI_BUILD_PATH=".$this->buildPath);
foreach ($this->getInterpolationVars() as $key => $value) {
putenv($key.'='.$value);
}
// Create a working copy of the project:
if (!$this->build->createWorkingCopy($this, $this->buildPath)) {
return false;

View file

@ -41,7 +41,7 @@ class Env implements \PHPCI\Plugin
$env_var = "$key=$value";
}
if (!putenv($env_var)) {
if (!putenv($this->phpci->interpolate($env_var))) {
$success = false;
$this->phpci->logFailure("Unable to set environment variable");
}

View file

@ -51,8 +51,8 @@ class Mysql implements \PHPCI\Plugin
if (isset($buildSettings['mysql'])) {
$sql = $buildSettings['mysql'];
$this->host = !empty($sql['host']) ? $sql['host'] : $this->host;
$this->user = !empty($sql['user']) ? $sql['user'] : $this->user;
$this->host = !empty($sql['host']) ? $sql['host'] : $this->phpci->interpolate($this->host);
$this->user = !empty($sql['user']) ? $sql['user'] : $this->phpci->interpolate($this->user);
$this->pass = array_key_exists('pass', $sql) ? $sql['pass'] : $this->pass;
}
}
@ -71,10 +71,12 @@ class Mysql implements \PHPCI\Plugin
foreach ($this->queries as $query) {
if (!is_array($query)) {
// Simple query
$this->pdo->query($query);
$this->pdo->query($this->phpci->interpolate($query));
} else if (isset($query['import'])) {
// SQL file execution
$this->executeFile($query['import']);
} else {
throw new \Exception("Invalid command");
}
}
} catch (\Exception $ex) {
@ -88,15 +90,15 @@ class Mysql implements \PHPCI\Plugin
protected function executeFile($query)
{
if (!isset($query['file'])) {
throw new \Exception("Import statement must contiain an 'file' key");
throw new \Exception("Import statement must contain a 'file' key");
}
$import_file = $this->phpci->buildPath . $query['file'];
$import_file = $this->phpci->buildPath . $this->phpci->interpolate($query['file']);
if (!is_readable($import_file)) {
throw new \Exception("Cannot open SQL import file: $import_file");
}
$database = isset($query['database'])? $query['database']: null;
$database = isset($query['database'])? $this->phpci->interpolate($query['database']): null;
$import_command = $this->getImportCommand($import_file, $database);
if (!$this->phpci->executeCommand($import_command)) {