1
0
Fork 0

Updates to path repository and path downloader, refs #4365

pull/3960/merge
Jordi Boggiano 2015-09-12 22:28:03 +01:00
parent 3a79313b63
commit 89c6a68a76
7 changed files with 75 additions and 85 deletions

View File

@ -30,21 +30,19 @@ class PathDownloader extends FileDownloader
public function download(PackageInterface $package, $path) public function download(PackageInterface $package, $path)
{ {
$fileSystem = new Filesystem(); $fileSystem = new Filesystem();
if ($fileSystem->exists($path)) { $this->filesystem->removeDirectory($path);
$fileSystem->remove($path);
} $this->io->writeError(sprintf(
' - Installing <info>%s</info> (<comment>%s</comment>) from %s',
$package->getName(),
$package->getFullPrettyVersion(),
$package->getDistUrl()
));
try { try {
$fileSystem->symlink($package->getDistUrl(), $path); $fileSystem->symlink($package->getDistUrl(), $path);
} catch (IOException $e) { } catch (IOException $e) {
$fileSystem->mirror($package->getDistUrl(), $path); $fileSystem->mirror($package->getDistUrl(), $path);
} }
$this->io->writeError(sprintf(
' Downloaded <info>%s</info> (<comment>%s</comment>) from %s',
$package->getName(),
$package->getFullPrettyVersion(),
$package->getDistUrl()
));
} }
} }

View File

@ -265,7 +265,7 @@ class Factory
// load package // load package
$parser = new VersionParser; $parser = new VersionParser;
$guesser = new VersionGuesser(new ProcessExecutor($io), $parser); $guesser = new VersionGuesser($config, new ProcessExecutor($io), $parser);
$loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser); $loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser);
$package = $loader->load($localConfig); $package = $loader->load($localConfig);
$composer->setPackage($package); $composer->setPackage($package);

View File

@ -51,7 +51,7 @@ class RootPackageLoader extends ArrayLoader
$this->manager = $manager; $this->manager = $manager;
$this->config = $config; $this->config = $config;
$this->versionGuesser = $versionGuesser ?: new VersionGuesser(new ProcessExecutor(), $this->versionParser); $this->versionGuesser = $versionGuesser ?: new VersionGuesser($config, new ProcessExecutor(), $this->versionParser);
} }
public function load(array $config, $class = 'Composer\Package\RootPackage') public function load(array $config, $class = 'Composer\Package\RootPackage')
@ -65,7 +65,7 @@ class RootPackageLoader extends ArrayLoader
if (getenv('COMPOSER_ROOT_VERSION')) { if (getenv('COMPOSER_ROOT_VERSION')) {
$version = getenv('COMPOSER_ROOT_VERSION'); $version = getenv('COMPOSER_ROOT_VERSION');
} else { } else {
$version = $this->versionGuesser->guessVersion($this->config, $config); $version = $this->versionGuesser->guessVersion($config, getcwd());
} }
if (!$version) { if (!$version) {

View File

@ -27,6 +27,11 @@ use Composer\Util\Svn as SvnUtil;
*/ */
class VersionGuesser class VersionGuesser
{ {
/**
* @var Config
*/
private $config;
/** /**
* @var ProcessExecutor * @var ProcessExecutor
*/ */
@ -38,45 +43,44 @@ class VersionGuesser
private $versionParser; private $versionParser;
/** /**
* @var null|string * @param Config $config
*/
private $cwd;
/**
* @param ProcessExecutor $process * @param ProcessExecutor $process
* @param VersionParser $versionParser * @param VersionParser $versionParser
* @param string $cwd
*/ */
public function __construct(ProcessExecutor $process, VersionParser $versionParser, $cwd = null) public function __construct(Config $config, ProcessExecutor $process, VersionParser $versionParser)
{ {
$this->config = $config;
$this->process = $process; $this->process = $process;
$this->versionParser = $versionParser; $this->versionParser = $versionParser;
$this->cwd = $cwd ?: getcwd();
} }
public function guessVersion(Config $config, array $packageConfig) /**
* @param array $packageConfig
* @param string $path Path to guess into
*/
public function guessVersion(array $packageConfig, $path)
{ {
if (function_exists('proc_open')) { if (function_exists('proc_open')) {
$version = $this->guessGitVersion($packageConfig); $version = $this->guessGitVersion($packageConfig, $path);
if (null !== $version) { if (null !== $version) {
return $version; return $version;
} }
$version = $this->guessHgVersion($config, $packageConfig); $version = $this->guessHgVersion($packageConfig, $path);
if (null !== $version) { if (null !== $version) {
return $version; return $version;
} }
return $this->guessSvnVersion($packageConfig); return $this->guessSvnVersion($packageConfig, $path);
} }
} }
private function guessGitVersion(array $config) private function guessGitVersion(array $packageConfig, $path)
{ {
GitUtil::cleanEnv(); GitUtil::cleanEnv();
// try to fetch current version from git tags // try to fetch current version from git tags
if (0 === $this->process->execute('git describe --exact-match --tags', $output, $this->cwd)) { if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) {
try { try {
return $this->versionParser->normalize(trim($output)); return $this->versionParser->normalize(trim($output));
} catch (\Exception $e) { } catch (\Exception $e) {
@ -84,7 +88,7 @@ class VersionGuesser
} }
// try to fetch current version from git branch // try to fetch current version from git branch
if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->cwd)) { if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) {
$branches = array(); $branches = array();
$isFeatureBranch = false; $isFeatureBranch = false;
$version = null; $version = null;
@ -116,16 +120,16 @@ class VersionGuesser
} }
// try to find the best (nearest) version branch to assume this feature's version // try to find the best (nearest) version branch to assume this feature's version
$version = $this->guessFeatureVersion($config, $version, $branches, 'git rev-list %candidate%..%branch%'); $version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
return $version; return $version;
} }
} }
private function guessHgVersion(Config $config, array $packageConfig) private function guessHgVersion(array $packageConfig, $path)
{ {
// try to fetch current version from hg branch // try to fetch current version from hg branch
if (0 === $this->process->execute('hg branch', $output, $this->cwd)) { if (0 === $this->process->execute('hg branch', $output, $path)) {
$branch = trim($output); $branch = trim($output);
$version = $this->versionParser->normalizeBranch($branch); $version = $this->versionParser->normalizeBranch($branch);
$isFeatureBranch = 0 === strpos($version, 'dev-'); $isFeatureBranch = 0 === strpos($version, 'dev-');
@ -139,30 +143,29 @@ class VersionGuesser
} }
// re-use the HgDriver to fetch branches (this properly includes bookmarks) // re-use the HgDriver to fetch branches (this properly includes bookmarks)
$packageConfig = array('url' => $this->cwd); $driver = new HgDriver(array('url' => $path), new NullIO(), $this->config, $this->process);
$driver = new HgDriver($packageConfig, new NullIO(), $config, $this->process);
$branches = array_keys($driver->getBranches()); $branches = array_keys($driver->getBranches());
// try to find the best (nearest) version branch to assume this feature's version // try to find the best (nearest) version branch to assume this feature's version
$version = $this->guessFeatureVersion($config, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"'); $version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
return $version; return $version;
} }
} }
private function guessFeatureVersion(array $config, $version, array $branches, $scmCmdline) private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path)
{ {
// ignore feature branches if they have no branch-alias or self.version is used // ignore feature branches if they have no branch-alias or self.version is used
// and find the branch they came from to use as a version instead // and find the branch they came from to use as a version instead
if ((isset($config['extra']['branch-alias']) && !isset($config['extra']['branch-alias'][$version])) if ((isset($packageConfig['extra']['branch-alias']) && !isset($packageConfig['extra']['branch-alias'][$version]))
|| strpos(json_encode($config), '"self.version"') || strpos(json_encode($packageConfig), '"self.version"')
) { ) {
$branch = preg_replace('{^dev-}', '', $version); $branch = preg_replace('{^dev-}', '', $version);
$length = PHP_INT_MAX; $length = PHP_INT_MAX;
$nonFeatureBranches = ''; $nonFeatureBranches = '';
if (!empty($config['non-feature-branches'])) { if (!empty($packageConfig['non-feature-branches'])) {
$nonFeatureBranches = implode('|', $config['non-feature-branches']); $nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']);
} }
foreach ($branches as $candidate) { foreach ($branches as $candidate) {
@ -177,7 +180,7 @@ class VersionGuesser
} }
$cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline); $cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline);
if (0 !== $this->process->execute($cmdLine, $output, $this->cwd)) { if (0 !== $this->process->execute($cmdLine, $output, $path)) {
continue; continue;
} }
@ -194,15 +197,15 @@ class VersionGuesser
return $version; return $version;
} }
private function guessSvnVersion(array $config) private function guessSvnVersion(array $packageConfig, $path)
{ {
SvnUtil::cleanEnv(); SvnUtil::cleanEnv();
// try to fetch current version from svn // try to fetch current version from svn
if (0 === $this->process->execute('svn info --xml', $output, $this->cwd)) { if (0 === $this->process->execute('svn info --xml', $output, $path)) {
$trunkPath = isset($config['trunk-path']) ? preg_quote($config['trunk-path'], '#') : 'trunk'; $trunkPath = isset($packageConfig['trunk-path']) ? preg_quote($packageConfig['trunk-path'], '#') : 'trunk';
$branchesPath = isset($config['branches-path']) ? preg_quote($config['branches-path'], '#') : 'branches'; $branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
$tagsPath = isset($config['tags-path']) ? preg_quote($config['tags-path'], '#') : 'tags'; $tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
$urlPattern = '#<url>.*/('.$trunkPath.'|('.$branchesPath.'|'. $tagsPath .')/(.*))</url>#'; $urlPattern = '#<url>.*/('.$trunkPath.'|('.$branchesPath.'|'. $tagsPath .')/(.*))</url>#';

View File

@ -20,7 +20,6 @@ use Composer\Package\Loader\LoaderInterface;
use Composer\Package\Version\VersionGuesser; use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Symfony\Component\Filesystem\Filesystem;
/** /**
* This repository allows installing local packages that are not necessarily under their own VCS. * This repository allows installing local packages that are not necessarily under their own VCS.
@ -48,16 +47,6 @@ use Symfony\Component\Filesystem\Filesystem;
*/ */
class PathRepository extends ArrayRepository class PathRepository extends ArrayRepository
{ {
/**
* @var Config
*/
private $config;
/**
* @var Filesystem
*/
private $fileSystem;
/** /**
* @var ArrayLoader * @var ArrayLoader
*/ */
@ -68,52 +57,48 @@ class PathRepository extends ArrayRepository
*/ */
private $versionGuesser; private $versionGuesser;
/**
* @var array
*/
private $packageConfig;
/** /**
* @var string * @var string
*/ */
private $path; private $path;
/**
* @var ProcessExecutor
*/
private $process;
/** /**
* Initializes path repository. * Initializes path repository.
* *
* @param array $packageConfig * @param array $repoConfig
* @param IOInterface $io * @param IOInterface $io
* @param Config $config * @param Config $config
* @param LoaderInterface $loader
* @param Filesystem $filesystem
* @param VersionGuesser $versionGuesser
*/ */
public function __construct(array $packageConfig, IOInterface $io, Config $config, LoaderInterface $loader = null, Filesystem $filesystem = null, VersionGuesser $versionGuesser = null) public function __construct(array $repoConfig, IOInterface $io, Config $config)
{ {
if (!isset($packageConfig['url'])) { if (!isset($repoConfig['url'])) {
throw new \RuntimeException('You must specify the `url` configuration for the path repository'); throw new \RuntimeException('You must specify the `url` configuration for the path repository');
} }
$this->fileSystem = $filesystem ?: new Filesystem(); $this->loader = new ArrayLoader();
$this->loader = $loader ?: new ArrayLoader(); $this->path = realpath(rtrim($repoConfig['url'], '/')) . '/';
$this->config = $config; $this->process = new ProcessExecutor($io);
$this->packageConfig = $packageConfig; $this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
$this->path = realpath(rtrim($packageConfig['url'], '/')) . '/';
$this->versionGuesser = $versionGuesser ?: new VersionGuesser(new ProcessExecutor($io), new VersionParser(), $this->path); parent::__construct();
} }
/** /**
* Initializes path repository. * Initializes path repository.
* *
* This method will basically read the folder and add the found package. * This method will basically read the folder and add the found package.
*
*/ */
protected function initialize() protected function initialize()
{ {
parent::initialize(); parent::initialize();
$composerFilePath = $this->path.'composer.json'; $composerFilePath = $this->path.'composer.json';
if (!$this->fileSystem->exists($composerFilePath)) { if (!file_exists($composerFilePath)) {
throw new \RuntimeException(sprintf('No `composer.json` file found in path repository "%s"', $this->path)); throw new \RuntimeException(sprintf('No `composer.json` file found in path repository "%s"', $this->path));
} }
@ -122,10 +107,14 @@ class PathRepository extends ArrayRepository
$package['dist'] = array( $package['dist'] = array(
'type' => 'path', 'type' => 'path',
'url' => $this->path, 'url' => $this->path,
'reference' => '',
); );
if (!isset($package['version'])) { if (!isset($package['version'])) {
$package['version'] = $this->versionGuesser->guessVersion($this->config, $this->packageConfig) ?: 'dev-master'; $package['version'] = $this->versionGuesser->guessVersion($package, $this->path) ?: 'dev-master';
}
if (is_dir($this->path.'/.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $this->path)) {
$package['dist']['reference'] = trim($output);
} }
$package = $this->loader->load($package); $package = $this->loader->load($package);

View File

@ -76,7 +76,7 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
$package = $loader->load(array()); $package = $loader->load(array());
$this->assertEquals("1.0.0.0", $package->getVersion()); $this->assertEquals("1.0.0.0", $package->getVersion());
@ -139,7 +139,7 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
$package = $loader->load(array('require' => array('foo/bar' => 'self.version'))); $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
$this->assertEquals("dev-master", $package->getPrettyVersion()); $this->assertEquals("dev-master", $package->getPrettyVersion());
@ -188,7 +188,7 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
$package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*"))); $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
$this->assertEquals("dev-latest-production", $package->getPrettyVersion()); $this->assertEquals("dev-latest-production", $package->getPrettyVersion());

View File

@ -58,8 +58,8 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($executor, new VersionParser()); $guesser = new VersionGuesser($config, $executor, new VersionParser());
$version = $guesser->guessVersion($config, array()); $version = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-$commitHash", $version); $this->assertEquals("dev-$commitHash", $version);
} }
@ -88,8 +88,8 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($executor, new VersionParser()); $guesser = new VersionGuesser($config, $executor, new VersionParser());
$version = $guesser->guessVersion($config, array()); $version = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("2.0.5.0-alpha2", $version); $this->assertEquals("2.0.5.0-alpha2", $version);
} }
@ -129,8 +129,8 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($executor, new VersionParser()); $guesser = new VersionGuesser($config, $executor, new VersionParser());
$version = $guesser->guessVersion($config, array()); $version = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-foo", $version); $this->assertEquals("dev-foo", $version);
} }