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\Filesystem\Filesystem;
14 use Symfony\Component\Process\ProcessBuilder;
15 use Symfony\Component\Finder\Finder;
16 use Drupal\Console\Core\Utils\ConfigurationManager;
17 use Drupal\Console\Core\Generator\InitGenerator;
18 use Drupal\Console\Core\Utils\ShowFile;
23 * @package Drupal\Console\Core\Command
25 class InitCommand extends Command
33 * @var ConfigurationManager
35 protected $configurationManager;
45 protected $consoleRoot;
52 private $configParameters = [
58 'generate_inline' => false,
59 'generate_chain' => false
62 private $directories = [
68 * InitCommand constructor.
70 * @param ShowFile $showFile
71 * @param ConfigurationManager $configurationManager
72 * @param InitGenerator $generator
73 * @param string $appRoot
74 * @param string $consoleRoot
76 public function __construct(
78 ConfigurationManager $configurationManager,
79 InitGenerator $generator,
83 $this->showFile = $showFile;
84 $this->configurationManager = $configurationManager;
85 $this->generator = $generator;
86 $this->appRoot = $appRoot;
87 $this->consoleRoot = $consoleRoot;
88 parent::__construct();
94 protected function configure()
98 ->setDescription($this->trans('commands.init.description'))
102 InputOption::VALUE_OPTIONAL,
103 $this->trans('commands.init.options.destination')
108 InputOption::VALUE_NONE,
109 $this->trans('commands.init.options.override')
114 InputOption::VALUE_NONE,
115 $this->trans('commands.init.options.autocomplete')
122 protected function interact(InputInterface $input, OutputInterface $output)
124 $destination = $input->getOption('destination');
125 $autocomplete = $input->getOption('autocomplete');
126 $configuration = $this->configurationManager->getConfiguration();
129 if ($this->appRoot && $this->consoleRoot) {
130 $destination = $this->getIo()->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'] = $this->getIo()->choiceNoList(
143 $this->trans('commands.init.questions.language'),
144 array_keys($configuration->get('application.languages'))
147 $this->configParameters['temp'] = $this->getIo()->ask(
148 $this->trans('commands.init.questions.temp'),
152 $this->configParameters['chain'] = $this->getIo()->confirm(
153 $this->trans('commands.init.questions.chain'),
157 $this->configParameters['sites'] = $this->getIo()->confirm(
158 $this->trans('commands.init.questions.sites'),
162 $this->configParameters['learning'] = $this->getIo()->confirm(
163 $this->trans('commands.init.questions.learning'),
167 $this->configParameters['generate_inline'] = $this->getIo()->confirm(
168 $this->trans('commands.init.questions.generate-inline'),
172 $this->configParameters['generate_chain'] = $this->getIo()->confirm(
173 $this->trans('commands.init.questions.generate-chain'),
177 if (!$autocomplete) {
178 $autocomplete = $this->getIo()->confirm(
179 $this->trans('commands.init.questions.autocomplete'),
182 $input->setOption('autocomplete', $autocomplete);
189 protected function execute(InputInterface $input, OutputInterface $output)
192 $destination = $input->getOption('destination');
193 $autocomplete = $input->getOption('autocomplete');
194 $override = $input->getOption('override');
196 $destination = $this->configurationManager->getConsoleDirectory();
199 $finder = new Finder();
203 $this->configurationManager->getVendorCoreRoot()
206 if (!$this->configParameters['chain']) {
207 $finder->exclude('chain');
209 if (!$this->configParameters['sites']) {
210 $finder->exclude('sites');
214 foreach ($finder as $configFile) {
215 $sourceFile = sprintf(
217 $this->configurationManager->getVendorCoreRoot(),
218 $configFile->getRelativePathname()
221 $destinationFile = sprintf(
224 $configFile->getRelativePathname()
227 $fs = new Filesystem();
228 foreach ($this->directories as $directory) {
229 if (!$fs->exists($destination.$directory)) {
230 $fs->mkdir($destination.$directory);
234 if ($this->copyFile($sourceFile, $destinationFile, $override)) {
235 $copiedFiles[] = $destinationFile;
240 $this->showFile->copiedFiles($this->getIo(), $copiedFiles, false);
241 $this->getIo()->newLine();
244 $executableName = null;
246 $processBuilder = new ProcessBuilder(['bash']);
247 $process = $processBuilder->getProcess();
248 $process->setCommandLine('echo $_');
250 $fullPathExecutable = explode('/', $process->getOutput());
251 $executableName = trim(end($fullPathExecutable));
255 $this->generator->generate([
256 'user_home' => $this->configurationManager->getConsoleDirectory(),
257 'executable_name' => $executableName,
258 'override' => $override,
259 'destination' => $destination,
260 'config_parameters' => $this->configParameters,
263 $this->getIo()->writeln($this->trans('application.messages.autocomplete'));
269 * @param string $source
270 * @param string $destination
271 * @param string $override
274 private function copyFile($source, $destination, $override)
276 if (file_exists($destination)) {
280 $destination . '.old'
287 $filePath = dirname($destination);
288 if (!is_dir($filePath)) {
289 mkdir($filePath, 0777, true);