From 6386921f99f08189360faa288548924c14d6ceb1 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Tue, 2 Oct 2012 10:42:48 -0500 Subject: [PATCH] deleted the repository, added a config command that current will just list the config for either file and open vim to edit the file --- src/Composer/Command/ConfigCommand.php | 133 ++++++++++++++++++ .../Command/ConfigRepositoriesAddCommand.php | 129 ----------------- src/Composer/Console/Application.php | 2 +- 3 files changed, 134 insertions(+), 130 deletions(-) create mode 100644 src/Composer/Command/ConfigCommand.php delete mode 100644 src/Composer/Command/ConfigRepositoriesAddCommand.php diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php new file mode 100644 index 000000000..f8f2f1292 --- /dev/null +++ b/src/Composer/Command/ConfigCommand.php @@ -0,0 +1,133 @@ + + * 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 Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Composer\Config; +use Composer\Factory; +use Composer\Json\JsonFile; + +class ConfigCommand extends Command +{ + /** + * @var array + */ + protected $repositories = array(); + + /** + * @var Composer\Json\JsonFile + */ + protected $configFile; + + /** + * {@inheritDoc} + */ + protected function configure() + { + $this + ->setName('config') + ->setDescription('Set config options') + ->setDefinition(array( + new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.'), + new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'), + new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'), + // @todo insert argument here + )) + ->setHelp(<<configFile = $input->getOption('global') + ? (Factory::createConfig()->get('home') . '/config.json') + : 'composer.json'; + + $this->configFile = new JsonFile($this->configFile); + if (!$this->configFile->exists()) { + touch($this->configFile->getPath()); + // If you read an empty file, Composer throws an error + // Toss some of the defaults in there + $defaults = Config::$defaultConfig; + $defaults['repositories'] = Config::$defaultRepositories; + $this->configFile->write($defaults); + } + } + + /** + * {@inheritDoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + // Open file in editor + if ($input->getOption('editor')) { + // @todo Find a way to use another editor + $editor = 'vim'; + system($editor . ' ' . $this->configFile->getPath() . ' > `tty`'); + return 0; + } + + // List the configuration of the file settings + if ($input->getOption('list')) { + $this->displayFileContents($this->configFile->read(), $output); + return 0; + } + } + + /** + * {@inheritDoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + } + + /** + * Display the contents of the file in a pretty formatted way + * + * @param array $contents + * @param OutputInterface $output + * @param integer $depth + * @param string|null $k + */ + protected function displayFileContents(array $contents, OutputInterface $output, &$depth = 0, $k = null) + { + // @todo Look into a way to refactor this code, as it is right now, I + // don't like it + foreach ($contents as $key => $value) { + if (is_array($value)) { + $depth++; + $k .= $key . '.'; + $this->displayFileContents($value, $output, $depth, $k); + if (substr_count($k,'.') > 1) { + $k = str_split($k,strrpos($k,'.',-2)); + $k = $k[0] . '.'; + } else { $k = null; } + $depth--; + continue; + } + $output->writeln('[' . $k . $key . '] ' . $value . ''); + } + } +} + + diff --git a/src/Composer/Command/ConfigRepositoriesAddCommand.php b/src/Composer/Command/ConfigRepositoriesAddCommand.php deleted file mode 100644 index eae0abd24..000000000 --- a/src/Composer/Command/ConfigRepositoriesAddCommand.php +++ /dev/null @@ -1,129 +0,0 @@ - - * 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 Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; -use Composer\Factory; -use Composer\Installer; -use Composer\Json\JsonFile; - -class ConfigRepositoriesAddCommand extends Command -{ - /** - * @var array - */ - protected $repositories = array(); - - /** - * {@inheritDoc} - */ - protected function configure() - { - // @todo Make it so the user can pass into this command an array - $this - ->setName('config:repositories:add') - ->setDescription('Add a repository') - ->setDefinition(array( - new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.') - )) - ->setHelp(<<getOption('global') - ? (Factory::createConfig()->get('home') . '/config.json') - : 'composer.json'; - - $configFile = new JsonFile($configFile); - if (!$configFile->exists()) { - touch($globalConfig->getPath()); - // If you read an empty file, Composer throws an error - $globalConfig->write(array()); - } - - // Make sure we have something to add - if (count($this->repositories)) { - // @todo Check and make sure the type/url combo does not - // alredy exist. - $config = $configFile->read(); - foreach ($this->repositories as $repo) { - $config['repositories'][] = array( - 'type' => $repo['type'], - 'url' => $repo['url'], - ); - } - - if ($input->isInteractive()) { - $output->writeln(array( - '', - JsonFile::encode($config), - '', - )); - $dialog = $this->getHelperSet()->get('dialog'); - if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you want to continuw and save the repositories', 'yes', '?'), true)) { - $output->writeln('Command aborted by the user.'); - return 1; - } - } - $configFile->write($config); - } else { - $output->writeln('No repositories have been added.'); - } - } - - /** - * {@inheritDoc} - */ - protected function interact(InputInterface $input, OutputInterface $output) - { - $dialog = $this->getHelperSet()->get('dialog'); - - $output->writeln(array( - '', - 'With this command you can add as many repositories to either the', - 'local composer.json file or the global composer config file.', - '', - 'Type can be any of the following: composer, vcs, pear, package', - '', - 'For more information see docs: http://getcomposer.org/doc/05-repositories.md', - '', - )); - - do { - $type = $dialog->ask($output, $dialog->getQuestion('Repository Type')); - $repo = $dialog->ask($output, $dialog->getQuestion('Repository URL')); - if (null === $type && null === $repo) { - break; - } - $this->repositories[] = array( - 'type' => $type, - 'url' => $repo, - ); - } while(true); - } -} - diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index aee57a865..856ea5583 100755 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -155,7 +155,7 @@ class Application extends BaseApplication { $commands = parent::getDefaultCommands(); $commands[] = new Command\AboutCommand(); - $commands[] = new Command\ConfigRepositoriesAddCommand(); + $commands[] = new Command\ConfigCommand(); $commands[] = new Command\DependsCommand(); $commands[] = new Command\InitCommand(); $commands[] = new Command\InstallCommand();