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

@ -7,11 +7,31 @@ use Symfony\Component\Console\Command\Command;
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;
class GenerateDrupal7Command extends Command
{
/** @var Filesystem */
private $filesystem;
/** @var Finder */
private $finder;
/** @var SymfonyStyle $io */
private $io;
private $moduleName;
public function __construct(Finder $finder, string $name = null)
{
parent::__construct($name);
$this->finder = $finder;
}
/**
* {@inheritdoc}
*/
@ -32,11 +52,14 @@ class GenerateDrupal7Command extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$this->moduleName = $input->getArgument('module-name');
$this->ensureDirectoryDoesNotExist();
mkdir($this->moduleName);
$this
->ensureDirectoryDoesNotExist()
->createModuleDirectory()
->createFiles();
return 0;
}
@ -49,5 +72,40 @@ class GenerateDrupal7Command extends Command
if (is_dir($this->moduleName)) {
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;
}
}