diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/AddCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/AddCommandTest.php index 742262f5..3d9e9647 100644 --- a/src/SonsOfPHP/Bard/Tests/Console/Command/AddCommandTest.php +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/AddCommandTest.php @@ -43,15 +43,17 @@ final class AddCommandTest extends TestCase { private Application $application; + private AddCommand $command; + protected function setUp(): void { $this->application = new Application(); + $this->command = $this->application->get('add'); } public function testItsNameIsCorrect(): void { - $command = $this->application->get('add'); - $commandTester = new CommandTester($command); + $commandTester = new CommandTester($this->command); $commandTester->execute([ 'path' => 'tmp/repo', diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/CopyCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/CopyCommandTest.php new file mode 100644 index 00000000..fd2e8af0 --- /dev/null +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/CopyCommandTest.php @@ -0,0 +1,66 @@ +application = new Application(); + $this->command = $this->application->get('copy'); + } + + public function testItExecutesSuccessfully(): void + { + $commandTester = new CommandTester($this->command); + + $commandTester->execute([ + 'source' => 'LICENSE', + '--dry-run' => true, + '-vvv' => true, + ]); + + $commandTester->assertCommandIsSuccessful(); + } +} diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/MergeCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/MergeCommandTest.php new file mode 100644 index 00000000..ccb5696a --- /dev/null +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/MergeCommandTest.php @@ -0,0 +1,85 @@ +application = new Application(); + $this->command = $this->application->get('merge'); + } + + public function testItExecutesSuccessfully(): void + { + $commandTester = new CommandTester($this->command); + + $commandTester->execute([ + '--dry-run' => true, + '-vvv' => true, + ]); + + $commandTester->assertCommandIsSuccessful(); + } +} diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/PushCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/PushCommandTest.php new file mode 100644 index 00000000..2e71154b --- /dev/null +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/PushCommandTest.php @@ -0,0 +1,65 @@ +application = new Application(); + $this->command = $this->application->get('push'); + } + + public function testItExecutesSuccessfully(): void + { + $commandTester = new CommandTester($this->command); + + $commandTester->execute([ + '--dry-run' => true, + '-vvv' => true, + ]); + + $commandTester->assertCommandIsSuccessful(); + } +} diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/ReleaseCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/ReleaseCommandTest.php new file mode 100644 index 00000000..0220aab7 --- /dev/null +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/ReleaseCommandTest.php @@ -0,0 +1,72 @@ +application = new Application(); + $this->command = $this->application->get('release'); + } + + public function testItsNameIsCorrect(): void + { + $commandTester = new CommandTester($this->command); + + $commandTester->execute([ + 'release' => 'patch', + '--dry-run' => true, + '-vvv' => true, + ]); + + $commandTester->assertCommandIsSuccessful(); + } +} diff --git a/src/SonsOfPHP/Bard/Tests/Console/Command/SplitCommandTest.php b/src/SonsOfPHP/Bard/Tests/Console/Command/SplitCommandTest.php new file mode 100644 index 00000000..0f65882c --- /dev/null +++ b/src/SonsOfPHP/Bard/Tests/Console/Command/SplitCommandTest.php @@ -0,0 +1,63 @@ +application = new Application(); + $this->command = $this->application->get('split'); + } + + public function testItsNameIsCorrect(): void + { + $commandTester = new CommandTester($this->command); + + $commandTester->execute([ + '--dry-run' => true, + '-vvv' => true, + ]); + + $commandTester->assertCommandIsSuccessful(); + } +} diff --git a/src/SonsOfPHP/Bard/src/Console/Command/CopyCommand.php b/src/SonsOfPHP/Bard/src/Console/Command/CopyCommand.php index 9401ca5f..efba21e6 100644 --- a/src/SonsOfPHP/Bard/src/Console/Command/CopyCommand.php +++ b/src/SonsOfPHP/Bard/src/Console/Command/CopyCommand.php @@ -10,7 +10,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Process\Process; /** @@ -24,26 +23,29 @@ protected function configure(): void ->setName('copy') ->setDescription('Copies a file to each package') ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry Run (Do not make any changes)') + ->addOption('overwrite', null, InputOption::VALUE_NONE, 'If file exists, overwrite it') ->addArgument('source', InputArgument::REQUIRED, 'Source file to copy') ->addArgument('package', InputArgument::OPTIONAL, 'Which package?') + ->addUsage('LICENSE') + ->addUsage('LICENSE sonsofphp/bard') + ->addUsage('--overwrite LICENSE sonsofphp/bard') ->setHelp( <<<'HELP' - The copy command will copy whatever file you give it to all the other - repositories it is managing. This is useful for LICENSE files. +The copy command will copy whatever file you give it to all the +other repositories it is managing. This is useful for LICENSE files. - Examples: +Examples: - %command.full_name% LICENSE + %command.full_name% LICENSE - Read more at https://docs.sonsofphp.com/bard/ - HELP +Read more at https://docs.sonsofphp.com/bard/ +HELP ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); $isDryRun = $input->getOption('dry-run'); // --- @@ -55,26 +57,64 @@ protected function execute(InputInterface $input, OutputInterface $output): int // --- // --- - $bardJsonFile = new JsonFile($input->getOption('working-dir') . '/bard.json'); - foreach ($bardJsonFile->getSection('packages') as $pkg) { - $pkgComposerFile = realpath($input->getOption('working-dir') . '/' . $pkg['path'] . '/composer.json'); - $pkgComposerJsonFile = new JsonFile($pkgComposerFile); - $pkgName = $pkgComposerJsonFile->getSection('name'); + foreach ($this->bardConfig->getSection('packages') as $pkg) { + $pkgPath = realpath($input->getOption('working-dir') . '/' . $pkg['path']); + $pkgFile = new JsonFile($pkgPath . '/composer.json'); + $pkgName = $pkgFile->getSection('name'); if (null !== $input->getArgument('package') && $pkgName !== $input->getArgument('package')) { continue; } - $process = new Process(['cp', $sourceFile, $pkg['path']]); - $io->text($process->getCommandLine()); - if (!$isDryRun) { - $this->getHelper('process')->run($output, $process); + $doesFileExists = file_exists($pkgPath . '/' . $input->getArgument('source')); + + if ( + (($doesFileExists && true === $input->getOption('overwrite')) || false === $doesFileExists) + && !$isDryRun + ) { + $process = new Process(['cp', $sourceFile, $pkgPath]); + //$worker = (new CopyFileWorker($source))->apply($pkg['path']); + $this->getProcessHelper()->run($output, $process); } + + $message = match ($doesFileExists) { + true => match ($input->getOption('overwrite')) { + true => sprintf( + 'Updated "%s/%s"', + $pkg['path'], + $input->getArgument('source'), + ), + false => sprintf( + 'File Exists "%s/%s" and has not been updated', + $pkg['path'], + $input->getArgument('source'), + ), + }, + false => sprintf( + 'Copied "%s" to "%s"', + $input->getArgument('source'), + $pkg['path'] + ), + }; + + $style = match ($doesFileExists) { + false => 'fg=white', + true => match ($input->getOption('overwrite')) { + false => 'fg=red', + true => 'fg=green', + }, + }; + + $this->bardStyle->text($this->getFormatterHelper()->formatSection($pkgName, $message, $style)); } // --- - $io->success(sprintf('File "%s" has been copied to all managed repos.', $sourceFile)); + $this->bardStyle->success(sprintf('File "%s" has been copied to all managed repos.', $sourceFile)); + + if ($isDryRun) { + $this->bardStyle->info('Dry-run enabled, nothing was modified'); + } return self::SUCCESS; }