1
0
Fork 0

Add ignore filters option to archive command

pull/6062/head
jbixler 2017-01-11 09:15:20 -06:00
parent 285ff274ac
commit dac51c7f4b
7 changed files with 79 additions and 16 deletions

View File

@ -45,6 +45,7 @@ class ArchiveCommand extends BaseCommand
new InputOption('dir', null, InputOption::VALUE_REQUIRED, 'Write the archive to this directory'), new InputOption('dir', null, InputOption::VALUE_REQUIRED, 'Write the archive to this directory'),
new InputOption('file', null, InputOption::VALUE_REQUIRED, 'Write the archive with the given file name.' new InputOption('file', null, InputOption::VALUE_REQUIRED, 'Write the archive with the given file name.'
.' Note that the format will be appended.'), .' Note that the format will be appended.'),
new InputOption('ignore-filters', false, InputOption::VALUE_NONE, 'Ignore filters when saving package')
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The <info>archive</info> command creates an archive of the specified format The <info>archive</info> command creates an archive of the specified format
@ -82,7 +83,8 @@ EOT
$input->getArgument('version'), $input->getArgument('version'),
$input->getOption('format'), $input->getOption('format'),
$input->getOption('dir'), $input->getOption('dir'),
$input->getOption('file') $input->getOption('file'),
$input->getOption('ignore-filters')
); );
if (0 === $returnCode && $composer) { if (0 === $returnCode && $composer) {
@ -92,7 +94,7 @@ EOT
return $returnCode; return $returnCode;
} }
protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.', $fileName = null) protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.', $fileName = null, $ignoreFilters)
{ {
$factory = new Factory; $factory = new Factory;
$downloadManager = $factory->createDownloadManager($io, $config); $downloadManager = $factory->createDownloadManager($io, $config);
@ -109,7 +111,7 @@ EOT
} }
$io->writeError('<info>Creating the archive into "'.$dest.'".</info>'); $io->writeError('<info>Creating the archive into "'.$dest.'".</info>');
$packagePath = $archiveManager->archive($package, $format, $dest, $fileName); $packagePath = $archiveManager->archive($package, $format, $dest, $fileName, $ignoreFilters);
$fs = new Filesystem; $fs = new Filesystem;
$shortPath = $fs->findShortestPath(getcwd(), $packagePath, true); $shortPath = $fs->findShortestPath(getcwd(), $packagePath, true);

View File

@ -37,18 +37,23 @@ class ArchivableFilesFinder extends \FilterIterator
* *
* @param string $sources Path to source files to be archived * @param string $sources Path to source files to be archived
* @param array $excludes Composer's own exclude rules from composer.json * @param array $excludes Composer's own exclude rules from composer.json
* @param boolean $ignoreFilters Ignore filters when looking for files
*/ */
public function __construct($sources, array $excludes) public function __construct($sources, array $excludes, $ignoreFilters = false)
{ {
$fs = new Filesystem(); $fs = new Filesystem();
$sources = $fs->normalizePath($sources); $sources = $fs->normalizePath($sources);
$filters = array( if ($ignoreFilters) {
new HgExcludeFilter($sources), $filters = array();
new GitExcludeFilter($sources), } else {
new ComposerExcludeFilter($sources, $excludes), $filters = array(
); new HgExcludeFilter($sources),
new GitExcludeFilter($sources),
new ComposerExcludeFilter($sources, $excludes),
);
}
$this->finder = new Finder(); $this->finder = new Finder();

View File

@ -99,11 +99,12 @@ class ArchiveManager
* @param string $targetDir The directory 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 * @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 * the package name. Note that the format will be appended to this name
* @param boolean $ignoreFilters Ignore filters when looking for files in the package
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws \RuntimeException * @throws \RuntimeException
* @return string The path of the created archive * @return string The path of the created archive
*/ */
public function archive(PackageInterface $package, $format, $targetDir, $fileName = null) public function archive(PackageInterface $package, $format, $targetDir, $fileName = null, $ignoreFilters = false)
{ {
if (empty($format)) { if (empty($format)) {
throw new \InvalidArgumentException('Format must be specified'); throw new \InvalidArgumentException('Format must be specified');
@ -163,7 +164,7 @@ class ArchiveManager
$tempTarget = sys_get_temp_dir().'/composer_archive'.uniqid().'.'.$format; $tempTarget = sys_get_temp_dir().'/composer_archive'.uniqid().'.'.$format;
$filesystem->ensureDirectoryExists(dirname($tempTarget)); $filesystem->ensureDirectoryExists(dirname($tempTarget));
$archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, $package->getArchiveExcludes()); $archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, $package->getArchiveExcludes(), $ignoreFilters);
$filesystem->rename($archivePath, $target); $filesystem->rename($archivePath, $target);
// cleanup temporary download // cleanup temporary download

View File

@ -29,7 +29,7 @@ interface ArchiverInterface
* *
* @return string The path to the written archive file * @return string The path to the written archive file
*/ */
public function archive($sources, $target, $format, array $excludes = array()); public function archive($sources, $target, $format, array $excludes = array(), $ignoreFilters = false);
/** /**
* Format supported by the archiver. * Format supported by the archiver.

View File

@ -34,7 +34,7 @@ class PharArchiver implements ArchiverInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function archive($sources, $target, $format, array $excludes = array()) public function archive($sources, $target, $format, array $excludes = array(), $ignoreFilters = false)
{ {
$sources = realpath($sources); $sources = realpath($sources);
@ -53,7 +53,7 @@ class PharArchiver implements ArchiverInterface
} }
$phar = new \PharData($target, null, null, static::$formats[$format]); $phar = new \PharData($target, null, null, static::$formats[$format]);
$files = new ArchivableFilesFinder($sources, $excludes); $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters);
$filesOnly = new ArchivableFilesFilter($files); $filesOnly = new ArchivableFilesFilter($files);
$phar->buildFromIterator($filesOnly, $sources); $phar->buildFromIterator($filesOnly, $sources);
$filesOnly->addEmptyDir($phar, $sources); $filesOnly->addEmptyDir($phar, $sources);

View File

@ -27,7 +27,7 @@ class ZipArchiver implements ArchiverInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function archive($sources, $target, $format, array $excludes = array()) public function archive($sources, $target, $format, array $excludes = array(), $ignoreFilters = false)
{ {
$fs = new Filesystem(); $fs = new Filesystem();
$sources = $fs->normalizePath($sources); $sources = $fs->normalizePath($sources);
@ -35,7 +35,7 @@ class ZipArchiver implements ArchiverInterface
$zip = new ZipArchive(); $zip = new ZipArchive();
$res = $zip->open($target, ZipArchive::CREATE); $res = $zip->open($target, ZipArchive::CREATE);
if ($res === true) { if ($res === true) {
$files = new ArchivableFilesFinder($sources, $excludes); $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters);
foreach ($files as $file) { foreach ($files as $file) {
/** @var $file \SplFileInfo */ /** @var $file \SplFileInfo */
$filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/'); $filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/');

View File

@ -235,6 +235,61 @@ class ArchivableFilesFinderTest extends TestCase
$this->assertArchivableFiles($expectedFiles); $this->assertArchivableFiles($expectedFiles);
} }
public function testSkipExcludes()
{
$excludes = array(
'prefixB.foo',
);
$this->finder = new ArchivableFilesFinder($this->sources, $excludes, true);
$this->assertArchivableFiles(array(
'/!important!.txt',
'/!important_too!.txt',
'/#weirdfile',
'/A/prefixA.foo',
'/A/prefixB.foo',
'/A/prefixC.foo',
'/A/prefixD.foo',
'/A/prefixE.foo',
'/A/prefixF.foo',
'/B/sub/prefixA.foo',
'/B/sub/prefixB.foo',
'/B/sub/prefixC.foo',
'/B/sub/prefixD.foo',
'/B/sub/prefixE.foo',
'/B/sub/prefixF.foo',
'/C/prefixA.foo',
'/C/prefixB.foo',
'/C/prefixC.foo',
'/C/prefixD.foo',
'/C/prefixE.foo',
'/C/prefixF.foo',
'/D/prefixA',
'/D/prefixB',
'/D/prefixC',
'/D/prefixD',
'/D/prefixE',
'/D/prefixF',
'/E/subtestA.foo',
'/F/subtestA.foo',
'/G/subtestA.foo',
'/H/subtestA.foo',
'/I/J/subtestA.foo',
'/K/dirJ/subtestA.foo',
'/parameters.yml',
'/parameters.yml.dist',
'/prefixA.foo',
'/prefixB.foo',
'/prefixC.foo',
'/prefixD.foo',
'/prefixE.foo',
'/prefixF.foo',
'/toplevelA.foo',
'/toplevelB.foo',
));
}
protected function getArchivableFiles() protected function getArchivableFiles()
{ {
$files = array(); $files = array();