1
0
Fork 0

Use version guesser to get local package version

pull/4365/head
Samuel ROZE 2015-08-18 15:08:27 +01:00
parent 5261a5fa03
commit 623f31fcc4
2 changed files with 45 additions and 13 deletions

View File

@ -21,14 +21,21 @@ class VersionGuesser
*/ */
private $versionParser; private $versionParser;
/**
* @var null|string
*/
private $cwd;
/** /**
* @param ProcessExecutor $process * @param ProcessExecutor $process
* @param VersionParser $versionParser * @param VersionParser $versionParser
* @param string $cwd
*/ */
public function __construct(ProcessExecutor $process, VersionParser $versionParser) public function __construct(ProcessExecutor $process, VersionParser $versionParser, $cwd = null)
{ {
$this->process = $process; $this->process = $process;
$this->versionParser = $versionParser; $this->versionParser = $versionParser;
$this->cwd = $cwd ?: getcwd();
} }
public function guessVersion(Config $config, array $packageConfig) public function guessVersion(Config $config, array $packageConfig)
@ -53,7 +60,7 @@ class VersionGuesser
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)) { if (0 === $this->process->execute('git describe --exact-match --tags', $output, $this->cwd)) {
try { try {
return $this->versionParser->normalize(trim($output)); return $this->versionParser->normalize(trim($output));
} catch (\Exception $e) { } catch (\Exception $e) {
@ -61,7 +68,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)) { if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->cwd)) {
$branches = array(); $branches = array();
$isFeatureBranch = false; $isFeatureBranch = false;
$version = null; $version = null;
@ -102,7 +109,7 @@ class VersionGuesser
private function guessHgVersion(Config $config, array $packageConfig) private function guessHgVersion(Config $config, array $packageConfig)
{ {
// try to fetch current version from hg branch // try to fetch current version from hg branch
if (0 === $this->process->execute('hg branch', $output)) { if (0 === $this->process->execute('hg branch', $output, $this->cwd)) {
$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-');
@ -116,7 +123,7 @@ 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' => getcwd()); $packageConfig = array('url' => $this->cwd);
$driver = new HgDriver($packageConfig, new NullIO(), $config, $this->process); $driver = new HgDriver($packageConfig, new NullIO(), $config, $this->process);
$branches = array_keys($driver->getBranches()); $branches = array_keys($driver->getBranches());
@ -154,7 +161,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)) { if (0 !== $this->process->execute($cmdLine, $output, $this->cwd)) {
continue; continue;
} }
@ -176,7 +183,7 @@ class VersionGuesser
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)) { if (0 === $this->process->execute('svn info --xml', $output, $this->cwd)) {
$trunkPath = isset($config['trunk-path']) ? preg_quote($config['trunk-path'], '#') : 'trunk'; $trunkPath = isset($config['trunk-path']) ? preg_quote($config['trunk-path'], '#') : 'trunk';
$branchesPath = isset($config['branches-path']) ? preg_quote($config['branches-path'], '#') : 'branches'; $branchesPath = isset($config['branches-path']) ? preg_quote($config['branches-path'], '#') : 'branches';
$tagsPath = isset($config['tags-path']) ? preg_quote($config['tags-path'], '#') : 'tags'; $tagsPath = isset($config['tags-path']) ? preg_quote($config['tags-path'], '#') : 'tags';

View File

@ -12,8 +12,13 @@
namespace Composer\Repository; namespace Composer\Repository;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
/** /**
@ -42,6 +47,11 @@ use Symfony\Component\Filesystem\Filesystem;
*/ */
class PathRepository extends ArrayRepository class PathRepository extends ArrayRepository
{ {
/**
* @var Config
*/
private $config;
/** /**
* @var Filesystem * @var Filesystem
*/ */
@ -52,6 +62,16 @@ class PathRepository extends ArrayRepository
*/ */
private $loader; private $loader;
/**
* @var VersionGuesser
*/
private $versionGuesser;
/**
* @var array
*/
private $packageConfig;
/** /**
* @var string * @var string
*/ */
@ -60,17 +80,22 @@ class PathRepository extends ArrayRepository
/** /**
* Initializes path repository. * Initializes path repository.
* *
* @param array $config package definition * @param array $packageConfig
* @param IOInterface $io
* @param Config $config
*/ */
public function __construct(array $config) public function __construct(array $packageConfig, IOInterface $io, Config $config)
{ {
if (!isset($config['url'])) { if (!isset($packageConfig['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 = new Filesystem(); $this->fileSystem = new Filesystem();
$this->loader = new ArrayLoader(); $this->loader = new ArrayLoader();
$this->path = realpath(rtrim($config['url'], '/')) . '/'; $this->config = $config;
$this->packageConfig = $packageConfig;
$this->path = realpath(rtrim($packageConfig['url'], '/')) . '/';
$this->versionGuesser = new VersionGuesser(new ProcessExecutor($io), new VersionParser(), $this->path);
} }
/** /**
@ -91,12 +116,12 @@ class PathRepository extends ArrayRepository
$json = file_get_contents($composerFilePath); $json = file_get_contents($composerFilePath);
$package = JsonFile::parseJson($json, $composerFilePath); $package = JsonFile::parseJson($json, $composerFilePath);
$package['dist'] = array( $package['dist'] = array(
'type' => 'folder', 'type' => 'path',
'url' => $this->path, 'url' => $this->path,
); );
if (!isset($package['version'])) { if (!isset($package['version'])) {
$package['version'] = 'dev-master'; $package['version'] = $this->versionGuesser->guessVersion($this->config, $this->packageConfig) ?: 'dev-master';
} }
$package = $this->loader->load($package); $package = $this->loader->load($package);