1
0
Fork 0

seperate class for Process, using this one all over the place

pull/180/head
digitalkaoz 2012-01-05 11:12:54 +01:00
parent a15567cea6
commit 897ff9126d
10 changed files with 88 additions and 67 deletions

View File

@ -13,7 +13,7 @@
namespace Composer; namespace Composer;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process; use Composer\Util\Process;
/** /**
* The Compiler class compiles composer into a phar * The Compiler class compiles composer into a phar

View File

@ -13,7 +13,7 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -39,7 +39,7 @@ class GitDownloader implements DownloaderInterface
$url = escapeshellarg($package->getSourceUrl()); $url = escapeshellarg($package->getSourceUrl());
$ref = escapeshellarg($package->getSourceReference()); $ref = escapeshellarg($package->getSourceReference());
Filesystem::runProcess(sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref)); Process::execute(sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref));
} }
/** /**
@ -52,7 +52,7 @@ class GitDownloader implements DownloaderInterface
} }
$this->enforceCleanDirectory($path); $this->enforceCleanDirectory($path);
Filesystem::runProcess(sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $target->getSourceReference())); Process::execute(sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $target->getSourceReference()));
} }
/** /**
@ -67,7 +67,7 @@ class GitDownloader implements DownloaderInterface
private function enforceCleanDirectory($path) private function enforceCleanDirectory($path)
{ {
Filesystem::runProcess(sprintf('cd %s && git status --porcelain', $path),$output); Process::execute(sprintf('cd %s && git status --porcelain', $path),$output);
if (implode('', $output)) { if (implode('', $output)) {
throw new \RuntimeException('Source directory has uncommitted changes'); throw new \RuntimeException('Source directory has uncommitted changes');
} }

View File

@ -13,7 +13,7 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Per Bernhardt <plb@webfactory.de> * @author Per Bernhardt <plb@webfactory.de>
@ -39,7 +39,7 @@ class HgDownloader implements DownloaderInterface
$url = escapeshellarg($package->getSourceUrl()); $url = escapeshellarg($package->getSourceUrl());
$ref = escapeshellarg($package->getSourceReference()); $ref = escapeshellarg($package->getSourceReference());
Filesystem::runProcess(sprintf('(hg clone %s %s 2> /dev/null) && cd %2$s && hg up %s', $url, $path, $ref)); Process::execute(sprintf('(hg clone %s %s 2> /dev/null) && cd %2$s && hg up %s', $url, $path, $ref));
} }
/** /**
@ -52,7 +52,7 @@ class HgDownloader implements DownloaderInterface
} }
$this->enforceCleanDirectory($path); $this->enforceCleanDirectory($path);
Filesystem::runProcess(sprintf('cd %s && hg pull && hg up %s', $path, escapeshellarg($target->getSourceReference()))); Process::execute(sprintf('cd %s && hg pull && hg up %s', $path, escapeshellarg($target->getSourceReference())));
} }
/** /**
@ -67,7 +67,7 @@ class HgDownloader implements DownloaderInterface
private function enforceCleanDirectory($path) private function enforceCleanDirectory($path)
{ {
Filesystem::runProcess(sprintf('cd %s && hg st', $path), $output); Process::execute(sprintf('cd %s && hg st', $path), $output);
if (implode('', $output)) { if (implode('', $output)) {
throw new \RuntimeException('Source directory has uncommitted changes'); throw new \RuntimeException('Source directory has uncommitted changes');
} }

View File

@ -3,7 +3,7 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Ben Bieker <mail@ben-bieker.de> * @author Ben Bieker <mail@ben-bieker.de>
@ -29,7 +29,7 @@ class SvnDownloader implements DownloaderInterface
$url = escapeshellarg($package->getSourceUrl()); $url = escapeshellarg($package->getSourceUrl());
$ref = escapeshellarg($package->getSourceReference()); $ref = escapeshellarg($package->getSourceReference());
Filesystem::runProcess(sprintf('svn co %s/%s %s', $url, $ref, $path)); Process::execute(sprintf('svn co %s/%s %s', $url, $ref, $path));
} }
/** /**
@ -43,7 +43,7 @@ class SvnDownloader implements DownloaderInterface
$url = escapeshellarg($target->getSourceUrl()); $url = escapeshellarg($target->getSourceUrl());
$ref = escapeshellarg($target->getSourceReference()); $ref = escapeshellarg($target->getSourceReference());
Filesystem::runProcess(sprintf('cd %s && svn switch %s/%s', $path, $url, $ref)); Process::execute(sprintf('cd %s && svn switch %s/%s', $path, $url, $ref));
} }
/** /**

View File

@ -12,7 +12,7 @@
namespace Composer\Downloader\Util; namespace Composer\Downloader\Util;
use Symfony\Component\Process\Process; use Composer\Util\Process;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -22,9 +22,9 @@ class Filesystem
public function removeDirectory($directory) public function removeDirectory($directory)
{ {
if (defined('PHP_WINDOWS_VERSION_BUILD')) { if (defined('PHP_WINDOWS_VERSION_BUILD')) {
static::runProcess(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory)))); Process::execute(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory))));
} else { } else {
static::runProcess(sprintf('rm -rf %s', escapeshellarg($directory))); Process::execute(sprintf('rm -rf %s', escapeshellarg($directory)));
} }
} }
@ -127,30 +127,4 @@ class Filesystem
{ {
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
} }
}
/**
* runs a process on the commandline
*
* @static
* @param $command the command to execute
* @param null $output the output will be written into this var if passed
* @return int statuscode
*/
public static function runProcess($command, &$output = null)
{
$process = new Process($command);
$process->run(function($type, $buffer) use ($output) {
if (null === $output) {
return;
}
echo $buffer;
});
if (null !== $output) {
$output = $process->getOutput();
}
return $process->getExitCode();
}
}

View File

@ -13,7 +13,7 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Symfony\Component\Process\Process; use Composer\Util\Process;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>

View File

@ -3,7 +3,7 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -30,9 +30,9 @@ class GitDriver implements VcsDriverInterface
$url = escapeshellarg($this->url); $url = escapeshellarg($this->url);
$tmpDir = escapeshellarg($this->tmpDir); $tmpDir = escapeshellarg($this->tmpDir);
if (is_dir($this->tmpDir)) { if (is_dir($this->tmpDir)) {
Filesystem::runProcess(sprintf('cd %s && git fetch origin', $tmpDir), $output); Process::execute(sprintf('cd %s && git fetch origin', $tmpDir), $output);
} else { } else {
Filesystem::runProcess(sprintf('git clone %s %s', $url, $tmpDir), $output); Process::execute(sprintf('git clone %s %s', $url, $tmpDir), $output);
} }
$this->getTags(); $this->getTags();
@ -46,7 +46,7 @@ class GitDriver implements VcsDriverInterface
{ {
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
$this->rootIdentifier = 'master'; $this->rootIdentifier = 'master';
Filesystem::runProcess(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output); Process::execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($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];
@ -90,7 +90,7 @@ class GitDriver implements VcsDriverInterface
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
Filesystem::runProcess(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); Process::execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
$composer = implode("\n", $output); $composer = implode("\n", $output);
unset($output); unset($output);
@ -101,7 +101,7 @@ class GitDriver implements VcsDriverInterface
$composer = JsonFile::parseJson($composer); $composer = JsonFile::parseJson($composer);
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
Filesystem::runProcess(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); Process::execute(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
$date = new \DateTime('@'.$output[0]); $date = new \DateTime('@'.$output[0]);
$composer['time'] = $date->format('Y-m-d H:i:s'); $composer['time'] = $date->format('Y-m-d H:i:s');
} }
@ -117,7 +117,7 @@ class GitDriver implements VcsDriverInterface
public function getTags() public function getTags()
{ {
if (null === $this->tags) { if (null === $this->tags) {
Filesystem::runProcess(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output); Process::execute(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output);
$this->tags = $output ? array_combine($output, $output) : array(); $this->tags = $output ? array_combine($output, $output) : array();
} }
@ -132,7 +132,7 @@ class GitDriver implements VcsDriverInterface
if (null === $this->branches) { if (null === $this->branches) {
$branches = array(); $branches = array();
Filesystem::runProcess(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output); Process::execute(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($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);

View File

@ -13,7 +13,7 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Per Bernhardt <plb@webfactory.de> * @author Per Bernhardt <plb@webfactory.de>
@ -40,9 +40,9 @@ class HgDriver implements VcsDriverInterface
$url = escapeshellarg($this->url); $url = escapeshellarg($this->url);
$tmpDir = escapeshellarg($this->tmpDir); $tmpDir = escapeshellarg($this->tmpDir);
if (is_dir($this->tmpDir)) { if (is_dir($this->tmpDir)) {
Filesystem::runProcess(sprintf('cd %s && hg pull -u', $tmpDir), $output); Process::execute(sprintf('cd %s && hg pull -u', $tmpDir), $output);
} else { } else {
Filesystem::runProcess(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output); Process::execute(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output);
} }
$this->getTags(); $this->getTags();
@ -56,7 +56,7 @@ class HgDriver implements VcsDriverInterface
{ {
$tmpDir = escapeshellarg($this->tmpDir); $tmpDir = escapeshellarg($this->tmpDir);
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
Filesystem::runProcess(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output); Process::execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
$this->rootIdentifier = $output[0]; $this->rootIdentifier = $output[0];
} }
@ -95,7 +95,7 @@ class HgDriver implements VcsDriverInterface
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
Filesystem::runProcess(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); Process::execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
$composer = implode("\n", $output); $composer = implode("\n", $output);
unset($output); unset($output);
@ -106,7 +106,7 @@ class HgDriver implements VcsDriverInterface
$composer = JsonFile::parseJson($composer); $composer = JsonFile::parseJson($composer);
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
Filesystem::runProcess(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); Process::execute(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
$date = new \DateTime($output[0]); $date = new \DateTime($output[0]);
$composer['time'] = $date->format('Y-m-d H:i:s'); $composer['time'] = $date->format('Y-m-d H:i:s');
} }
@ -124,7 +124,7 @@ class HgDriver implements VcsDriverInterface
if (null === $this->tags) { if (null === $this->tags) {
$tags = array(); $tags = array();
Filesystem::runProcess(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output); Process::execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $tag) { foreach ($output as $tag) {
if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match))
$tags[$match[1]] = $match[2]; $tags[$match[1]] = $match[2];
@ -144,7 +144,7 @@ class HgDriver implements VcsDriverInterface
if (null === $this->branches) { if (null === $this->branches) {
$branches = array(); $branches = array();
Filesystem::runProcess(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output); Process::execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output);
foreach ($output as $branch) { foreach ($output as $branch) {
if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match))
$branches[$match[1]] = $match[2]; $branches[$match[1]] = $match[2];
@ -183,7 +183,7 @@ class HgDriver implements VcsDriverInterface
return false; return false;
} }
$exit = Filesystem::runProcess(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored); $exit = Process::execute(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored);
return $exit === 0; return $exit === 0;
} }
} }

View File

@ -3,7 +3,7 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Downloader\Util\Filesystem; use Composer\Util\Process;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -79,7 +79,7 @@ class SvnDriver implements VcsDriverInterface
$rev = ''; $rev = '';
} }
Filesystem::runProcess(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output); Process::execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output);
$composer = implode("\n", $output); $composer = implode("\n", $output);
unset($output); unset($output);
@ -90,7 +90,7 @@ class SvnDriver implements VcsDriverInterface
$composer = JsonFile::parseJson($composer); $composer = JsonFile::parseJson($composer);
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
Filesystem::runProcess(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output); Process::execute(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output);
foreach ($output as $line) { foreach ($output as $line) {
if (preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) { if (preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
$date = new \DateTime($match[1]); $date = new \DateTime($match[1]);
@ -111,7 +111,7 @@ class SvnDriver implements VcsDriverInterface
public function getTags() public function getTags()
{ {
if (null === $this->tags) { if (null === $this->tags) {
Filesystem::runProcess(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output); Process::execute(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output);
$this->tags = array(); $this->tags = array();
foreach ($output as $tag) { foreach ($output as $tag) {
$this->tags[rtrim($tag, '/')] = '/tags/'.$tag; $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
@ -127,7 +127,7 @@ class SvnDriver implements VcsDriverInterface
public function getBranches() public function getBranches()
{ {
if (null === $this->branches) { if (null === $this->branches) {
Filesystem::runProcess(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output); Process::execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output);
$this->branches = array(); $this->branches = array();
foreach ($output as $line) { foreach ($output as $line) {
@ -139,7 +139,7 @@ class SvnDriver implements VcsDriverInterface
} }
unset($output); unset($output);
Filesystem::runProcess(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output); Process::execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output);
foreach ($output as $line) { foreach ($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] === './') {
@ -179,7 +179,7 @@ class SvnDriver implements VcsDriverInterface
return false; return false;
} }
$exit = Filesystem::runProcess(sprintf('svn info --non-interactive %s 2>/dev/null', escapeshellarg($url)), $ignored); $exit = Process::execute(sprintf('svn info --non-interactive %s 2>/dev/null', escapeshellarg($url)), $ignored);
return $exit === 0; return $exit === 0;
} }
} }

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
use Symfony\Component\Process\Process as BaseProcess;
/**
* @author Robert Schönthal <seroscho@googlemail.com>
*/
class Process extends BaseProcess
{
/**
* runs a process on the commandline
*
* @static
* @param $command the command to execute
* @param null $output the output will be written into this var if passed
* @return int statuscode
*/
public static function execute($command, &$output = null)
{
$process = new static($command);
$process->run(function($type, $buffer) use ($output) {
if (null === $output) {
return;
}
echo $buffer;
});
if (null !== $output) {
$output = $process->getOutput();
}
return $process->getExitCode();
}
}