فهرست منبع

Merge pull request #4479 from MakiCode/feature#4397

Added --file flag: Feature#4397
Jordi Boggiano 9 سال پیش
والد
کامیت
a54f84f05f

+ 8 - 5
src/Composer/Command/ArchiveCommand.php

@@ -40,8 +40,10 @@ class ArchiveCommand extends Command
             ->setDefinition(array(
                 new InputArgument('package', InputArgument::OPTIONAL, 'The package to archive instead of the current project'),
                 new InputArgument('version', InputArgument::OPTIONAL, 'A version constraint to find the package to archive'),
-                new InputOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Format of the resulting archive: tar or zip'),
-                new InputOption('dir', false, InputOption::VALUE_OPTIONAL, 'Write the archive to this directory'),
+                new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the resulting archive: tar or zip'),
+                new InputOption('dir', false, InputOption::VALUE_REQUIRED, 'Write the archive to this directory'),
+                new InputOption('file', false, InputOption::VALUE_REQUIRED, 'Write the archive with the given file name.'
+                    .' Note that the format will be appended.'),
             ))
             ->setHelp(<<<EOT
 The <info>archive</info> command creates an archive of the specified format
@@ -78,7 +80,8 @@ EOT
             $input->getArgument('package'),
             $input->getArgument('version'),
             $input->getOption('format'),
-            $input->getOption('dir')
+            $input->getOption('dir'),
+            $input->getOption('file')
         );
 
         if (0 === $returnCode && $composer) {
@@ -88,7 +91,7 @@ EOT
         return $returnCode;
     }
 
-    protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.')
+    protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.', $fileName = null)
     {
         $factory = new Factory;
         $downloadManager = $factory->createDownloadManager($io, $config);
@@ -105,7 +108,7 @@ EOT
         }
 
         $io->writeError('<info>Creating the archive into "'.$dest.'".</info>');
-        $packagePath = $archiveManager->archive($package, $format, $dest);
+        $packagePath = $archiveManager->archive($package, $format, $dest, $fileName);
         $fs = new Filesystem;
         $shortPath = $fs->findShortestPath(getcwd(), $packagePath, true);
 

+ 9 - 3
src/Composer/Package/Archiver/ArchiveManager.php

@@ -96,12 +96,14 @@ class ArchiveManager
      *
      * @param  PackageInterface          $package   The package to archive
      * @param  string                    $format    The format of the archive (zip, tar, ...)
-     * @param  string                    $targetDir The diretory where to build the archive
+     * @param  string                    $targetDir The directory where to build the archive
+     * @param  string|null               $fileName  The relative file name to use for the archive, or null to generate
+     *                                              the package name. Note that the format will be appended to this name
      * @throws \InvalidArgumentException
      * @throws \RuntimeException
      * @return string                    The path of the created archive
      */
-    public function archive(PackageInterface $package, $format, $targetDir)
+    public function archive(PackageInterface $package, $format, $targetDir, $fileName = null)
     {
         if (empty($format)) {
             throw new \InvalidArgumentException('Format must be specified');
@@ -122,7 +124,11 @@ class ArchiveManager
         }
 
         $filesystem = new Filesystem();
-        $packageName = $this->getPackageFilename($package);
+        if (null === $fileName) {
+            $packageName = $this->getPackageFilename($package);
+        } else {
+            $packageName = $fileName;
+        }
 
         // Archive filename
         $filesystem->ensureDirectoryExists($targetDir);

+ 32 - 2
tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php

@@ -13,11 +13,16 @@
 namespace Composer\Test\Package\Archiver;
 
 use Composer\Factory;
+use Composer\Package\Archiver\ArchiveManager;
 use Composer\Package\PackageInterface;
 
 class ArchiveManagerTest extends ArchiverTest
 {
+    /**
+     * @var ArchiveManager
+     */
     protected $manager;
+
     protected $targetDir;
 
     public function setUp()
@@ -55,9 +60,34 @@ class ArchiveManagerTest extends ArchiverTest
         unlink($target);
     }
 
-    protected function getTargetName(PackageInterface $package, $format)
+    public function testArchiveCustomFileName()
+    {
+        $this->setupGitRepo();
+
+        $package = $this->setupPackage();
+
+        $fileName = 'testArchiveName';
+
+        $this->manager->archive($package, 'tar', $this->targetDir, $fileName);
+
+        $target = $this->targetDir . '/' . $fileName . '.tar';
+
+        $this->assertFileExists($target);
+
+        $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
+        $this->assertFileNotExists($tmppath);
+
+        unlink($target);
+    }
+    
+    protected function getTargetName(PackageInterface $package, $format, $fileName = null)
     {
-        $packageName = $this->manager->getPackageFilename($package);
+        if(null === $fileName) {
+            $packageName = $this->manager->getPackageFilename($package);
+        } else {
+            $packageName = $fileName;
+        }
+
         $target = $this->targetDir.'/'.$packageName.'.'.$format;
 
         return $target;