1
0
Fork 0

Fix usage of the ProcessExecutor

pull/223/merge
Jordi Boggiano 2012-01-22 20:08:20 +01:00
parent 5ce9de422b
commit 702d415472
6 changed files with 38 additions and 32 deletions

View File

@ -75,7 +75,7 @@ class GitDownloader implements DownloaderInterface
private function enforceCleanDirectory($path) private function enforceCleanDirectory($path)
{ {
$this->process->execute(sprintf('cd %s && git status --porcelain', $path), $output); $this->process->execute(sprintf('cd %s && git status --porcelain', $path), $output);
if (implode('', $output)) { if (trim($output)) {
throw new \RuntimeException('Source directory has uncommitted changes'); throw new \RuntimeException('Source directory has uncommitted changes');
} }
} }

View File

@ -75,7 +75,7 @@ class HgDownloader implements DownloaderInterface
private function enforceCleanDirectory($path) private function enforceCleanDirectory($path)
{ {
$this->process->execute(sprintf('cd %s && hg st', $path), $output); $this->process->execute(sprintf('cd %s && hg st', $path), $output);
if (implode('', $output)) { if (trim($output)) {
throw new \RuntimeException('Source directory has uncommitted changes'); throw new \RuntimeException('Source directory has uncommitted changes');
} }
} }

View File

@ -48,7 +48,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
$this->rootIdentifier = 'master'; $this->rootIdentifier = 'master';
$this->process->execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output); $this->process->execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($this->process->splitLines($output) as $branch) {
if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) { if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) {
$this->rootIdentifier = $match[1]; $this->rootIdentifier = $match[1];
break; break;
@ -91,11 +91,9 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
$this->process->execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $this->process->execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
$composer = implode("\n", $output);
unset($output);
if (!$composer) { if (!trim($composer)) {
throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl()); throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
} }
@ -119,6 +117,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
{ {
if (null === $this->tags) { if (null === $this->tags) {
$this->process->execute(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output); $this->process->execute(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output);
$output = $this->process->splitLines($output);
$this->tags = $output ? array_combine($output, $output) : array(); $this->tags = $output ? array_combine($output, $output) : array();
} }
@ -134,7 +133,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
$branches = array(); $branches = array();
$this->process->execute(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output); $this->process->execute(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($this->process->splitLines($output) as $branch) {
if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
preg_match('{^ *[^/]+/(\S+) *([a-f0-9]+) .*$}', $branch, $match); preg_match('{^ *[^/]+/(\S+) *([a-f0-9]+) .*$}', $branch, $match);
$branches[$match[1]] = $match[2]; $branches[$match[1]] = $match[2];

View File

@ -58,6 +58,7 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
$tmpDir = escapeshellarg($this->tmpDir); $tmpDir = escapeshellarg($this->tmpDir);
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
$this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output); $this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
$output = $this->process->splitLines($output);
$this->rootIdentifier = $output[0]; $this->rootIdentifier = $output[0];
} }
@ -96,11 +97,9 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
$this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
$composer = implode("\n", $output);
unset($output);
if (!$composer) { if (!trim($composer)) {
throw new \UnexpectedValueException('Failed to retrieve composer information for identifier ' . $identifier . ' in ' . $this->getUrl()); throw new \UnexpectedValueException('Failed to retrieve composer information for identifier ' . $identifier . ' in ' . $this->getUrl());
} }
@ -126,10 +125,11 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
$tags = array(); $tags = array();
$this->process->execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output); $this->process->execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $tag) { foreach ($this->process->splitLines($output) as $tag) {
if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
$tags[$match[1]] = $match[2]; $tags[$match[1]] = $match[2];
} }
}
$this->tags = $tags; $this->tags = $tags;
} }
@ -146,10 +146,11 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
$branches = array(); $branches = array();
$this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output); $this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($this->process->splitLines($output) as $branch) {
if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) if ($branch && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
$branches[$match[1]] = $match[2]; $branches[$match[1]] = $match[2];
} }
}
$this->branches = $branches; $this->branches = $branches;
} }

View File

@ -80,11 +80,9 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
$rev = ''; $rev = '';
} }
$this->process->execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output); $this->process->execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)), $composer);
$composer = implode("\n", $output);
unset($output);
if (!$composer) { if (!trim($composer)) {
throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl()); throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
} }
@ -92,8 +90,8 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
$this->process->execute(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output); $this->process->execute(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output);
foreach ($output as $line) { foreach ($this->process->splitLines($output) as $line) {
if (preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) { if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
$date = new \DateTime($match[1]); $date = new \DateTime($match[1]);
$composer['time'] = $date->format('Y-m-d H:i:s'); $composer['time'] = $date->format('Y-m-d H:i:s');
break; break;
@ -114,10 +112,12 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
if (null === $this->tags) { if (null === $this->tags) {
$this->process->execute(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output); $this->process->execute(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output);
$this->tags = array(); $this->tags = array();
foreach ($output as $tag) { foreach ($this->process->splitLines($output) as $tag) {
if ($tag) {
$this->tags[rtrim($tag, '/')] = '/tags/'.$tag; $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
} }
} }
}
return $this->tags; return $this->tags;
} }
@ -131,7 +131,7 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
$this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output); $this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output);
$this->branches = array(); $this->branches = array();
foreach ($output as $line) { foreach ($this->process->splitLines($output) as $line) {
preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match); preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
if ($match[2] === 'trunk/') { if ($match[2] === 'trunk/') {
$this->branches['trunk'] = '/trunk/@'.$match[1]; $this->branches['trunk'] = '/trunk/@'.$match[1];
@ -141,7 +141,7 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
unset($output); unset($output);
$this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output); $this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output);
foreach ($output as $line) { foreach ($this->process->splitLines($output) as $line) {
preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match); preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
if ($match[2] === './') { if ($match[2] === './') {
continue; continue;

View File

@ -28,19 +28,25 @@ class ProcessExecutor
*/ */
public function execute($command, &$output = null) public function execute($command, &$output = null)
{ {
$captureOutput = count(func_get_args()) > 1;
$process = new Process($command); $process = new Process($command);
$process->run(function($type, $buffer) use ($output) { $process->run(function($type, $buffer) use ($captureOutput) {
if (null === $output) { if ($captureOutput) {
return; return;
} }
echo $buffer; echo $buffer;
}); });
if (null !== $output) { if ($captureOutput) {
$output = $process->getOutput(); $output = $process->getOutput();
} }
return $process->getExitCode(); return $process->getExitCode();
} }
public function splitLines($output)
{
return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
}
} }