1
0
Fork 0

Allow archiving the current project with composer archive

pull/1567/head
Nils Adermann 2013-02-07 17:45:03 +01:00
parent afcdad4b23
commit ba375b6867
2 changed files with 22 additions and 12 deletions

View File

@ -36,7 +36,7 @@ class ArchiveCommand extends Command
->setName('archive')
->setDescription('Create an archive of this composer package')
->setDefinition(array(
new InputArgument('package', InputArgument::REQUIRED, 'The package to archive'),
new InputArgument('package', InputArgument::OPTIONAL, 'The package to archive instead of the current project'),
new InputArgument('version', InputArgument::OPTIONAL, 'The package version to archive'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the resulting archive: tar or zip', 'tar'),
new InputOption('dir', false, InputOption::VALUE_REQUIRED, 'Write the archive to this directory', '.'),
@ -44,9 +44,9 @@ class ArchiveCommand extends Command
->setHelp(<<<EOT
The <info>archive</info> command creates an archive of the specified format
containing the files and directories of the Composer project or the specified
package and writes it to the specified directory.
package in the specified version and writes it to the specified directory.
<info>php composer.phar archive [--format=zip] [--dir=/foo] package version</info>
<info>php composer.phar archive [--format=zip] [--dir=/foo] [package] [version]</info>
EOT
)
@ -64,17 +64,21 @@ EOT
);
}
public function archive(IOInterface $io, $packageName, $version = false, $format = 'tar', $dest = '.')
public function archive(IOInterface $io, $packageName = false, $version = false, $format = 'tar', $dest = '.')
{
$config = Factory::createConfig();
$factory = new Factory;
$archiveManager = $factory->createArchiveManager($config);
if ($packageName) {
$package = $this->selectPackage($io, $packageName, $version);
if (!$package) {
return 1;
}
} else {
$package = $this->getComposer()->getPackage();
}
$io->write('<info>Creating the archive.</info>');
$archiveManager->archive($package, $format, $dest);

View File

@ -16,6 +16,7 @@ use Composer\Downloader\DownloadManager;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackage;
use Composer\Util\Filesystem;
/**
@ -73,19 +74,24 @@ class ArchiveManager
throw new \RuntimeException(sprintf('No archiver found to support %s format', $format));
}
// Directory used to download the sources
$filesystem = new Filesystem();
$packageName = preg_replace('#[^a-z0-9-_.]#i', '-', $package->getPrettyString());
$sourcePath = sys_get_temp_dir().'/composer_archiver/'.$packageName;
$filesystem->ensureDirectoryExists($sourcePath);
// Archive filename
$filesystem->ensureDirectoryExists($targetDir);
$target = realpath($targetDir).'/'.$packageName.'.'.$format;
$filesystem->ensureDirectoryExists(dirname($target));
if ($package instanceof RootPackage) {
$sourcePath = realpath('.');
} else {
// Directory used to download the sources
$sourcePath = sys_get_temp_dir().'/composer_archiver/'.$packageName;
$filesystem->ensureDirectoryExists($sourcePath);
// Download sources
$this->downloadManager->download($package, $sourcePath, true);
}
// Create the archive
$sourceRef = $package->getSourceReference();