5 * Contains \Drupal\Console\Command\Generate\ModuleCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Generator\ModuleGenerator;
14 use Drupal\Console\Command\Shared\ConfirmationTrait;
15 use Drupal\Console\Core\Command\Command;
16 use Drupal\Console\Utils\Validator;
17 use Drupal\Console\Core\Utils\StringConverter;
18 use Drupal\Console\Utils\DrupalApi;
19 use Webmozart\PathUtil\Path;
21 class ModuleCommand extends Command
23 use ConfirmationTrait;
26 * @var ModuleGenerator
41 * @var StringConverter
43 protected $stringConverter;
53 protected $twigtemplate;
57 * ModuleCommand constructor.
59 * @param ModuleGenerator $generator
60 * @param Validator $validator
62 * @param StringConverter $stringConverter
63 * @param DrupalApi $drupalApi
64 * @param $twigtemplate
66 public function __construct(
67 ModuleGenerator $generator,
70 StringConverter $stringConverter,
74 $this->generator = $generator;
75 $this->validator = $validator;
76 $this->appRoot = $appRoot;
77 $this->stringConverter = $stringConverter;
78 $this->drupalApi = $drupalApi;
79 $this->twigtemplate = $twigtemplate;
80 parent::__construct();
86 protected function configure()
89 ->setName('generate:module')
90 ->setDescription($this->trans('commands.generate.module.description'))
91 ->setHelp($this->trans('commands.generate.module.help'))
95 InputOption::VALUE_REQUIRED,
96 $this->trans('commands.generate.module.options.module')
101 InputOption::VALUE_REQUIRED,
102 $this->trans('commands.generate.module.options.machine-name')
107 InputOption::VALUE_REQUIRED,
108 $this->trans('commands.generate.module.options.module-path')
113 InputOption::VALUE_OPTIONAL,
114 $this->trans('commands.generate.module.options.description')
119 InputOption::VALUE_OPTIONAL,
120 $this->trans('commands.generate.module.options.core')
125 InputOption::VALUE_OPTIONAL,
126 $this->trans('commands.generate.module.options.package')
131 InputOption::VALUE_NONE,
132 $this->trans('commands.generate.module.options.module-file')
137 InputOption::VALUE_REQUIRED,
138 $this->trans('commands.generate.module.options.features-bundle')
143 InputOption::VALUE_NONE,
144 $this->trans('commands.generate.module.options.composer')
149 InputOption::VALUE_OPTIONAL,
150 $this->trans('commands.generate.module.options.dependencies'),
156 InputOption::VALUE_OPTIONAL,
157 $this->trans('commands.generate.module.options.test')
162 InputOption::VALUE_OPTIONAL,
163 $this->trans('commands.generate.module.options.twigtemplate')
165 ->setAliases(['gm']);
171 protected function execute(InputInterface $input, OutputInterface $output)
173 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
174 if (!$this->confirmOperation()) {
178 $module = $this->validator->validateModuleName($input->getOption('module'));
180 // Get the profile path and define a profile path if it is null
181 // Check that it is an absolute path or otherwise create an absolute path using appRoot
182 $modulePath = $input->getOption('module-path');
183 $modulePath = $modulePath == null ? 'modules/custom' : $modulePath;
184 $modulePath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
185 $modulePath = $this->validator->validateModulePath($modulePath, true);
187 $machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
188 $description = $input->getOption('description');
189 $core = $input->getOption('core');
190 $package = $input->getOption('package');
191 $moduleFile = $input->getOption('module-file');
192 $featuresBundle = $input->getOption('features-bundle');
193 $composer = $input->getOption('composer');
194 $dependencies = $this->validator->validateExtensions(
195 $input->getOption('dependencies'),
199 $test = $input->getOption('test');
200 $twigTemplate = $input->getOption('twigtemplate');
202 $this->generator->generate([
204 'machine_name' => $machineName,
205 'module_path' => $modulePath,
206 'description' => $description,
208 'package' => $package,
209 'module_file' => $moduleFile,
210 'features_bundle' => $featuresBundle,
211 'composer' => $composer,
212 'dependencies' => $dependencies,
214 'twig_template' => $twigTemplate,
223 protected function interact(InputInterface $input, OutputInterface $output)
225 $validator = $this->validator;
228 $module = $input->getOption('module') ?
229 $this->validator->validateModuleName(
230 $input->getOption('module')
232 } catch (\Exception $error) {
233 $this->getIo()->error($error->getMessage());
239 $module = $this->getIo()->ask(
240 $this->trans('commands.generate.module.questions.module'),
242 function ($module) use ($validator) {
243 return $validator->validateModuleName($module);
246 $input->setOption('module', $module);
250 $machineName = $input->getOption('machine-name') ?
251 $this->validator->validateModuleName(
252 $input->getOption('machine-name')
254 } catch (\Exception $error) {
255 $this->getIo()->error($error->getMessage());
259 $machineName = $this->getIo()->ask(
260 $this->trans('commands.generate.module.questions.machine-name'),
261 $this->stringConverter->createMachineName($module),
262 function ($machine_name) use ($validator) {
263 return $validator->validateMachineName($machine_name);
266 $input->setOption('machine-name', $machineName);
269 $modulePath = $input->getOption('module-path');
271 $modulePath = $this->getIo()->ask(
272 $this->trans('commands.generate.module.questions.module-path'),
274 function ($modulePath) use ($machineName) {
275 $fullPath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
276 $fullPath = $fullPath.'/'.$machineName;
277 if (file_exists($fullPath)) {
278 throw new \InvalidArgumentException(
280 $this->trans('commands.generate.module.errors.directory-exists'),
290 $input->setOption('module-path', $modulePath);
292 $description = $input->getOption('description');
294 $description = $this->getIo()->ask(
295 $this->trans('commands.generate.module.questions.description'),
296 $this->trans('commands.generate.module.suggestions.my-awesome-module')
299 $input->setOption('description', $description);
301 $package = $input->getOption('package');
303 $package = $this->getIo()->ask(
304 $this->trans('commands.generate.module.questions.package'),
308 $input->setOption('package', $package);
310 $core = $input->getOption('core');
312 $core = $this->getIo()->ask(
313 $this->trans('commands.generate.module.questions.core'), '8.x',
315 // Only allow 8.x and higher as core version.
316 if (!preg_match('/^([0-9]+)\.x$/', $core, $matches) || ($matches[1] < 8)) {
317 throw new \InvalidArgumentException(
319 $this->trans('commands.generate.module.errors.invalid-core'),
328 $input->setOption('core', $core);
331 $moduleFile = $input->getOption('module-file');
333 $moduleFile = $this->getIo()->confirm(
334 $this->trans('commands.generate.module.questions.module-file'),
337 $input->setOption('module-file', $moduleFile);
340 $featuresBundle = $input->getOption('features-bundle');
341 if (!$featuresBundle) {
342 $featuresSupport = $this->getIo()->confirm(
343 $this->trans('commands.generate.module.questions.features-support'),
346 if ($featuresSupport) {
347 $featuresBundle = $this->getIo()->ask(
348 $this->trans('commands.generate.module.questions.features-bundle'),
352 $input->setOption('features-bundle', $featuresBundle);
355 $composer = $input->getOption('composer');
357 $composer = $this->getIo()->confirm(
358 $this->trans('commands.generate.module.questions.composer'),
361 $input->setOption('composer', $composer);
364 $dependencies = $input->getOption('dependencies');
365 if (!$dependencies) {
366 $addDependencies = $this->getIo()->confirm(
367 $this->trans('commands.generate.module.questions.dependencies'),
370 if ($addDependencies) {
371 $dependencies = $this->getIo()->ask(
372 $this->trans('commands.generate.module.options.dependencies')
375 $input->setOption('dependencies', $dependencies);
378 $test = $input->getOption('test');
380 $test = $this->getIo()->confirm(
381 $this->trans('commands.generate.module.questions.test'),
384 $input->setOption('test', $test);
387 $twigtemplate = $input->getOption('twigtemplate');
388 if (!$twigtemplate) {
389 $twigtemplate = $this->getIo()->confirm(
390 $this->trans('commands.generate.module.questions.twigtemplate'),
393 $input->setOption('twigtemplate', $twigtemplate);