diff --git a/src/Composer/Command/Helper/DialogHelper.php b/src/Composer/Command/Helper/DialogHelper.php new file mode 100644 index 000000000..a0c034116 --- /dev/null +++ b/src/Composer/Command/Helper/DialogHelper.php @@ -0,0 +1,23 @@ +writeln(array( + '', + $this->getHelperSet()->get('formatter')->formatBlock($text, $style, true), + '', + )); + } + + public function getQuestion($question, $default, $sep = ':') + { + return $default ? sprintf('%s [%s]%s ', $question, $default, $sep) : sprintf('%s%s ', $question, $sep); + } +} \ No newline at end of file diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php new file mode 100644 index 000000000..758778b6b --- /dev/null +++ b/src/Composer/Command/InitCommand.php @@ -0,0 +1,126 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Json\JsonFile; +use Composer\Command\Helper\DialogHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + + +if (!defined('JSON_PRETTY_PRINT')) { + define('JSON_PRETTY_PRINT', 128); +} + +/** + * @author Justin Rainbow + */ +class InitCommand extends Command +{ + protected function configure() + { + $this + ->setName('init') + ->setDescription('Creates a basic composer.json file in current directory.') + ->setDefinition(array( + new InputOption('name', null, InputOption::VALUE_NONE, 'Name of the package'), + new InputOption('description', null, InputOption::VALUE_NONE, 'Description of package'), + // new InputOption('version', null, InputOption::VALUE_NONE, 'Version of package'), + new InputOption('homepage', null, InputOption::VALUE_NONE, 'Homepage of package'), + )) + ->setHelp(<<install command reads the composer.json file from the +current directory, processes it, and downloads and installs all the +libraries and dependencies outlined in that file. + +php composer.phar install + +EOT + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $dialog = $this->getDialogHelper(); + + $options = array_filter(array_intersect_key($input->getOptions(), array_flip(array('name','description')))); + + $file = new JsonFile("composer.json"); + + $indentSize = 2; + $lines = array(); + + foreach ($options as $key => $value) { + $lines[] = sprintf('%s%s: %s', str_repeat(' ', $indentSize), json_encode($key), json_encode($value)); + } + + $json = "{\n" . implode(",\n", $lines) . "\n}\n"; + + if ($input->isInteractive()) { + $output->writeln(array( + '', + $json, + '' + )); + if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) { + $output->writeln('Command aborted'); + + return 1; + } + } + + $file->write($options); + } + + protected function interact(InputInterface $input, OutputInterface $output) + { + $dialog = $this->getDialogHelper(); + $dialog->writeSection($output, 'Welcome to the Composer config generator'); + + // namespace + $output->writeln(array( + '', + 'This command will guide you through creating your composer.json config.', + '', + )); + + $cwd = realpath("."); + + $name = $input->getOption('name') ?: basename($cwd); + $name = $dialog->ask( + $output, + $dialog->getQuestion('Package name', $name), + $name + ); + $input->setOption('name', $name); + + $description = $input->getOption('description') ?: false; + $description = $dialog->ask( + $output, + $dialog->getQuestion('Description', $description) + ); + $input->setOption('description', $description); + } + + protected function getDialogHelper() + { + $dialog = $this->getHelperSet()->get('dialog'); + if (!$dialog || get_class($dialog) !== 'Composer\Command\Helper\DialogHelper') { + $this->getHelperSet()->set($dialog = new DialogHelper()); + } + + return $dialog; + } +} \ No newline at end of file diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 6d1d6ba0b..3b3979770 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -104,6 +104,7 @@ class Application extends BaseApplication { $this->add(new Command\AboutCommand()); $this->add(new Command\DependsCommand()); + $this->add(new Command\InitCommand()); $this->add(new Command\InstallCommand()); $this->add(new Command\UpdateCommand()); $this->add(new Command\SearchCommand());