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;
25 * @package Drupal\Console\Core\Command
27 class InitCommand extends Command
37 * @var ConfigurationManager
39 protected $configurationManager;
49 protected $consoleRoot;
56 private $configParameters = [
62 'generate_inline' => false,
63 'generate_chain' => false
67 * InitCommand constructor.
69 * @param ShowFile $showFile
70 * @param ConfigurationManager $configurationManager
71 * @param InitGenerator $generator
72 * @param string $appRoot
73 * @param string $consoleRoot
75 public function __construct(
77 ConfigurationManager $configurationManager,
78 InitGenerator $generator,
82 $this->showFile = $showFile;
83 $this->configurationManager = $configurationManager;
84 $this->generator = $generator;
85 $this->appRoot = $appRoot;
86 $this->consoleRoot = $consoleRoot;
87 parent::__construct();
93 protected function configure()
97 ->setDescription($this->trans('commands.init.description'))
101 InputOption::VALUE_OPTIONAL,
102 $this->trans('commands.init.options.destination')
107 InputOption::VALUE_NONE,
108 $this->trans('commands.init.options.override')
113 InputOption::VALUE_NONE,
114 $this->trans('commands.init.options.autocomplete')
121 protected function interact(InputInterface $input, OutputInterface $output)
123 $io = new DrupalStyle($input, $output);
124 $destination = $input->getOption('destination');
125 $autocomplete = $input->getOption('autocomplete');
126 $configuration = $this->configurationManager->getConfiguration();
129 if ($this->appRoot && $this->consoleRoot) {
130 $destination = $io->choice(
131 $this->trans('commands.init.questions.destination'),
132 $this->configurationManager->getConfigurationDirectories()
135 $destination = $this->configurationManager
136 ->getConsoleDirectory();
139 $input->setOption('destination', $destination);
142 $this->configParameters['language'] = $io->choiceNoList(
143 $this->trans('commands.init.questions.language'),
144 array_keys($configuration->get('application.languages'))
147 $this->configParameters['temp'] = $io->ask(
148 $this->trans('commands.init.questions.temp'),
152 $this->configParameters['learning'] = $io->confirm(
153 $this->trans('commands.init.questions.chain'),
157 $this->configParameters['sites'] = $io->confirm(
158 $this->trans('commands.init.questions.sites'),
162 $this->configParameters['chain'] = $io->confirm(
163 $this->trans('commands.init.questions.learning'),
167 $this->configParameters['generate_inline'] = $io->confirm(
168 $this->trans('commands.init.questions.generate-inline'),
172 $this->configParameters['generate_chain'] = $io->confirm(
173 $this->trans('commands.init.questions.generate-chain'),
177 if (!$autocomplete) {
178 $autocomplete = $io->confirm(
179 $this->trans('commands.init.questions.autocomplete'),
182 $input->setOption('autocomplete', $autocomplete);
189 protected function execute(InputInterface $input, OutputInterface $output)
191 $io = new DrupalStyle($input, $output);
193 $destination = $input->getOption('destination');
194 $autocomplete = $input->getOption('autocomplete');
195 $override = $input->getOption('override');
197 $destination = $this->configurationManager->getConsoleDirectory();
200 $finder = new Finder();
204 $this->configurationManager->getApplicationDirectory(),
208 if (!$this->configParameters['chain']) {
209 $finder->exclude('chain');
211 if (!$this->configParameters['sites']) {
212 $finder->exclude('sites');
216 foreach ($finder as $configFile) {
217 $sourceFile = sprintf(
218 '%s%s/config/dist/%s',
219 $this->configurationManager->getApplicationDirectory(),
221 $configFile->getRelativePathname()
224 $destinationFile = sprintf(
227 $configFile->getRelativePathname()
230 if ($this->copyFile($sourceFile, $destinationFile, $override)) {
231 $copiedFiles[] = $destinationFile;
236 $this->showFile->copiedFiles($io, $copiedFiles, false);
240 $executableName = null;
242 $processBuilder = new ProcessBuilder(['bash']);
243 $process = $processBuilder->getProcess();
244 $process->setCommandLine('echo $_');
246 $fullPathExecutable = explode('/', $process->getOutput());
247 $executableName = trim(end($fullPathExecutable));
251 $this->generator->generate(
252 $this->configurationManager->getConsoleDirectory(),
256 $this->configParameters
259 $io->writeln($this->trans('application.messages.autocomplete'));
265 * @param string $source
266 * @param string $destination
267 * @param string $override
270 private function copyFile($source, $destination, $override)
272 if (file_exists($destination)) {
276 $destination . '.old'
283 $filePath = dirname($destination);
284 if (!is_dir($filePath)) {
285 mkdir($filePath, 0777, true);