diff --git a/services.yml b/services.yml index 20cb70c..5fa41e4 100644 --- a/services.yml +++ b/services.yml @@ -26,6 +26,8 @@ services: console.chain_discovery: class: Drupal\Console\Core\Utils\ChainDiscovery arguments: ['@console.root', '@console.configuration_manager'] + console.count_code_lines: + class: Drupal\Console\Core\Utils\CountCodeLines # DrupalConsoleCore Commands console.about: class: Drupal\Console\Core\Command\AboutCommand diff --git a/src/Application.php b/src/Application.php index 72907fc..909ee87 100644 --- a/src/Application.php +++ b/src/Application.php @@ -2,6 +2,7 @@ namespace Drupal\Console\Core; +use Drupal\Console\Core\EventSubscriber\ShowGenerateCountCodeLinesListener; use Drupal\Console\Core\Utils\TranslatorManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -242,6 +243,11 @@ private function registerEvents() $this->container->get('console.translator_manager') ) ); + $dispatcher->addSubscriber( + new ShowGenerateCountCodeLinesListener( + $this->container->get('console.count_code_lines') + ) + ); $this->setDispatcher($dispatcher); } diff --git a/src/EventSubscriber/ShowGenerateCountCodeLinesListener.php b/src/EventSubscriber/ShowGenerateCountCodeLinesListener.php new file mode 100644 index 0000000..d1a3292 --- /dev/null +++ b/src/EventSubscriber/ShowGenerateCountCodeLinesListener.php @@ -0,0 +1,66 @@ +countCodeLines = $countCodeLines; + } + + /** + * @param ConsoleTerminateEvent $event + */ + public function showGenerateCountLines(ConsoleTerminateEvent $event) + { + if ($event->getExitCode() != 0) { + return; + } + + /* @var Command $command */ + $command = $event->getCommand(); + /* @var DrupalStyle $io */ + $io = new DrupalStyle($event->getInput(), $event->getOutput()); + + $countCodeLines = $this->countCodeLines->getCountCodeLines(); + $io->writeln($countCodeLines); + } + + /** + * @{@inheritdoc} + */ + public static function getSubscribedEvents() + { + return [ConsoleEvents::TERMINATE => 'showGenerateCountCodeLines']; + } +} diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 87d81dd..b276faa 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -9,6 +9,7 @@ use Drupal\Console\Core\Utils\TwigRenderer; use Drupal\Console\Core\Utils\FileQueue; +use Drupal\Console\Core\Utils\CountCodeLines; /** * Class Generator @@ -27,6 +28,11 @@ abstract class Generator */ protected $fileQueue; + /** + * @var CountCodeLines + */ + protected $countCodeLines; + /** * @param $renderer */ @@ -43,6 +49,14 @@ public function setFileQueue(FileQueue $fileQueue) $this->fileQueue = $fileQueue; } + /** + * @param $countCodeLines + */ + public function setCountCodeLines(CountCodeLines $countCodeLines) + { + $this->countCodeLines = $countCodeLines; + } + /** * @param string $template * @param string $target @@ -61,18 +75,31 @@ protected function renderFile( mkdir(dirname($target), 0777, true); } + $currentLine = 0; + if (!empty($flag) && file_exists($target)) { + $currentLine = count(file($target)); + } $content = $this->renderer->render($template, $parameters); if (file_put_contents($target, $content, $flag)) { $this->fileQueue->addFile($target); + $newCodeLine = count(file($target)); + + if ($currentLine > 0) { + $newCodeLine = ($newCodeLine-$currentLine); + } + + $this->countCodeLines->addCountCodeLines($newCodeLine); + return true; } return false; } - public function addSkeletonDir($skeletonDir) { + public function addSkeletonDir($skeletonDir) + { $this->renderer->addSkeletonDir($skeletonDir); } } diff --git a/src/Utils/CountCodeLines.php b/src/Utils/CountCodeLines.php new file mode 100644 index 0000000..6467a08 --- /dev/null +++ b/src/Utils/CountCodeLines.php @@ -0,0 +1,37 @@ +countCodeLine = $this->countCodeLine + $countCodeLine; + } + + /** + * @return integer + */ + public function getCountCodeLines() + { + return $this->countCodeLine; + } +}