namespace Drupal\Console\Command\Generate;
-use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Generator\ModuleGenerator;
use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
+use Drupal\Console\Core\Command\Command;
use Drupal\Console\Utils\Validator;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Utils\DrupalApi;
+use Webmozart\PathUtil\Path;
class ModuleCommand extends Command
{
use ConfirmationTrait;
- use CommandTrait;
/**
* @var ModuleGenerator
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.module.options.twigtemplate')
- );
+ )
+ ->setAliases(['gm']);
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
- $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
-
- // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
- if (!$this->confirmGeneration($io, $yes)) {
+ // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+ if (!$this->confirmOperation()) {
return 1;
}
$module = $this->validator->validateModuleName($input->getOption('module'));
- $modulePath = $this->appRoot . $input->getOption('module-path');
+ // Get the profile path and define a profile path if it is null
+ // Check that it is an absolute path or otherwise create an absolute path using appRoot
+ $modulePath = $input->getOption('module-path');
+ $modulePath = $modulePath == null ? 'modules/custom' : $modulePath;
+ $modulePath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
$modulePath = $this->validator->validateModulePath($modulePath, true);
$machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
$dependencies = $this->validator->validateExtensions(
$input->getOption('dependencies'),
'module',
- $io
+ $this->getIo()
);
$test = $input->getOption('test');
$twigTemplate = $input->getOption('twigtemplate');
- $this->generator->generate(
- $module,
- $machineName,
- $modulePath,
- $description,
- $core,
- $package,
- $moduleFile,
- $featuresBundle,
- $composer,
- $dependencies,
- $test,
- $twigTemplate
- );
+ $this->generator->generate([
+ 'module' => $module,
+ 'machine_name' => $machineName,
+ 'module_path' => $modulePath,
+ 'description' => $description,
+ 'core' => $core,
+ 'package' => $package,
+ 'module_file' => $moduleFile,
+ 'features_bundle' => $featuresBundle,
+ 'composer' => $composer,
+ 'dependencies' => $dependencies,
+ 'test' => $test,
+ 'twig_template' => $twigTemplate,
+ ]);
return 0;
}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
$validator = $this->validator;
try {
$input->getOption('module')
) : null;
} catch (\Exception $error) {
- $io->error($error->getMessage());
+ $this->getIo()->error($error->getMessage());
return 1;
}
if (!$module) {
- $module = $io->ask(
+ $module = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.module'),
null,
function ($module) use ($validator) {
$input->getOption('machine-name')
) : null;
} catch (\Exception $error) {
- $io->error($error->getMessage());
+ $this->getIo()->error($error->getMessage());
}
if (!$machineName) {
- $machineName = $io->ask(
+ $machineName = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.machine-name'),
$this->stringConverter->createMachineName($module),
function ($machine_name) use ($validator) {
$modulePath = $input->getOption('module-path');
if (!$modulePath) {
- $drupalRoot = $this->appRoot;
- $modulePath = $io->ask(
+ $modulePath = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.module-path'),
- '/modules/custom',
- function ($modulePath) use ($drupalRoot, $machineName) {
- $modulePath = ($modulePath[0] != '/' ? '/' : '').$modulePath;
- $fullPath = $drupalRoot.$modulePath.'/'.$machineName;
+ 'modules/custom',
+ function ($modulePath) use ($machineName) {
+ $fullPath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
+ $fullPath = $fullPath.'/'.$machineName;
if (file_exists($fullPath)) {
throw new \InvalidArgumentException(
sprintf(
$description = $input->getOption('description');
if (!$description) {
- $description = $io->ask(
+ $description = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.description'),
- 'My Awesome Module'
+ $this->trans('commands.generate.module.suggestions.my-awesome-module')
);
}
$input->setOption('description', $description);
$package = $input->getOption('package');
if (!$package) {
- $package = $io->ask(
+ $package = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.package'),
'Custom'
);
$core = $input->getOption('core');
if (!$core) {
- $core = $io->ask(
+ $core = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.core'), '8.x',
function ($core) {
// Only allow 8.x and higher as core version.
$moduleFile = $input->getOption('module-file');
if (!$moduleFile) {
- $moduleFile = $io->confirm(
+ $moduleFile = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.module-file'),
true
);
$featuresBundle = $input->getOption('features-bundle');
if (!$featuresBundle) {
- $featuresSupport = $io->confirm(
+ $featuresSupport = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.features-support'),
false
);
if ($featuresSupport) {
- $featuresBundle = $io->ask(
+ $featuresBundle = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.features-bundle'),
'default'
);
$composer = $input->getOption('composer');
if (!$composer) {
- $composer = $io->confirm(
+ $composer = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.composer'),
true
);
$dependencies = $input->getOption('dependencies');
if (!$dependencies) {
- $addDependencies = $io->confirm(
+ $addDependencies = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.dependencies'),
false
);
if ($addDependencies) {
- $dependencies = $io->ask(
+ $dependencies = $this->getIo()->ask(
$this->trans('commands.generate.module.options.dependencies')
);
}
$test = $input->getOption('test');
if (!$test) {
- $test = $io->confirm(
+ $test = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.test'),
true
);
$twigtemplate = $input->getOption('twigtemplate');
if (!$twigtemplate) {
- $twigtemplate = $io->confirm(
+ $twigtemplate = $this->getIo()->confirm(
$this->trans('commands.generate.module.questions.twigtemplate'),
true
);
$input->setOption('twigtemplate', $twigtemplate);
}
}
-
- /**
- * @return ModuleGenerator
- */
- protected function createGenerator()
- {
- return new ModuleGenerator();
- }
}