Generate a .info file

This commit is contained in:
Oliver Davies 2020-02-09 13:32:21 +00:00
parent 000716d670
commit 55ca3647c4
6 changed files with 255 additions and 69 deletions

View file

@ -2,13 +2,16 @@
use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command; use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder;
require_once __DIR__.'/vendor/autoload.php'; require_once __DIR__.'/vendor/autoload.php';
$app = new Application(); $app = new Application();
$finder = new Finder();
$app->addCommands([ $app->addCommands([
new GenerateDrupal7Command(), new GenerateDrupal7Command($finder),
]); ]);
$app->run(); $app->run();

View file

@ -4,7 +4,9 @@
"type": "project", "type": "project",
"require": { "require": {
"symfony/console": "^5.0", "symfony/console": "^5.0",
"symfony/dependency-injection": "^5.0" "symfony/dependency-injection": "^5.0",
"symfony/filesystem": "^5.0",
"symfony/finder": "^5.0"
}, },
"require-dev": { "require-dev": {
"php": "5.6 || ^7.0", "php": "5.6 || ^7.0",

217
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "21bd996a5a5331085c6854ddc3f384f2", "content-hash": "eec04f9d7e938466a1a1782750a35b63",
"packages": [ "packages": [
{ {
"name": "psr/container", "name": "psr/container",
@ -204,6 +204,163 @@
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"time": "2020-01-31T09:49:43+00:00" "time": "2020-01-31T09:49:43+00:00"
}, },
{
"name": "symfony/filesystem",
"version": "v5.0.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/filesystem.git",
"reference": "3afadc0f57cd74f86379d073e694b0f2cda2a88c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/filesystem/zipball/3afadc0f57cd74f86379d073e694b0f2cda2a88c",
"reference": "3afadc0f57cd74f86379d073e694b0f2cda2a88c",
"shasum": ""
},
"require": {
"php": "^7.2.5",
"symfony/polyfill-ctype": "~1.8"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Filesystem\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Filesystem Component",
"homepage": "https://symfony.com",
"time": "2020-01-21T08:40:24+00:00"
},
{
"name": "symfony/finder",
"version": "v5.0.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/finder.git",
"reference": "4176e7cb846fe08f32518b7e0ed8462e2db8d9bb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/finder/zipball/4176e7cb846fe08f32518b7e0ed8462e2db8d9bb",
"reference": "4176e7cb846fe08f32518b7e0ed8462e2db8d9bb",
"shasum": ""
},
"require": {
"php": "^7.2.5"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Finder\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Finder Component",
"homepage": "https://symfony.com",
"time": "2020-01-04T14:08:26+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.13.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f8f0b461be3385e56d6de3dbb5a0df24c0c275e3",
"reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.13-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"time": "2019-11-27T13:56:44+00:00"
},
{ {
"name": "symfony/polyfill-mbstring", "name": "symfony/polyfill-mbstring",
"version": "v1.13.1", "version": "v1.13.1",
@ -1817,64 +1974,6 @@
"homepage": "https://github.com/sebastianbergmann/version", "homepage": "https://github.com/sebastianbergmann/version",
"time": "2020-01-21T06:36:37+00:00" "time": "2020-01-21T06:36:37+00:00"
}, },
{
"name": "symfony/polyfill-ctype",
"version": "v1.13.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f8f0b461be3385e56d6de3dbb5a0df24c0c275e3",
"reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.13-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"time": "2019-11-27T13:56:44+00:00"
},
{ {
"name": "symfony/var-dumper", "name": "symfony/var-dumper",
"version": "v5.0.4", "version": "v5.0.4",

View file

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

View file

@ -7,11 +7,31 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; 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;
class GenerateDrupal7Command extends Command class GenerateDrupal7Command extends Command
{ {
/** @var Filesystem */
private $filesystem;
/** @var Finder */
private $finder;
/** @var SymfonyStyle $io */
private $io;
private $moduleName; private $moduleName;
public function __construct(Finder $finder, string $name = null)
{
parent::__construct($name);
$this->finder = $finder;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -32,11 +52,14 @@ class GenerateDrupal7Command extends Command
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->io = new SymfonyStyle($input, $output);
$this->moduleName = $input->getArgument('module-name'); $this->moduleName = $input->getArgument('module-name');
$this->ensureDirectoryDoesNotExist(); $this
->ensureDirectoryDoesNotExist()
mkdir($this->moduleName); ->createModuleDirectory()
->createFiles();
return 0; return 0;
} }
@ -49,5 +72,40 @@ class GenerateDrupal7Command extends Command
if (is_dir($this->moduleName)) { if (is_dir($this->moduleName)) {
throw CannotCreateModuleException::directoryAlreadyExists(); throw CannotCreateModuleException::directoryAlreadyExists();
} }
return $this;
}
private function createModuleDirectory()
{
mkdir($this->moduleName);
return $this;
}
private function createFiles()
{
$createdFiles = [];
/** @var SplFileInfo $file */
foreach ($this->finder->in('fixtures/drupal7_module')->name('/.info/') as $file) {
$contents = $this->updateFileContents($file->getContents());
file_put_contents(
"{$this->moduleName}/{$this->moduleName}.{$file->getExtension()}",
$contents
);
$createdFiles[] = "{$this->moduleName}.{$file->getExtension()}";
}
$this->io->definitionList($createdFiles);
}
private function updateFileContents($contents)
{
$contents = str_replace('{{ name }}', $this->moduleName, $contents);
return $contents;
} }
} }

View file

@ -6,12 +6,14 @@ use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException; use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class GenerateDrupal7ModuleCommandTest extends TestCase class GenerateDrupal7ModuleCommandTest extends TestCase
{ {
protected function tearDown(): void protected function tearDown(): void
{ {
rmdir('test_module'); (new Filesystem())->remove('test_module');
} }
/** @test */ /** @test */
@ -20,7 +22,10 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
$this->expectExceptionObject(CannotCreateModuleException::directoryAlreadyExists()); $this->expectExceptionObject(CannotCreateModuleException::directoryAlreadyExists());
$commandTester = new CommandTester(new GenerateDrupal7Command()); $finder = new Finder();
$command = new GenerateDrupal7Command($finder);
$commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
'module-name' => 'test_module' 'module-name' => 'test_module'
]); ]);
@ -29,17 +34,33 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
/** @test */ /** @test */
public function it_creates_a_new_module_directory() public function it_creates_a_new_module_directory()
{ {
$commandTester = new CommandTester(new GenerateDrupal7Command()); $finder = new Finder();
$command = new GenerateDrupal7Command($finder);
$commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
'module-name' => 'test_module', 'module-name' => 'test_module',
]); ]);
$this->assertTrue(is_dir('test_module')); $this->assertTrue(is_dir('test_module'));
} }
/** @test */
public function it_generates_an_info_file()
{
$finder = new Finder();
$command = new GenerateDrupal7Command($finder);
$commandTester = new CommandTester($command);
$commandTester->execute([
'module-name' => 'test_module',
]); ]);
$this->assertTrue(is_dir('my-new-drupal-module')); $this->assertTrue(is_file('test_module/test_module.info'));
rmdir('my-new-drupal-module'); $contents = file_get_contents('test_module/test_module.info');
$this->assertStringContainsString('name = test_module', $contents);
$this->assertStringContainsString('description = The description for test_module.', $contents);
} }
} }