Ver código fonte

Update interface to merge vcs with basic archivers

Matthieu Moquet 13 anos atrás
pai
commit
bfd2275cb0

+ 4 - 20
src/Composer/Package/Archiver/ArchiveManager.php

@@ -30,8 +30,6 @@ class ArchiveManager
 
     protected $archivers = array();
 
-    protected $vcsArchivers = array();
-
     /**
      * @param string          $buildDir        The directory used to build the archive
      * @param DownloadManager $downloadManager A manager used to download package sources
@@ -53,11 +51,7 @@ class ArchiveManager
      */
     public function addArchiver(ArchiverInterface $archiver)
     {
-        if ($archiver instanceof VcsArchiver) {
-            $this->vcsArchivers[$archiver->getSourceType()] = $archiver;
-        } else {
-            $this->archivers[] = $archiver;
-        }
+        $this->archivers[] = $archiver;
     }
 
     /**
@@ -88,21 +82,11 @@ class ArchiveManager
         // Download sources
         $this->downloadManager->download($package, $sources, true);
 
-        // Try VCS archivers first
         $sourceType = $package->getSourceType();
-        if (isset($this->archivers[$sourceType]) && $this->archivers[$sourceType]->supports($format)) {
-            $archiver = $this->archivers[$sourceType];
-            $archiver->setSourceRef($sourceRef);
-            $archiver->setFormat($format);
-            $archiver->archive($sources, $target);
-
-            return $target;
-        }
-
-        // Fallback on default archivers
+        $sourceRef  = $package->getSourceReference();
         foreach ($this->archivers as $archiver) {
-            if ($archiver->supports($format)) {
-                $archiver->archive($sources, $target);
+            if ($archiver->supports($format, $sourceType)) {
+                $archiver->archive($sources, $target, $format, $sourceRef);
 
                 return $target;
             }

+ 9 - 5
src/Composer/Package/Archiver/ArchiverInterface.php

@@ -23,17 +23,21 @@ interface ArchiverInterface
     /**
      * Create an archive from the sources.
      *
-     * @param string $source The sources directory
-     * @param string $target The target file
+     * @param string $source    The sources directory
+     * @param string $target    The target file
+     * @param string $format    The format used for archive
+     * @param string $sourceRef The reference of the source to archive or null
+     *                          for the current reference
      */
-    public function archive($sources, $target);
+    public function archive($sources, $target, $format, $sourceRef = null);
 
     /**
      * Format supported by the archiver.
      *
-     * @param string $format The format to support
+     * @param string $format     The archive format
+     * @param string $sourceType The source type (git, svn, hg, etc.)
      *
      * @return boolean true if the format is supported by the archiver
      */
-    public function supports($format);
+    public function supports($format, $sourceType);
 }

+ 19 - 15
src/Composer/Package/Archiver/GitArchiver.php

@@ -12,19 +12,31 @@
 
 namespace Composer\Package\Archiver;
 
+use Composer\Util\ProcessExecutor;
+
 /**
  * @author Till Klampaeckel <till@php.net>
  * @author Matthieu Moquet <matthieu@moquet.net>
  */
-class GitArchiver extends VcsArchiver
+class GitArchiver implements ArchiverInterface
 {
+    protected $process;
+
+    public function __construct($process = null)
+    {
+        $this->process = $process ?: new ProcessExecutor();
+    }
+
     /**
      * {@inheritdoc}
      */
-    public function archive($source, $target)
+    public function archive($sources, $target, $format, $sourceRef = null)
     {
-        $format = $this->format ?: 'zip';
-        $sourceRef = $this->sourceRef ?: 'HEAD';
+        // Since git-archive no longer works with a commit ID in git 1.7.10,
+        // use by default the HEAD reference instead of the commit sha1
+        if (null === $sourceRef || preg_match('/^[0-9a-f]{40}$/i',$sourceRef)) {
+            $sourceRef = 'HEAD';
+        }
 
         $command = sprintf(
             'git archive --format %s --output %s %s',
@@ -33,7 +45,7 @@ class GitArchiver extends VcsArchiver
             $sourceRef
         );
 
-        $exitCode = $this->process->execute($command, $output, $source);
+        $exitCode = $this->process->execute($command, $output, $sources);
 
         if (0 !== $exitCode) {
             throw new \RuntimeException(
@@ -45,17 +57,9 @@ class GitArchiver extends VcsArchiver
     /**
      * {@inheritdoc}
      */
-    public function getSourceType()
-    {
-        return 'git';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports($format)
+    public function supports($format, $sourceType)
     {
-        return in_array($format, array(
+        return 'git' === $sourceType && in_array($format, array(
             'zip',
             'tar',
             'tgz',

+ 17 - 15
src/Composer/Package/Archiver/MercurialArchiver.php

@@ -12,19 +12,29 @@
 
 namespace Composer\Package\Archiver;
 
+use Composer\Util\ProcessExecutor;
+
 /**
  * @author Till Klampaeckel <till@php.net>
  * @author Matthieu Moquet <matthieu@moquet.net>
  */
-class MercurialArchiver extends VcsArchiver
+class MercurialArchiver implements ArchiverInterface
 {
+    protected $process;
+
+    public function __construct($process = null)
+    {
+        $this->process = $process ?: new ProcessExecutor();
+    }
+
     /**
      * {@inheritdoc}
      */
-    public function archive($source, $target)
+    public function archive($sources, $target, $format, $sourceRef = null)
     {
-        $format = $this->format ?: 'zip';
-        $sourceRef = $this->sourceRef ?: 'default';
+        if (null === $sourceRef) {
+            $sourceRef = 'default';
+        }
 
         $command = sprintf(
             'hg archive --rev %s --type %s %s',
@@ -33,7 +43,7 @@ class MercurialArchiver extends VcsArchiver
             escapeshellarg($target)
         );
 
-        $exitCode = $this->process->execute($command, $output, $source);
+        $exitCode = $this->process->execute($command, $output, $sources);
 
         if (0 !== $exitCode) {
             throw new \RuntimeException(
@@ -45,17 +55,9 @@ class MercurialArchiver extends VcsArchiver
     /**
      * {@inheritdoc}
      */
-    public function getSourceType()
-    {
-        return 'hg';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function supports($format)
+    public function supports($format, $sourceType)
     {
-        return in_array($format, array(
+        return 'hg' === $sourceType && in_array($format, array(
             'tar',
             'tbz2',
             'tgz',

+ 2 - 2
src/Composer/Package/Archiver/TarArchiver.php

@@ -24,7 +24,7 @@ class TarArchiver extends BaseArchiver
     /**
      * {@inheritdoc}
      */
-    public function archive($sources, $target)
+    public function archive($sources, $target, $format, $sourceRef = null)
     {
         $this->createPharArchive($sources, $target, \Phar::TAR);
     }
@@ -32,7 +32,7 @@ class TarArchiver extends BaseArchiver
     /**
      * {@inheritdoc}
      */
-    public function supports($format)
+    public function supports($format, $sourceType)
     {
         return 'tar' === $format;
     }

+ 0 - 60
src/Composer/Package/Archiver/VcsArchiver.php

@@ -1,60 +0,0 @@
-<?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\Package\Archiver;
-
-use Composer\Util\ProcessExecutor;
-
-/**
- * VCS archivers are optimized for a specific source type.
- *
- * @author Till Klampaeckel <till@php.net>
- * @author Matthieu Moquet <matthieu@moquet.net>
- */
-abstract class VcsArchiver implements ArchiverInterface
-{
-    protected $process;
-    protected $sourceRef;
-    protected $format;
-
-    public function __construct($process = null)
-    {
-        $this->process = $process ?: new ProcessExecutor();
-    }
-
-    public function getSourceRef()
-    {
-        return $this->sourceRef;
-    }
-
-    public function setSourceRef($sourceRef)
-    {
-        $this->sourceRef = $sourceRef;
-    }
-
-    public function getFormat()
-    {
-        return $this->format;
-    }
-
-    public function setFormat($format)
-    {
-        $this->format = $format;
-    }
-
-    /**
-     * Get the source type supported by the archiver.
-     *
-     * @return string The source type of the archiver
-     */
-    abstract public function getSourceType();
-}

+ 2 - 2
src/Composer/Package/Archiver/ZipArchiver.php

@@ -24,7 +24,7 @@ class ZipArchiver extends BaseArchiver
     /**
      * {@inheritdoc}
      */
-    public function archive($sources, $target)
+    public function archive($sources, $target, $format, $sourceRef = null)
     {
         $this->createPharArchive($sources, $target, \Phar::ZIP);
     }
@@ -32,7 +32,7 @@ class ZipArchiver extends BaseArchiver
     /**
      * {@inheritdoc}
      */
-    public function supports($format)
+    public function supports($format, $sourceType)
     {
         return 'zip' === $format;
     }

+ 2 - 6
tests/Composer/Test/Package/Archiver/GitArchiverTest.php

@@ -29,9 +29,7 @@ class GitArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new GitArchiver();
-        $archiver->setFormat('zip');
-        $archiver->setSourceRef('master');
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'zip', 'master');
         $this->assertFileExists($target);
 
         unlink($target);
@@ -47,9 +45,7 @@ class GitArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new GitArchiver();
-        $archiver->setFormat('tar');
-        $archiver->setSourceRef('master');
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'tar', 'master');
         $this->assertFileExists($target);
 
         unlink($target);

+ 2 - 6
tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php

@@ -30,9 +30,7 @@ class MercurialArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new MercurialArchiver();
-        $archiver->setFormat('zip');
-        $archiver->setSourceRef('default');
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'zip', 'default');
         $this->assertFileExists($target);
 
         unlink($target);
@@ -48,9 +46,7 @@ class MercurialArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new MercurialArchiver();
-        $archiver->setFormat('tar');
-        $archiver->setSourceRef('default');
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'tar', 'default');
         $this->assertFileExists($target);
 
         unlink($target);

+ 1 - 1
tests/Composer/Test/Package/Archiver/TarArchiverTest.php

@@ -29,7 +29,7 @@ class TarArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new TarArchiver();
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'tar');
         $this->assertFileExists($target);
 
         unlink($target);

+ 1 - 1
tests/Composer/Test/Package/Archiver/ZipArchiverTest.php

@@ -29,7 +29,7 @@ class ZipArchiverTest extends ArchiverTest
 
         // Test archive
         $archiver = new ZipArchiver();
-        $archiver->archive($package->getSourceUrl(), $target);
+        $archiver->archive($package->getSourceUrl(), $target, 'zip');
         $this->assertFileExists($target);
 
         unlink($target);