From 41b4773d1e8c831d5533fc9f7ec3759f8e57164d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kr=C3=B3l?= Date: Tue, 22 Aug 2017 21:48:25 +0200 Subject: [PATCH] Merge commands --- README.md | 2 +- src/Console/Application.php | 1 - src/Console/FormatCommand.php | 6 ++-- src/Console/RegisterCommand.php | 36 ------------------- src/Wingman.php | 9 ++--- ...rCommandTest.php => FormatCommandTest.php} | 7 ++-- 6 files changed, 11 insertions(+), 50 deletions(-) delete mode 100644 src/Console/RegisterCommand.php rename tests/Console/{RegisterCommandTest.php => FormatCommandTest.php} (88%) diff --git a/README.md b/README.md index a8f6776..b1dabd0 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Formatting file: ./composer.json Install `mleko/wingman` as dependency ``` $ composer require --dev mleko/wingman -$ vendor/bin/wingman register +$ vendor/bin/wingman --register Register wingman in file: ./composer.json Wingman registered Formatting file: ./composer.json diff --git a/src/Console/Application.php b/src/Console/Application.php index 123307b..98e62f1 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -17,7 +17,6 @@ public function __construct() parent::__construct("Wingman"); $this->add(new FormatCommand()); - $this->add(new RegisterCommand()); $this->setDefaultCommand("format"); } diff --git a/src/Console/FormatCommand.php b/src/Console/FormatCommand.php index bf4d213..161d7cb 100644 --- a/src/Console/FormatCommand.php +++ b/src/Console/FormatCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class FormatCommand extends Command @@ -23,14 +24,15 @@ protected function configure() parent::configure(); $this->setName("format") ->setDescription("Format composer.json file") - ->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", "./composer.json"); + ->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", "./composer.json") + ->addOption("register", null, InputOption::VALUE_NONE, "Register wingman as post-update script"); } protected function execute(InputInterface $input, OutputInterface $output) { $path = $input->getArgument("path"); $wingman = new Wingman(); - $wingman->formatFile($path, new ConsoleOutputAdapter($output)); + $wingman->formatFile($path, new ConsoleOutputAdapter($output), $input->getOption("register")); } } diff --git a/src/Console/RegisterCommand.php b/src/Console/RegisterCommand.php deleted file mode 100644 index 27f67c6..0000000 --- a/src/Console/RegisterCommand.php +++ /dev/null @@ -1,36 +0,0 @@ -setName("register") - ->setDescription("Register wingman as post-update script and format composer.json file") - ->addArgument("path", InputArgument::OPTIONAL, "Path to composer.json file", "./composer.json"); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $path = $input->getArgument("path"); - $wingman = new Wingman(); - $wingman->registerInFile($path, new ConsoleOutputAdapter($output)); - } - -} diff --git a/src/Wingman.php b/src/Wingman.php index f817187..1e45120 100644 --- a/src/Wingman.php +++ b/src/Wingman.php @@ -65,14 +65,9 @@ public function format($composerJson) return Formatter::format($composerJson, $this->rules); } - public function formatFile($path, Output $output) + public function formatFile($path, Output $output, $register = false) { - return $this->processFile($path, $output); - } - - public function registerInFile($path, Output $output) - { - return $this->processFile($path, $output, true); + return $this->processFile($path, $output, $register); } public function register($composerJson) diff --git a/tests/Console/RegisterCommandTest.php b/tests/Console/FormatCommandTest.php similarity index 88% rename from tests/Console/RegisterCommandTest.php rename to tests/Console/FormatCommandTest.php index bbb4de6..902215f 100644 --- a/tests/Console/RegisterCommandTest.php +++ b/tests/Console/FormatCommandTest.php @@ -14,7 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; -class RegisterCommandTest extends TestCase +class FormatCommandTest extends TestCase { /** * @var vfsStreamDirectory @@ -36,12 +36,13 @@ public function testRegisterCommand() $virtualFile->setContent(json_encode(["name" => "acme/test"])); $application = new Application(); - $registerCommand = $application->find("register"); + $registerCommand = $application->find("format"); $commandTester = new CommandTester($registerCommand); $resultCode = $commandTester->execute([ "command" => $registerCommand->getName(), - "path" => $virtualFile->url() + "path" => $virtualFile->url(), + "--register" => true ]); $this->assertEquals(0, $resultCode);