瀏覽代碼

Use version guesser to get local package version

Samuel ROZE 9 年之前
父節點
當前提交
623f31fcc4
共有 2 個文件被更改,包括 45 次插入13 次删除
  1. 14 7
      src/Composer/Package/Version/VersionGuesser.php
  2. 31 6
      src/Composer/Repository/PathRepository.php

+ 14 - 7
src/Composer/Package/Version/VersionGuesser.php

@@ -21,14 +21,21 @@ class VersionGuesser
      */
     private $versionParser;
 
+    /**
+     * @var null|string
+     */
+    private $cwd;
+
     /**
      * @param ProcessExecutor $process
      * @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->versionParser = $versionParser;
+        $this->cwd = $cwd ?: getcwd();
     }
 
     public function guessVersion(Config $config, array $packageConfig)
@@ -53,7 +60,7 @@ class VersionGuesser
         GitUtil::cleanEnv();
 
         // 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 {
                 return $this->versionParser->normalize(trim($output));
             } catch (\Exception $e) {
@@ -61,7 +68,7 @@ class VersionGuesser
         }
 
         // 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();
             $isFeatureBranch = false;
             $version = null;
@@ -102,7 +109,7 @@ class VersionGuesser
     private function guessHgVersion(Config $config, array $packageConfig)
     {
         // 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);
             $version = $this->versionParser->normalizeBranch($branch);
             $isFeatureBranch = 0 === strpos($version, 'dev-');
@@ -116,7 +123,7 @@ class VersionGuesser
             }
 
             // 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);
             $branches = array_keys($driver->getBranches());
 
@@ -154,7 +161,7 @@ class VersionGuesser
                 }
 
                 $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;
                 }
 
@@ -176,7 +183,7 @@ class VersionGuesser
         SvnUtil::cleanEnv();
 
         // 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';
             $branchesPath = isset($config['branches-path']) ? preg_quote($config['branches-path'], '#') : 'branches';
             $tagsPath = isset($config['tags-path']) ? preg_quote($config['tags-path'], '#') : 'tags';

+ 31 - 6
src/Composer/Repository/PathRepository.php

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