1
0
Fork 0

Add --stability and support for package:version and package=version to create-project, fixes #957

pull/1361/merge
Jordi Boggiano 2012-11-24 15:34:50 +01:00
parent 49238388e4
commit dee9bcb9f1
3 changed files with 86 additions and 35 deletions

View File

@ -17,6 +17,9 @@ use Composer\Factory;
use Composer\Installer; use Composer\Installer;
use Composer\Installer\ProjectInstaller; use Composer\Installer\ProjectInstaller;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Package\BasePackage;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\DependencyResolver\Pool;
use Composer\Repository\ComposerRepository; use Composer\Repository\ComposerRepository;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\FilesystemRepository; use Composer\Repository\FilesystemRepository;
@ -36,6 +39,7 @@ use Composer\Package\Version\VersionParser;
* Install a package as new project into new directory. * Install a package as new project into new directory.
* *
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class CreateProjectCommand extends Command class CreateProjectCommand extends Command
{ {
@ -48,6 +52,7 @@ class CreateProjectCommand extends Command
new InputArgument('package', InputArgument::REQUIRED, 'Package name to be installed'), new InputArgument('package', InputArgument::REQUIRED, 'Package name to be installed'),
new InputArgument('directory', InputArgument::OPTIONAL, 'Directory where the files should be created'), new InputArgument('directory', InputArgument::OPTIONAL, 'Directory where the files should be created'),
new InputArgument('version', InputArgument::OPTIONAL, 'Version, will defaults to latest'), new InputArgument('version', InputArgument::OPTIONAL, 'Version, will defaults to latest'),
new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum-stability allowed (unless a version is specified).', 'stable'),
new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'),
new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist even for dev versions.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist even for dev versions.'),
new InputOption('repository-url', null, InputOption::VALUE_REQUIRED, 'Pick a different repository url to look for the package.'), new InputOption('repository-url', null, InputOption::VALUE_REQUIRED, 'Pick a different repository url to look for the package.'),
@ -64,6 +69,11 @@ for developers of your project.
<info>php composer.phar create-project vendor/project target-directory [version]</info> <info>php composer.phar create-project vendor/project target-directory [version]</info>
You can also specify the version with the package name using = or : as separator.
To install unstable packages, either specify the version you want, or use the
--stability=dev (where dev can be one of RC, beta, alpha or dev).
To setup a developer workable version you should create the project using the source To setup a developer workable version you should create the project using the source
controlled code by appending the <info>'--prefer-source'</info> flag. Also, it is controlled code by appending the <info>'--prefer-source'</info> flag. Also, it is
advisable to install all dependencies required for development by appending the advisable to install all dependencies required for development by appending the
@ -84,6 +94,7 @@ EOT
$input->getArgument('package'), $input->getArgument('package'),
$input->getArgument('directory'), $input->getArgument('directory'),
$input->getArgument('version'), $input->getArgument('version'),
$input->getOption('stability'),
$input->getOption('prefer-source'), $input->getOption('prefer-source'),
$input->getOption('prefer-dist'), $input->getOption('prefer-dist'),
$input->getOption('dev'), $input->getOption('dev'),
@ -94,10 +105,18 @@ EOT
); );
} }
public function installProject(IOInterface $io, $packageName, $directory = null, $packageVersion = null, $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false, $keepVcs = false) public function installProject(IOInterface $io, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false, $keepVcs = false)
{ {
$config = Factory::createConfig(); $config = Factory::createConfig();
$stability = strtolower($stability);
if ($stability === 'rc') {
$stability = 'RC';
}
if (!isset(BasePackage::$stabilities[$stability])) {
throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::$stabilities)));
}
$dm = $this->createDownloadManager($io, $config); $dm = $this->createDownloadManager($io, $config);
if ($preferSource) { if ($preferSource) {
$dm->setPreferSource(true); $dm->setPreferSource(true);
@ -113,33 +132,31 @@ EOT
throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url."); throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url.");
} }
$candidates = array();
$name = strtolower($packageName);
if ($packageVersion === null) {
$sourceRepo->filterPackages(function ($package) use (&$candidates, $name) {
if ($package->getName() === $name) {
$candidates[] = $package;
}
});
} else {
$parser = new VersionParser(); $parser = new VersionParser();
$version = $parser->normalize($packageVersion); $candidates = array();
$sourceRepo->filterPackages(function ($package) use (&$candidates, $name, $version) { $requirements = $parser->parseNameVersionPairs(array($packageName));
if ($package->getName() === $name && $version === $package->getVersion()) { $name = strtolower($requirements[0]['name']);
$candidates[] = $package; if (!$packageVersion && isset($requirements[0]['version'])) {
$packageVersion = $requirements[0]['version'];
return false; }
$pool = new Pool($packageVersion ? 'dev' : $stability);
$pool->addRepository($sourceRepo);
$constraint = $packageVersion ? new VersionConstraint('=', $parser->normalize($packageVersion)) : null;
$candidates = $pool->whatProvides($name, $constraint);
foreach ($candidates as $key => $candidate) {
if ($candidate->getName() !== $name) {
unset($candidates[$key]);
} }
});
} }
if (!$candidates) { if (!$candidates) {
throw new \InvalidArgumentException("Could not find package $packageName" . ($packageVersion ? " with version $packageVersion." : '')); throw new \InvalidArgumentException("Could not find package $name" . ($packageVersion ? " with version $packageVersion." : " with stability $stability."));
} }
if (null === $directory) { if (null === $directory) {
$parts = explode("/", $packageName, 2); $parts = explode("/", $name, 2);
$directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts); $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
} }

View File

@ -17,6 +17,7 @@ use Composer\Factory;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository; use Composer\Repository\PlatformRepository;
use Composer\Package\Version\VersionParser;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -295,20 +296,24 @@ EOT
$prompt = $dialog->getQuestion('Search for a package', false, ':'); $prompt = $dialog->getQuestion('Search for a package', false, ':');
if ($requires) { if ($requires) {
$requires = $this->normalizeRequirements($requires);
$result = array();
foreach ($requires as $key => $requirement) { foreach ($requires as $key => $requirement) {
$requires[$key] = $this->normalizeRequirement($requirement); if (!isset($requirement['version']) && $input->isInteractive()) {
if (false === strpos($requires[$key], ' ') && $input->isInteractive()) { $question = $dialog->getQuestion('Please provide a version constraint for the '.$requirement['name'].' requirement');
$question = $dialog->getQuestion('Please provide a version constraint for the '.$requirement.' requirement');
if ($constraint = $dialog->ask($output, $question)) { if ($constraint = $dialog->ask($output, $question)) {
$requires[$key] .= ' ' . $constraint; $requirement['version'] = $constraint;
} }
} }
if (false === strpos($requires[$key], ' ')) { if (!isset($requirement['version'])) {
throw new \InvalidArgumentException('The requirement '.$requirement.' must contain a version constraint'); throw new \InvalidArgumentException('The requirement '.$requirement['name'].' must contain a version constraint');
}
} }
return $requires; $result[] = $requirement['name'] . ' ' . $requirement['version'];
}
return $result;
} }
while (null !== $package = $dialog->ask($output, $prompt)) { while (null !== $package = $dialog->ask($output, $prompt)) {
@ -364,21 +369,14 @@ EOT
protected function formatRequirements(array $requirements) protected function formatRequirements(array $requirements)
{ {
$requires = array(); $requires = array();
$requirements = $this->normalizeRequirements($requirements);
foreach ($requirements as $requirement) { foreach ($requirements as $requirement) {
$requirement = $this->normalizeRequirement($requirement); $requires[$requirement['name']] = $requirement['version'];
list($packageName, $packageVersion) = explode(" ", $requirement, 2);
$requires[$packageName] = $packageVersion;
} }
return $requires; return $requires;
} }
protected function normalizeRequirement($requirement)
{
return preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', $requirement);
}
protected function getGitConfig() protected function getGitConfig()
{ {
if (null !== $this->gitConfig) { if (null !== $this->gitConfig) {
@ -441,6 +439,13 @@ EOT
return false; return false;
} }
protected function normalizeRequirements(array $requirements)
{
$parser = new VersionParser();
return $parser->parseNameVersionPairs($requirements);
}
protected function addVendorIgnore($ignoreFile, $vendor = 'vendor') protected function addVendorIgnore($ignoreFile, $vendor = 'vendor')
{ {
$contents = ""; $contents = "";

View File

@ -320,4 +320,33 @@ class VersionParser
throw new \UnexpectedValueException('Could not parse version constraint '.$constraint); throw new \UnexpectedValueException('Could not parse version constraint '.$constraint);
} }
/**
* Parses a name/version pairs and returns an array of pairs + the
*
* @param array $pairs a set of package/version pairs separated by ":", "=" or " "
* @return array[] array of arrays containing a name and (if provided) a version
*/
public function parseNameVersionPairs(array $pairs)
{
$pairs = array_values($pairs);
$result = array();
for ($i = 0; $i < count($pairs); $i++) {
$pair = preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', trim($pairs[$i]));
if (false === strpos($pair, ' ') && isset($pairs[$i+1]) && false === strpos($pairs[$i+1], '/')) {
$pair .= ' '.$pairs[$i+1];
$i++;
}
if (strpos($pair, ' ')) {
list($name, $version) = explode(" ", $pair, 2);
$result[] = array('name' => $name, 'version' => $version);
} else {
$result[] = array('name' => $pair);
}
}
return $result;
}
} }