Compare commits

...

15 commits

14 changed files with 111 additions and 2225 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/vendor/
/composer.lock

14
.php_cs.dist Normal file
View file

@ -0,0 +1,14 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['fixtures']);
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => true,
'phpdoc_order' => true,
])
->setFinder($finder);

17
.travis.yml Normal file
View file

@ -0,0 +1,17 @@
language: php
php:
- 7.2
- 7.3
- 7.4
env:
matrix:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""
before_script:
- composer install --dev --prefer-source --no-interaction
script:
- vendor/bin/phpunit

View file

@ -1,7 +1,28 @@
# Drupal Module Generator (dmg)
A scaffolding tool for generating new modules for Drupal 7 and 8.
A scaffolding tool for generating new modules for Drupal 7 and (soon) 8.
[Watch a short demo][demo].
[demo]: https://opdavi.es/6i3YZ 'A short demo video on YouTube'
## Installation
The Drupal Module Generator is installed via [Composer][]:
```bash
composer global require opdavies/drupal-module-generator
```
[composer]: https://getcomposer.org
## Usage
TODO
### Drupal 7
```bash
dmg generate:drupal-7-module {name}
```
Generated Drupal 7 modules contain the appropriately named `.info` and `.module` files,
as well as a test case located in `src/Tests/Functional` which [is loaded automatically](https://www.oliverdavies.uk/articles/psr4-autoloading-test-cases-drupal-7).

View file

@ -13,7 +13,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
require __DIR__.'/../vendor/autoload.php';
}
$app = new Application();
$app = new Application('Drupal Module Generator');
$finder = new Finder();
$moduleNameConverter = new ModuleNameConverter();

View file

@ -3,16 +3,17 @@
"description": "Generates boilerplate code for Drupal modules.",
"type": "project",
"require": {
"symfony/console": "^3.0 || ^4.0 || ^5.0",
"symfony/dependency-injection": "^3.0 || ^4.0 || ^5.0",
"symfony/filesystem": "^3.0 || ^4.0 || ^5.0",
"symfony/finder": "^3.0 || ^4.0 || ^5.0",
"php": "^7.2",
"symfony/console": "^4.0 || ^5.0",
"symfony/dependency-injection": "^4.0 || ^5.0",
"symfony/filesystem": "^4.0 || ^5.0",
"symfony/finder": "^4.0 || ^5.0",
"tightenco/collect": "^6.14"
},
"require-dev": {
"php": "5.6 || ^7.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^3.0 || ^4.0 || ^5.0"
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "^8.5",
"symfony/var-dumper": "^4.0 || ^5.0"
},
"license": "MIT",
"authors": [
@ -35,6 +36,9 @@
"bin/dmg"
],
"config": {
"platform": {
"php": "7.2.5"
},
"sort-packages": true
}
}

2200
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,3 @@
name = {{ name }}
description = The description for {{ name }}.
description = {{ name }} module.
core = 7.x

View file

@ -1,5 +1,21 @@
<?php
namespace Drupal\{{ name }}\Tests\Functional;
namespace Drupal\{{ machine_name }}\Tests\Functional;
final class {{ test_name }} extends \DrupalWebTestCase {}
final class {{ test_name }} extends \DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => '{{ name }}',
'description' => '{{ name }} tests.',
'group' => '{{ name }}',
);
}
public function test_that_the_front_page_loads() {
$this->drupalGet('<front>');
$this->assertResponse(200);
}
}

View file

@ -10,12 +10,11 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tightenco\Collect\Support\Collection;
class GenerateDrupal7Command extends Command
final class GenerateDrupal7Command extends Command
{
private $moduleName;
private $machineName;
@ -42,7 +41,7 @@ class GenerateDrupal7Command extends Command
/**
* {@inheritdoc}
*/
protected static $defaultName = 'generate-drupal-7-module';
protected static $defaultName = 'generate:drupal-7-module';
/**
* {@inheritDoc}
@ -50,8 +49,10 @@ class GenerateDrupal7Command extends Command
protected function configure()
{
$this
->setDescription('Generate a new Drupal 7 module.')
->addArgument('module-name', InputArgument::REQUIRED, 'The name of the module to create');
->setDescription('Generate a new Drupal 7 module')
->addArgument('module-name', InputArgument::REQUIRED, 'The name of the module to create')
->setAliases(['d7', 'drupal7'])
;
}
/**
@ -61,6 +62,8 @@ class GenerateDrupal7Command extends Command
{
$this->io = new SymfonyStyle($input, $output);
$this->io->title("Drupal Module Generator (D7)");
$this->machineName = $input->getArgument('module-name');
$this->moduleName = $this->moduleNameConverter->__invoke($this->machineName);
$this->testName = $this->testNameConverter->__invoke($this->machineName);
@ -95,7 +98,6 @@ class GenerateDrupal7Command extends Command
private function createFiles()
{
$createdFiles = new Collection();
$testNameConverter = new TestNameConverter();
/** @var SplFileInfo $file */
foreach ($this->finder->in(__DIR__.'/../../fixtures/drupal7_module')->files() as $file) {
@ -115,11 +117,16 @@ class GenerateDrupal7Command extends Command
$createdFiles->push($filename);
}
$this->io->listing($createdFiles->filter()->sort()->toArray());
if ($createdFiles->isNotEmpty()) {
$this->io->block('Files generated:');
$this->io->listing($createdFiles->sort()->toArray());
}
}
private function updateFileContents($contents)
{
$contents = str_replace('{{ machine_name }}', $this->machineName, $contents);
$contents = str_replace('{{ name }}', $this->moduleName, $contents);
$contents = str_replace('{{ test_name }}', $this->testName, $contents);

View file

@ -2,7 +2,7 @@
namespace Opdavies\DrupalModuleGenerator\Exception;
class CannotCreateModuleException extends \RuntimeException
final class CannotCreateModuleException extends \RuntimeException
{
public static function directoryAlreadyExists()
{

View file

@ -2,7 +2,7 @@
namespace Opdavies\DrupalModuleGenerator\Service;
class ModuleNameConverter
final class ModuleNameConverter
{
public function __invoke(string $moduleName)
{

View file

@ -2,7 +2,7 @@
namespace Opdavies\DrupalModuleGenerator\Service;
class TestNameConverter
final class TestNameConverter
{
public function __invoke(string $moduleName)
{

View file

@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class GenerateDrupal7ModuleCommandTest extends TestCase
final class GenerateDrupal7ModuleCommandTest extends TestCase
{
protected function tearDown(): void
{
@ -69,7 +69,7 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
$contents = file_get_contents('test_module/test_module.info');
$this->assertStringContainsString('name = Test Module', $contents);
$this->assertStringContainsString('description = The description for Test Module.', $contents);
$this->assertStringContainsString('description = Test Module module.', $contents);
}
/** @test */
@ -109,6 +109,12 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
$contents = file_get_contents('test_module/src/Tests/Functional/TestModuleTest.php');
$this->assertStringContainsString('namespace Drupal\\test_module\\Tests\\Functional', $contents);
$this->assertStringContainsString('final class TestModuleTest', $contents);
$this->assertStringContainsString("'name' => 'Test Module'", $contents);
$this->assertStringContainsString("'description' => 'Test Module tests.'", $contents);
$this->assertStringContainsString("'group' => 'Test Module'", $contents);
}
}