Update interface to merge vcs with basic archivers
parent
3b22791059
commit
bfd2275cb0
|
@ -30,8 +30,6 @@ class ArchiveManager
|
||||||
|
|
||||||
protected $archivers = array();
|
protected $archivers = array();
|
||||||
|
|
||||||
protected $vcsArchivers = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $buildDir The directory used to build the archive
|
* @param string $buildDir The directory used to build the archive
|
||||||
* @param DownloadManager $downloadManager A manager used to download package sources
|
* @param DownloadManager $downloadManager A manager used to download package sources
|
||||||
|
@ -53,12 +51,8 @@ class ArchiveManager
|
||||||
*/
|
*/
|
||||||
public function addArchiver(ArchiverInterface $archiver)
|
public function addArchiver(ArchiverInterface $archiver)
|
||||||
{
|
{
|
||||||
if ($archiver instanceof VcsArchiver) {
|
|
||||||
$this->vcsArchivers[$archiver->getSourceType()] = $archiver;
|
|
||||||
} else {
|
|
||||||
$this->archivers[] = $archiver;
|
$this->archivers[] = $archiver;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an archive of the specified package.
|
* Create an archive of the specified package.
|
||||||
|
@ -88,21 +82,11 @@ class ArchiveManager
|
||||||
// Download sources
|
// Download sources
|
||||||
$this->downloadManager->download($package, $sources, true);
|
$this->downloadManager->download($package, $sources, true);
|
||||||
|
|
||||||
// Try VCS archivers first
|
|
||||||
$sourceType = $package->getSourceType();
|
$sourceType = $package->getSourceType();
|
||||||
if (isset($this->archivers[$sourceType]) && $this->archivers[$sourceType]->supports($format)) {
|
$sourceRef = $package->getSourceReference();
|
||||||
$archiver = $this->archivers[$sourceType];
|
|
||||||
$archiver->setSourceRef($sourceRef);
|
|
||||||
$archiver->setFormat($format);
|
|
||||||
$archiver->archive($sources, $target);
|
|
||||||
|
|
||||||
return $target;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback on default archivers
|
|
||||||
foreach ($this->archivers as $archiver) {
|
foreach ($this->archivers as $archiver) {
|
||||||
if ($archiver->supports($format)) {
|
if ($archiver->supports($format, $sourceType)) {
|
||||||
$archiver->archive($sources, $target);
|
$archiver->archive($sources, $target, $format, $sourceRef);
|
||||||
|
|
||||||
return $target;
|
return $target;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,15 +25,19 @@ interface ArchiverInterface
|
||||||
*
|
*
|
||||||
* @param string $source The sources directory
|
* @param string $source The sources directory
|
||||||
* @param string $target The target file
|
* @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.
|
* 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
|
* @return boolean true if the format is supported by the archiver
|
||||||
*/
|
*/
|
||||||
public function supports($format);
|
public function supports($format, $sourceType);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,19 +12,31 @@
|
||||||
|
|
||||||
namespace Composer\Package\Archiver;
|
namespace Composer\Package\Archiver;
|
||||||
|
|
||||||
|
use Composer\Util\ProcessExecutor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Till Klampaeckel <till@php.net>
|
* @author Till Klampaeckel <till@php.net>
|
||||||
* @author Matthieu Moquet <matthieu@moquet.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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function archive($source, $target)
|
public function archive($sources, $target, $format, $sourceRef = null)
|
||||||
{
|
{
|
||||||
$format = $this->format ?: 'zip';
|
// Since git-archive no longer works with a commit ID in git 1.7.10,
|
||||||
$sourceRef = $this->sourceRef ?: 'HEAD';
|
// 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(
|
$command = sprintf(
|
||||||
'git archive --format %s --output %s %s',
|
'git archive --format %s --output %s %s',
|
||||||
|
@ -33,7 +45,7 @@ class GitArchiver extends VcsArchiver
|
||||||
$sourceRef
|
$sourceRef
|
||||||
);
|
);
|
||||||
|
|
||||||
$exitCode = $this->process->execute($command, $output, $source);
|
$exitCode = $this->process->execute($command, $output, $sources);
|
||||||
|
|
||||||
if (0 !== $exitCode) {
|
if (0 !== $exitCode) {
|
||||||
throw new \RuntimeException(
|
throw new \RuntimeException(
|
||||||
|
@ -45,17 +57,9 @@ class GitArchiver extends VcsArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getSourceType()
|
public function supports($format, $sourceType)
|
||||||
{
|
{
|
||||||
return 'git';
|
return 'git' === $sourceType && in_array($format, array(
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function supports($format)
|
|
||||||
{
|
|
||||||
return in_array($format, array(
|
|
||||||
'zip',
|
'zip',
|
||||||
'tar',
|
'tar',
|
||||||
'tgz',
|
'tgz',
|
||||||
|
|
|
@ -12,19 +12,29 @@
|
||||||
|
|
||||||
namespace Composer\Package\Archiver;
|
namespace Composer\Package\Archiver;
|
||||||
|
|
||||||
|
use Composer\Util\ProcessExecutor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Till Klampaeckel <till@php.net>
|
* @author Till Klampaeckel <till@php.net>
|
||||||
* @author Matthieu Moquet <matthieu@moquet.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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function archive($source, $target)
|
public function archive($sources, $target, $format, $sourceRef = null)
|
||||||
{
|
{
|
||||||
$format = $this->format ?: 'zip';
|
if (null === $sourceRef) {
|
||||||
$sourceRef = $this->sourceRef ?: 'default';
|
$sourceRef = 'default';
|
||||||
|
}
|
||||||
|
|
||||||
$command = sprintf(
|
$command = sprintf(
|
||||||
'hg archive --rev %s --type %s %s',
|
'hg archive --rev %s --type %s %s',
|
||||||
|
@ -33,7 +43,7 @@ class MercurialArchiver extends VcsArchiver
|
||||||
escapeshellarg($target)
|
escapeshellarg($target)
|
||||||
);
|
);
|
||||||
|
|
||||||
$exitCode = $this->process->execute($command, $output, $source);
|
$exitCode = $this->process->execute($command, $output, $sources);
|
||||||
|
|
||||||
if (0 !== $exitCode) {
|
if (0 !== $exitCode) {
|
||||||
throw new \RuntimeException(
|
throw new \RuntimeException(
|
||||||
|
@ -45,17 +55,9 @@ class MercurialArchiver extends VcsArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getSourceType()
|
public function supports($format, $sourceType)
|
||||||
{
|
{
|
||||||
return 'hg';
|
return 'hg' === $sourceType && in_array($format, array(
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function supports($format)
|
|
||||||
{
|
|
||||||
return in_array($format, array(
|
|
||||||
'tar',
|
'tar',
|
||||||
'tbz2',
|
'tbz2',
|
||||||
'tgz',
|
'tgz',
|
||||||
|
|
|
@ -24,7 +24,7 @@ class TarArchiver extends BaseArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function archive($sources, $target)
|
public function archive($sources, $target, $format, $sourceRef = null)
|
||||||
{
|
{
|
||||||
$this->createPharArchive($sources, $target, \Phar::TAR);
|
$this->createPharArchive($sources, $target, \Phar::TAR);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ class TarArchiver extends BaseArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function supports($format)
|
public function supports($format, $sourceType)
|
||||||
{
|
{
|
||||||
return 'tar' === $format;
|
return 'tar' === $format;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
|
@ -24,7 +24,7 @@ class ZipArchiver extends BaseArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function archive($sources, $target)
|
public function archive($sources, $target, $format, $sourceRef = null)
|
||||||
{
|
{
|
||||||
$this->createPharArchive($sources, $target, \Phar::ZIP);
|
$this->createPharArchive($sources, $target, \Phar::ZIP);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ class ZipArchiver extends BaseArchiver
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function supports($format)
|
public function supports($format, $sourceType)
|
||||||
{
|
{
|
||||||
return 'zip' === $format;
|
return 'zip' === $format;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,9 +29,7 @@ class GitArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new GitArchiver();
|
$archiver = new GitArchiver();
|
||||||
$archiver->setFormat('zip');
|
$archiver->archive($package->getSourceUrl(), $target, 'zip', 'master');
|
||||||
$archiver->setSourceRef('master');
|
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
@ -47,9 +45,7 @@ class GitArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new GitArchiver();
|
$archiver = new GitArchiver();
|
||||||
$archiver->setFormat('tar');
|
$archiver->archive($package->getSourceUrl(), $target, 'tar', 'master');
|
||||||
$archiver->setSourceRef('master');
|
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
|
|
@ -30,9 +30,7 @@ class MercurialArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new MercurialArchiver();
|
$archiver = new MercurialArchiver();
|
||||||
$archiver->setFormat('zip');
|
$archiver->archive($package->getSourceUrl(), $target, 'zip', 'default');
|
||||||
$archiver->setSourceRef('default');
|
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
@ -48,9 +46,7 @@ class MercurialArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new MercurialArchiver();
|
$archiver = new MercurialArchiver();
|
||||||
$archiver->setFormat('tar');
|
$archiver->archive($package->getSourceUrl(), $target, 'tar', 'default');
|
||||||
$archiver->setSourceRef('default');
|
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
|
|
@ -29,7 +29,7 @@ class TarArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new TarArchiver();
|
$archiver = new TarArchiver();
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
$archiver->archive($package->getSourceUrl(), $target, 'tar');
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
|
|
@ -29,7 +29,7 @@ class ZipArchiverTest extends ArchiverTest
|
||||||
|
|
||||||
// Test archive
|
// Test archive
|
||||||
$archiver = new ZipArchiver();
|
$archiver = new ZipArchiver();
|
||||||
$archiver->archive($package->getSourceUrl(), $target);
|
$archiver->archive($package->getSourceUrl(), $target, 'zip');
|
||||||
$this->assertFileExists($target);
|
$this->assertFileExists($target);
|
||||||
|
|
||||||
unlink($target);
|
unlink($target);
|
||||||
|
|
Loading…
Reference in New Issue