Skip to content

Commit

Permalink
[generate:module:file] New Command issues #1965 (#2481)
Browse files Browse the repository at this point in the history
* New comand to generate module file

* Update generate:module file command
  • Loading branch information
miguel303 authored and enzolutions committed Jul 3, 2016
1 parent 25853f4 commit 6732752
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/translations/en/generate.module.file.yml
@@ -0,0 +1,7 @@
description: 'Generate a .module file'
help: 'The <info>generate:module:file</info> command helps you generates a new .module file'
welcome: 'Welcome to the Drupal module generator'
options:
module: 'The Module name'
questions:
module: 'Enter the new module name'
87 changes: 87 additions & 0 deletions src/Command/Generate/ModuleFileCommand.php
@@ -0,0 +1,87 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Generate\ModuleFileCommand.
*/

namespace Drupal\Console\Command\Generate;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Generator\ModuleFileGenerator;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\GeneratorCommand;
use Drupal\Console\Style\DrupalStyle;


class ModuleFileCommand extends GeneratorCommand
{
use ConfirmationTrait;
use ModuleTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('generate:module:file')
->setDescription($this->trans('commands.generate.module.file.description'))
->setHelp($this->trans('commands.generate.module.file.help'))
->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'));
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
if (!$this->confirmGeneration($io, $yes)) {
return;
}

$machine_name = $input->getOption('module');
$file_path = $this->getSite()->getModulePath($machine_name);

$generator = $this->getGenerator();
$generator->generate(
$machine_name,
$file_path
);
}


/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{

$io = new DrupalStyle($input, $output);

// --module option
$module = $input->getOption('module');

if (!$module) {
// @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($io);
}

$input->setOption('module', $module);

}

/**
* @return ModuleFileGenerator
*/
protected function createGenerator()
{
return new ModuleFileGenerator();
}
}
51 changes: 51 additions & 0 deletions src/Generator/ModuleFileGenerator.php
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Contains \Drupal\Console\Generator\ModuleFileGenerator.
*/

namespace Drupal\Console\Generator;

/**
* Class ModuleFileGenerator
* @package Drupal\Console\Generator
*/
class ModuleFileGenerator extends Generator
{
/**
* @param $machine_name
* @param $file_path
*/
public function generate(
$machine_name,
$file_path
) {
$dir = $file_path .'/'. $machine_name. '.module';

if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the .module file , it already exist at "%s"',
realpath($dir)
)
);
}

}

$parameters = array(
'machine_name' => $machine_name,
'file_path' => $file_path ,
);

if ($machine_name) {
$this->renderFile(
'module/module-file.twig',
$file_path . '/' . $machine_name . '.module',
$parameters
);
}
}
}
5 changes: 5 additions & 0 deletions templates/module/module-file.twig
@@ -0,0 +1,5 @@
{% extends "base/file.php.twig" %}

{% block file_path %}{{machine_name}}.module.{% endblock %}


0 comments on commit 6732752

Please sign in to comment.