@ -60,7 +60,8 @@ class InitCommand extends Command
new InputOption('author', null, InputOption::VALUE_NONE, 'Author name of package'),
new InputOption('author', null, InputOption::VALUE_NONE, 'Author name of package'),
// new InputOption('version', null, InputOption::VALUE_NONE, 'Version of package'),
// new InputOption('version', null, InputOption::VALUE_NONE, 'Version of package'),
new InputOption('homepage', null, InputOption::VALUE_NONE, 'Homepage of package'),
new InputOption('homepage', null, InputOption::VALUE_NONE, 'Homepage of package'),
new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'An array required packages'),
new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"'),
new InputOption('require-dev', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"'),
))
))
->setHelp(<<<EOT
->setHelp(<<<EOT
The <info>init</info> command creates a basic composer.json file
The <info>init</info> command creates a basic composer.json file
@ -216,10 +217,15 @@ EOT
));
));
$requirements = array();
$requirements = array();
if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dependencies interactively', 'yes', '?'), true)) {
if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dependencies (require) interactively', 'yes', '?'), true)) {
if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dev dependencies (require-dev) interactively', 'yes', '?'), true)) {
$package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add, or a <package><version> couple if it is not listed', false, ':'), $validator, 3);
$package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add, or a "[package] [version]" couple if it is not listed', false, ':'), $validator, 3);
@ -16,12 +16,15 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Factory;
use Composer\Json\JsonFile;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Json\JsonValidationException;
use Composer\Json\JsonValidationException;
use Composer\Util\RemoteFilesystem;
use Composer\Util\RemoteFilesystem;
/**
/**
* @author Jérémy Romey <jeremy@free-agent.fr>
* @author Jérémy Romey <jeremy@free-agent.fr>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
*/
class RequireCommand extends InitCommand
class RequireCommand extends InitCommand
{
{
@ -31,11 +34,11 @@ class RequireCommand extends InitCommand
->setName('require')
->setName('require')
->setDescription('Adds a required package to a composer.json')
->setDescription('Adds a required package to a composer.json')
->setDefinition(array(
->setDefinition(array(
new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json'),
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Required package with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"'),
new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'An array of required packages'),
new InputOption('dev', null, InputOption::VALUE_NONE, 'Add requirement to require-dev.'),
))
))
->setHelp(<<<EOT
->setHelp(<<<EOT
The add package command adds a required package to a given composer.json
The require command adds requirements to your composer.json
EOT
EOT
)
)
@ -44,7 +47,8 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output)
{
{
$file = $input->getArgument('file');
$factory = new Factory;
$file = $factory->getComposerFile();
if (!file_exists($file)) {
if (!file_exists($file)) {
$output->writeln('<error>'.$file.' not found.</error>');
$output->writeln('<error>'.$file.' not found.</error>');
@ -55,52 +59,44 @@ EOT
return 1;
return 1;
}
}
$laxValid = false;
try {
$json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
$json->read();
$json->validateSchema(JsonFile::LAX_SCHEMA);
$laxValid = true;
$json->validateSchema();
} catch (\Exception $e) {
$output->writeln('<error>'.$file.' has an error. Run the validate command for more info</error>');
if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey)) {
foreach ($requirements as $package => $version) {
foreach ($requirements as $package => $version) {
if (array_key_exists($package, $baseRequirements)) {
if ($dialog->askConfirmation($output, $dialog->getQuestion('The package '.$package.' is already in requirements. Would you like to update the version required from '.$baseRequirements[$package].' to '.$version, 'yes', '?'), true)) {
$baseRequirements[$package] = $version;
$baseRequirements[$package] = $version;
}
}
} else {
$baseRequirements[$package] = $version;
$composer[$requireKey] = $baseRequirements;
$json->write($composer);
}
}
$output->writeln('<info>'.$file.' has been updated</info>');
}
}
$options['require'] = $baseRequirements;
private function updateFileCleanly($json, array $base, array $new, $requireKey)
{
$contents = file_get_contents($json->getPath());
$json->encode($options);
$manipulator = new JsonManipulator($contents);
$json->write($options);
$output->writeln('<info>'.$file.' has been updated</info>');
foreach ($new as $package => $constraint) {
if (!$manipulator->addLink($requireKey, $package, $constraint)) {