From dee9bcb9f157a1bce1ff2f4f0f9812d5fda0cc2f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 24 Nov 2012 15:34:50 +0100 Subject: [PATCH] Add --stability and support for package:version and package=version to create-project, fixes #957 --- src/Composer/Command/CreateProjectCommand.php | 55 ++++++++++++------- src/Composer/Command/InitCommand.php | 37 +++++++------ .../Package/Version/VersionParser.php | 29 ++++++++++ 3 files changed, 86 insertions(+), 35 deletions(-) diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index a51abd5da..5cc8d6fb7 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -17,6 +17,9 @@ use Composer\Factory; use Composer\Installer; use Composer\Installer\ProjectInstaller; use Composer\IO\IOInterface; +use Composer\Package\BasePackage; +use Composer\Package\LinkConstraint\VersionConstraint; +use Composer\DependencyResolver\Pool; use Composer\Repository\ComposerRepository; use Composer\Repository\CompositeRepository; use Composer\Repository\FilesystemRepository; @@ -36,6 +39,7 @@ use Composer\Package\Version\VersionParser; * Install a package as new project into new directory. * * @author Benjamin Eberlei + * @author Jordi Boggiano */ class CreateProjectCommand extends Command { @@ -48,6 +52,7 @@ class CreateProjectCommand extends Command new InputArgument('package', InputArgument::REQUIRED, 'Package name to be installed'), new InputArgument('directory', InputArgument::OPTIONAL, 'Directory where the files should be created'), 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-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.'), @@ -64,6 +69,11 @@ for developers of your project. php composer.phar create-project vendor/project target-directory [version] +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 controlled code by appending the '--prefer-source' flag. Also, it is advisable to install all dependencies required for development by appending the @@ -84,6 +94,7 @@ EOT $input->getArgument('package'), $input->getArgument('directory'), $input->getArgument('version'), + $input->getOption('stability'), $input->getOption('prefer-source'), $input->getOption('prefer-dist'), $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(); + $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); if ($preferSource) { $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."); } + $parser = new VersionParser(); $candidates = array(); - $name = strtolower($packageName); + $requirements = $parser->parseNameVersionPairs(array($packageName)); + $name = strtolower($requirements[0]['name']); + if (!$packageVersion && isset($requirements[0]['version'])) { + $packageVersion = $requirements[0]['version']; + } - if ($packageVersion === null) { - $sourceRepo->filterPackages(function ($package) use (&$candidates, $name) { - if ($package->getName() === $name) { - $candidates[] = $package; - } - }); - } else { - $parser = new VersionParser(); - $version = $parser->normalize($packageVersion); - $sourceRepo->filterPackages(function ($package) use (&$candidates, $name, $version) { - if ($package->getName() === $name && $version === $package->getVersion()) { - $candidates[] = $package; + $pool = new Pool($packageVersion ? 'dev' : $stability); + $pool->addRepository($sourceRepo); - return false; - } - }); + $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) { - 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) { - $parts = explode("/", $packageName, 2); + $parts = explode("/", $name, 2); $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts); } diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index f21be1937..85dd21ac2 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -17,6 +17,7 @@ use Composer\Factory; use Composer\Package\BasePackage; use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; +use Composer\Package\Version\VersionParser; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -295,20 +296,24 @@ EOT $prompt = $dialog->getQuestion('Search for a package', false, ':'); if ($requires) { + $requires = $this->normalizeRequirements($requires); + $result = array(); + foreach ($requires as $key => $requirement) { - $requires[$key] = $this->normalizeRequirement($requirement); - if (false === strpos($requires[$key], ' ') && $input->isInteractive()) { - $question = $dialog->getQuestion('Please provide a version constraint for the '.$requirement.' requirement'); + if (!isset($requirement['version']) && $input->isInteractive()) { + $question = $dialog->getQuestion('Please provide a version constraint for the '.$requirement['name'].' requirement'); if ($constraint = $dialog->ask($output, $question)) { - $requires[$key] .= ' ' . $constraint; + $requirement['version'] = $constraint; } } - if (false === strpos($requires[$key], ' ')) { - throw new \InvalidArgumentException('The requirement '.$requirement.' must contain a version constraint'); + if (!isset($requirement['version'])) { + throw new \InvalidArgumentException('The requirement '.$requirement['name'].' must contain a version constraint'); } + + $result[] = $requirement['name'] . ' ' . $requirement['version']; } - return $requires; + return $result; } while (null !== $package = $dialog->ask($output, $prompt)) { @@ -364,21 +369,14 @@ EOT protected function formatRequirements(array $requirements) { $requires = array(); + $requirements = $this->normalizeRequirements($requirements); foreach ($requirements as $requirement) { - $requirement = $this->normalizeRequirement($requirement); - list($packageName, $packageVersion) = explode(" ", $requirement, 2); - - $requires[$packageName] = $packageVersion; + $requires[$requirement['name']] = $requirement['version']; } return $requires; } - protected function normalizeRequirement($requirement) - { - return preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', $requirement); - } - protected function getGitConfig() { if (null !== $this->gitConfig) { @@ -441,6 +439,13 @@ EOT return false; } + protected function normalizeRequirements(array $requirements) + { + $parser = new VersionParser(); + + return $parser->parseNameVersionPairs($requirements); + } + protected function addVendorIgnore($ignoreFile, $vendor = 'vendor') { $contents = ""; diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index e7aae0749..dbafae4af 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -320,4 +320,33 @@ class VersionParser 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; + } }