From 896dfc3fd394ae829e0be7ebe9ecd2d65989b867 Mon Sep 17 00:00:00 2001 From: Chris Haynes Date: Sat, 17 Dec 2016 17:21:03 -0600 Subject: [PATCH] Additions to the generate module command (#2995) * Adding template folder and file creation to generate module command. * Undoing un needed changes. * Undoing un needed changes. * Formatting changes. --- src/Command/Generate/ModuleCommand.php | 29 +++++++++++- src/Generator/ModuleGenerator.php | 46 ++++++++++++++++++- .../module/module-twig-template-append.twig | 12 +++++ templates/module/twig-template-file.twig | 1 + 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 templates/module/module-twig-template-append.twig create mode 100644 templates/module/twig-template-file.twig diff --git a/src/Command/Generate/ModuleCommand.php b/src/Command/Generate/ModuleCommand.php index 375f9d743..826e1986c 100644 --- a/src/Command/Generate/ModuleCommand.php +++ b/src/Command/Generate/ModuleCommand.php @@ -59,6 +59,11 @@ class ModuleCommand extends Command */ protected $site; + /** + * @var string + */ + protected $twigtemplate; + /** * ModuleCommand constructor. @@ -69,6 +74,7 @@ class ModuleCommand extends Command * @param DrupalApi $drupalApi * @param Client $httpClient * @param Site $site + * @param $twigtemplate */ public function __construct( ModuleGenerator $generator, @@ -77,7 +83,8 @@ public function __construct( StringConverter $stringConverter, DrupalApi $drupalApi, Client $httpClient, - Site $site + Site $site, + $twigtemplate ) { $this->generator = $generator; $this->validator = $validator; @@ -86,6 +93,7 @@ public function __construct( $this->drupalApi = $drupalApi; $this->httpClient = $httpClient; $this->site = $site; + $this->twigtemplate = $twigtemplate; parent::__construct(); } @@ -163,6 +171,12 @@ protected function configure() '', InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.module.options.test') + ) + ->addOption( + 'twigtemplate', + '', + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.module.options.twigtemplate') ); } @@ -192,6 +206,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $featuresBundle = $input->getOption('features-bundle'); $composer = $input->getOption('composer'); $test = $input->getOption('test'); + $twigtemplate = $input->getOption('twigtemplate'); // Modules Dependencies, re-factor and share with other commands $dependencies = $this->validator->validateModuleDependencies($input->getOption('dependencies')); @@ -220,7 +235,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $featuresBundle, $composer, $dependencies, - $test + $test, + $twigtemplate ); } @@ -434,6 +450,15 @@ function ($core) { ); $input->setOption('test', $test); } + + $twigtemplate = $input->getOption('twigtemplate'); + if (!$twigtemplate) { + $twigtemplate = $io->confirm( + $this->trans('commands.generate.module.questions.twigtemplate'), + true + ); + $input->setOption('twigtemplate', $twigtemplate); + } } /** diff --git a/src/Generator/ModuleGenerator.php b/src/Generator/ModuleGenerator.php index 4ddbad95d..705453c9e 100644 --- a/src/Generator/ModuleGenerator.php +++ b/src/Generator/ModuleGenerator.php @@ -25,6 +25,7 @@ class ModuleGenerator extends Generator * @param $composer * @param $dependencies * @param $test + * @param $twigtemplate */ public function generate( $module, @@ -37,7 +38,8 @@ public function generate( $featuresBundle, $composer, $dependencies, - $test + $test, + $twigtemplate ) { $dir .= '/'.$machineName; if (file_exists($dir)) { @@ -77,6 +79,7 @@ public function generate( 'package' => $package, 'dependencies' => $dependencies, 'test' => $test, + 'twigtemplate' => $twigtemplate, ); $this->renderFile( @@ -118,5 +121,46 @@ public function generate( $parameters ); } + if ($twigtemplate) { + $this->renderFile( + 'module/module-twig-template-append.twig', + $dir .'/' . $machineName . '.module', + $parameters, + FILE_APPEND + ); + $dir .= '/templates/'; + if (file_exists($dir)) { + if (!is_dir($dir)) { + throw new \RuntimeException( + sprintf( + 'Unable to generate the templates directory as the target directory "%s" exists but is a file.', + realpath($dir) + ) + ); + } + $files = scandir($dir); + if ($files != array('.', '..')) { + throw new \RuntimeException( + sprintf( + 'Unable to generate the templates directory as the target directory "%s" is not empty.', + realpath($dir) + ) + ); + } + if (!is_writable($dir)) { + throw new \RuntimeException( + sprintf( + 'Unable to generate the templates directory as the target directory "%s" is not writable.', + realpath($dir) + ) + ); + } + } + $this->renderFile( + 'module/twig-template-file.twig', + $dir . $machineName . '.html.twig', + $parameters + ); + } } } diff --git a/templates/module/module-twig-template-append.twig b/templates/module/module-twig-template-append.twig new file mode 100644 index 000000000..42b3d687b --- /dev/null +++ b/templates/module/module-twig-template-append.twig @@ -0,0 +1,12 @@ + +/** + * Implements hook_theme(). + */ +function {{machine_name}}_theme() { + return [ + '{{machine_name}}' => [ + 'template' => '{{machine_name}}', + 'render element' => 'children', + ], + ]; +} diff --git a/templates/module/twig-template-file.twig b/templates/module/twig-template-file.twig new file mode 100644 index 000000000..91e43c8f8 --- /dev/null +++ b/templates/module/twig-template-file.twig @@ -0,0 +1 @@ + \ No newline at end of file