|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace IonBazan\ComposerDiff\Command; |
| 4 | + |
| 5 | +use Composer\Command\BaseCommand; |
| 6 | +use Composer\DependencyResolver\Operation\InstallOperation; |
| 7 | +use Composer\DependencyResolver\Operation\OperationInterface; |
| 8 | +use Composer\DependencyResolver\Operation\UninstallOperation; |
| 9 | +use Composer\DependencyResolver\Operation\UpdateOperation; |
| 10 | +use IonBazan\ComposerDiff\MarkdownTable; |
| 11 | +use IonBazan\ComposerDiff\PackageDiff; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | + |
| 16 | +class DiffCommand extends BaseCommand |
| 17 | +{ |
| 18 | + protected function configure() |
| 19 | + { |
| 20 | + $this->setName('diff') |
| 21 | + ->setDescription('Displays package diff') |
| 22 | + ->addOption('base', 'b', InputOption::VALUE_REQUIRED, 'Base composer.lock file path or git ref', 'HEAD:composer.lock') |
| 23 | + ->addOption('target', 't', InputOption::VALUE_REQUIRED, 'Target composer.lock file path or git ref', 'composer.lock') |
| 24 | + ->addOption('no-dev', null, InputOption::VALUE_NONE, 'Ignore dev dependencies') |
| 25 | + ->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies') |
| 26 | + ->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)') |
| 27 | + ; |
| 28 | + } |
| 29 | + |
| 30 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 31 | + { |
| 32 | + $base = $input->getOption('base'); |
| 33 | + $target = $input->getOption('target'); |
| 34 | + $withPlatform = $input->getOption('with-platform'); |
| 35 | + $diff = new PackageDiff(); |
| 36 | + |
| 37 | + if (!$input->getOption('no-prod')) { |
| 38 | + $operations = $diff->getPackageDiff($base, $target, false, $withPlatform); |
| 39 | + $this->displayTable($operations, 'Prod Packages', $output); |
| 40 | + } |
| 41 | + |
| 42 | + if (!$input->getOption('no-dev')) { |
| 43 | + $operations = $diff->getPackageDiff($base, $target, true, $withPlatform); |
| 44 | + $this->displayTable($operations, 'Dev Packages', $output); |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + protected function displayTable(array $operations, $header, OutputInterface $output) |
| 51 | + { |
| 52 | + $table = new MarkdownTable($output); |
| 53 | + |
| 54 | + foreach ($operations as $operation) { |
| 55 | + $table->addRow($this->getTableRow($operation)); |
| 56 | + } |
| 57 | + |
| 58 | + $table->setHeaders(array($header, 'Base', 'Target'))->render(); |
| 59 | + $output->writeln(''); |
| 60 | + } |
| 61 | + |
| 62 | + protected function getTableRow(OperationInterface $operation) |
| 63 | + { |
| 64 | + if ($operation instanceof InstallOperation) { |
| 65 | + return array( |
| 66 | + $operation->getPackage()->getName(), |
| 67 | + 'New', |
| 68 | + $operation->getPackage()->getFullPrettyVersion(), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if ($operation instanceof UpdateOperation) { |
| 73 | + return array( |
| 74 | + $operation->getInitialPackage()->getName(), |
| 75 | + $operation->getInitialPackage()->getFullPrettyVersion(), |
| 76 | + $operation->getTargetPackage()->getFullPrettyVersion(), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + if ($operation instanceof UninstallOperation) { |
| 81 | + return array( |
| 82 | + $operation->getPackage()->getName(), |
| 83 | + $operation->getPackage()->getFullPrettyVersion(), |
| 84 | + 'Removed', |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + throw new \InvalidArgumentException('Invalid operation'); |
| 89 | + } |
| 90 | +} |
0 commit comments