5 * Contains \Drupal\Console\Core\Command\InitCommand.
8 namespace Drupal\Console\Core\Command;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Process\ProcessBuilder;
14 use Symfony\Component\Finder\Finder;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Utils\ConfigurationManager;
18 use Drupal\Console\Core\Generator\InitGenerator;
19 use Drupal\Console\Core\Utils\ShowFile;
20 use Drupal\Console\Core\Style\DrupalStyle;
24 * @package Drupal\Console\Core\Command
26 class InitCommand extends Command
36 * @var ConfigurationManager
38 protected $configurationManager;
48 protected $consoleRoot;
55 private $configParameters = [
59 'generate_inline' => false,
60 'generate_chain' => false
64 * InitCommand constructor.
65 * @param ShowFile $showFile
66 * @param ConfigurationManager $configurationManager
67 * @param InitGenerator $generator
68 * @param string $appRoot
69 * @param string $consoleRoot
71 public function __construct(
73 ConfigurationManager $configurationManager,
74 InitGenerator $generator,
78 $this->showFile = $showFile;
79 $this->configurationManager = $configurationManager;
80 $this->generator = $generator;
81 $this->appRoot = $appRoot;
82 $this->consoleRoot = $consoleRoot;
83 parent::__construct();
89 protected function configure()
93 ->setDescription($this->trans('commands.init.description'))
97 InputOption::VALUE_OPTIONAL,
98 $this->trans('commands.init.options.destination')
103 InputOption::VALUE_NONE,
104 $this->trans('commands.init.options.override')
109 InputOption::VALUE_NONE,
110 $this->trans('commands.init.options.autocomplete')
117 protected function interact(InputInterface $input, OutputInterface $output)
119 $io = new DrupalStyle($input, $output);
120 $destination = $input->getOption('destination');
121 $autocomplete = $input->getOption('autocomplete');
122 $configuration = $this->configurationManager->getConfiguration();
125 if ($this->appRoot && $this->consoleRoot) {
126 $destination = $io->choice(
127 $this->trans('commands.init.questions.destination'),
128 $this->configurationManager->getConfigurationDirectories()
131 $destination = $this->configurationManager
132 ->getConsoleDirectory();
135 $input->setOption('destination', $destination);
138 $this->configParameters['language'] = $io->choiceNoList(
139 $this->trans('commands.init.questions.language'),
140 array_keys($configuration->get('application.languages'))
143 $this->configParameters['temp'] = $io->ask(
144 $this->trans('commands.init.questions.temp'),
148 $this->configParameters['learning'] = $io->confirm(
149 $this->trans('commands.init.questions.learning'),
153 $this->configParameters['generate_inline'] = $io->confirm(
154 $this->trans('commands.init.questions.generate-inline'),
158 $this->configParameters['generate_chain'] = $io->confirm(
159 $this->trans('commands.init.questions.generate-chain'),
163 if (!$autocomplete) {
164 $autocomplete = $io->confirm(
165 $this->trans('commands.init.questions.autocomplete'),
168 $input->setOption('autocomplete', $autocomplete);
175 protected function execute(InputInterface $input, OutputInterface $output)
177 $io = new DrupalStyle($input, $output);
179 $destination = $input->getOption('destination');
180 $autocomplete = $input->getOption('autocomplete');
181 $override = $input->getOption('override');
183 $destination = $this->configurationManager->getConsoleDirectory();
186 $finder = new Finder();
190 $this->configurationManager->getApplicationDirectory(),
196 foreach ($finder as $configFile) {
197 $sourceFile = sprintf(
198 '%s%s/config/dist/%s',
199 $this->configurationManager->getApplicationDirectory(),
201 $configFile->getRelativePathname()
204 $destinationFile = sprintf(
207 $configFile->getRelativePathname()
210 if ($this->copyFile($sourceFile, $destinationFile, $override)) {
211 $copiedFiles[] = $destinationFile;
216 $this->showFile->copiedFiles($io, $copiedFiles, false);
220 $executableName = null;
222 $processBuilder = new ProcessBuilder(array('bash'));
223 $process = $processBuilder->getProcess();
224 $process->setCommandLine('echo $_');
226 $fullPathExecutable = explode('/', $process->getOutput());
227 $executableName = trim(end($fullPathExecutable));
231 $this->generator->generate(
232 $this->configurationManager->getConsoleDirectory(),
236 $this->configParameters
239 $io->writeln($this->trans('application.messages.autocomplete'));
245 * @param string $source
246 * @param string $destination
247 * @param string $override
250 private function copyFile($source, $destination, $override)
252 if (file_exists($destination)) {
256 $destination . '.old'
263 $filePath = dirname($destination);
264 if (!is_dir($filePath)) {
265 mkdir($filePath, 0777, true);