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.site')
114 InputOption::VALUE_NONE,
115 $this->trans('commands.init.options.override')
120 InputOption::VALUE_NONE,
121 $this->trans('commands.init.options.autocomplete')
128 protected function interact(InputInterface $input, OutputInterface $output)
130 $destination = $input->getOption('destination');
131 $site = $input->getOption('site');
132 $autocomplete = $input->getOption('autocomplete');
133 $configuration = $this->configurationManager->getConfiguration();
135 if ($site && $this->appRoot && $this->consoleRoot) {
136 $destination = $this->consoleRoot . '/console/';
140 if ($this->appRoot && $this->consoleRoot) {
141 $destination = $this->getIo()->choice(
142 $this->trans('commands.init.questions.destination'),
143 $this->configurationManager->getConfigurationDirectories()
146 $destination = $this->configurationManager
147 ->getConsoleDirectory();
150 $input->setOption('destination', $destination);
153 $this->configParameters['language'] = $this->getIo()->choiceNoList(
154 $this->trans('commands.init.questions.language'),
155 array_keys($configuration->get('application.languages'))
158 $this->configParameters['temp'] = $this->getIo()->ask(
159 $this->trans('commands.init.questions.temp'),
163 $this->configParameters['chain'] = $this->getIo()->confirm(
164 $this->trans('commands.init.questions.chain'),
168 $this->configParameters['sites'] = $this->getIo()->confirm(
169 $this->trans('commands.init.questions.sites'),
173 $this->configParameters['learning'] = $this->getIo()->confirm(
174 $this->trans('commands.init.questions.learning'),
178 $this->configParameters['generate_inline'] = $this->getIo()->confirm(
179 $this->trans('commands.init.questions.generate-inline'),
183 $this->configParameters['generate_chain'] = $this->getIo()->confirm(
184 $this->trans('commands.init.questions.generate-chain'),
188 if (!$autocomplete) {
189 $autocomplete = $this->getIo()->confirm(
190 $this->trans('commands.init.questions.autocomplete'),
193 $input->setOption('autocomplete', $autocomplete);
200 protected function execute(InputInterface $input, OutputInterface $output)
203 $destination = $input->getOption('destination');
204 $site = $input->getOption('site');
205 $autocomplete = $input->getOption('autocomplete');
206 $override = $input->getOption('override');
208 if ($site && $this->appRoot && $this->consoleRoot) {
209 $destination = $this->consoleRoot . '/console/';
213 $destination = $this->configurationManager->getConsoleDirectory();
216 $finder = new Finder();
220 $this->configurationManager->getVendorCoreRoot()
223 if (!$this->configParameters['chain']) {
224 $finder->exclude('chain');
226 if (!$this->configParameters['sites']) {
227 $finder->exclude('sites');
231 foreach ($finder as $configFile) {
232 $sourceFile = sprintf(
234 $this->configurationManager->getVendorCoreRoot(),
235 $configFile->getRelativePathname()
238 $destinationFile = sprintf(
241 $configFile->getRelativePathname()
244 $fs = new Filesystem();
245 foreach ($this->directories as $directory) {
246 if (!$fs->exists($destination.$directory)) {
247 $fs->mkdir($destination.$directory);
251 if ($this->copyFile($sourceFile, $destinationFile, $override)) {
252 $copiedFiles[] = $destinationFile;
257 $this->showFile->copiedFiles($this->getIo(), $copiedFiles, false);
258 $this->getIo()->newLine();
261 $executableName = null;
263 $processBuilder = new ProcessBuilder(['bash']);
264 $process = $processBuilder->getProcess();
265 $process->setCommandLine('echo $_');
267 $fullPathExecutable = explode('/', $process->getOutput());
268 $executableName = trim(end($fullPathExecutable));
272 $this->generator->generate([
273 'user_home' => $this->configurationManager->getConsoleDirectory(),
274 'executable_name' => $executableName,
275 'override' => $override,
276 'destination' => $destination,
277 'config_parameters' => $this->configParameters,
280 $this->getIo()->writeln($this->trans('application.messages.autocomplete'));
286 * @param string $source
287 * @param string $destination
288 * @param string $override
291 private function copyFile($source, $destination, $override)
293 if (file_exists($destination)) {
297 $destination . '.old'
304 $filePath = dirname($destination);
305 if (!is_dir($filePath)) {
306 mkdir($filePath, 0777, true);