From 42e88ac27afd15293c6a7bbfe8406364c32ade03 Mon Sep 17 00:00:00 2001 From: Shalvah Date: Mon, 26 Nov 2018 12:57:38 +0100 Subject: [PATCH] Add interactive option to install dependencies after running init command (#7521) * Add interactive option to install dependencies after running init command * Only ask to install dependencies when dependencies where defined --- src/Composer/Command/InitCommand.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 760826d22..515a16943 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -22,6 +22,7 @@ use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; use Composer\Repository\RepositoryFactory; use Composer\Util\ProcessExecutor; +use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -145,6 +146,11 @@ EOT } } } + + $question = 'Would you like to install dependencies now [yes]? '; + if ($input->isInteractive() && $this->hasDependencies($options) && $io->askConfirmation($question, true)) { + $this->installDependencies($output); + } } /** @@ -767,4 +773,23 @@ EOT return array_keys(array_slice($similarPackages, 0, 5)); } + + private function installDependencies($output) + { + try { + $installCommand = $this->getApplication()->find('install'); + $installCommand->run(new ArrayInput(array()), $output); + } catch (\Exception $e) { + $this->getIO()->writeError('Could not install dependencies. Run `composer install` to see more information.'); + } + + } + + private function hasDependencies($options) + { + $requires = (array) $options['require']; + $devRequires = isset($options['require-dev']) ? (array) $options['require-dev'] : array(); + + return !empty($requires) || !empty($devRequires); + } }