|
3 | 3 | namespace IonBazan\ComposerDiff\Command; |
4 | 4 |
|
5 | 5 | use Composer\Command\BaseCommand; |
| 6 | +use Composer\DependencyResolver\Operation\OperationInterface; |
| 7 | +use Composer\DependencyResolver\Operation\UpdateOperation; |
6 | 8 | use IonBazan\ComposerDiff\Formatter\Formatter; |
7 | 9 | use IonBazan\ComposerDiff\Formatter\JsonFormatter; |
8 | 10 | use IonBazan\ComposerDiff\Formatter\MarkdownListFormatter; |
|
16 | 18 |
|
17 | 19 | class DiffCommand extends BaseCommand |
18 | 20 | { |
| 21 | + const CHANGES_PROD = 2; |
| 22 | + const CHANGES_DEV = 4; |
| 23 | + const DOWNGRADES_PROD = 8; |
| 24 | + const DOWNGRADES_DEV = 16; |
19 | 25 | /** |
20 | 26 | * @var PackageDiff |
21 | 27 | */ |
@@ -54,43 +60,61 @@ protected function configure() |
54 | 60 | ->addOption('with-links', 'l', InputOption::VALUE_NONE, 'Include compare/release URLs') |
55 | 61 | ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist, json)', 'mdtable') |
56 | 62 | ->addOption('gitlab-domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Extra Gitlab domains (inherited from Composer config by default)', array()) |
| 63 | + ->addOption('strict', 's', InputOption::VALUE_NONE, 'Return non-zero exit code if there are any changes') |
57 | 64 | ->setHelp(<<<'EOF' |
58 | | -The <comment>%command.name%</comment> command displays all dependency changes between two <info>composer.lock</info> files. |
59 | | -By default, it will compare current filesystem changes with git <info>HEAD</info>: |
| 65 | +The <info>%command.name%</info> command displays all dependency changes between two <comment>composer.lock</comment> files. |
60 | 66 |
|
61 | | - <comment>%command.full_name%</comment> |
| 67 | +By default, it will compare current filesystem changes with git <comment>HEAD</comment>: |
| 68 | +
|
| 69 | + <info>%command.full_name%</info> |
62 | 70 |
|
63 | 71 | To compare with specific branch, pass its name as argument: |
64 | 72 |
|
65 | | - <comment>%command.full_name% master</comment> |
| 73 | + <info>%command.full_name% master</info> |
66 | 74 | |
67 | 75 | You can specify any valid git refs to compare with: |
68 | 76 |
|
69 | | - <comment>%command.full_name% HEAD~3 be4aabc</comment> |
| 77 | + <info>%command.full_name% HEAD~3 be4aabc</info> |
70 | 78 | |
71 | 79 | You can also use more verbose syntax for <info>base</info> and <info>target</info> options: |
72 | 80 |
|
73 | | - <comment>%command.full_name% --base master --target composer.lock</comment> |
| 81 | + <info>%command.full_name% --base master --target composer.lock</info> |
74 | 82 | |
75 | 83 | To compare files in specific path, use following syntax: |
76 | 84 |
|
77 | | - <comment>%command.full_name% master:subdirectory/composer.lock /path/to/another/composer.lock</comment> |
| 85 | + <info>%command.full_name% master:subdirectory/composer.lock /path/to/another/composer.lock</info> |
78 | 86 | |
79 | | -By default, <info>platform</info> dependencies are hidden. Add <comment>--with-platform</comment> option to include them in the report: |
| 87 | +By default, <info>platform</info> dependencies are hidden. Add <info>--with-platform</info> option to include them in the report: |
80 | 88 | |
81 | | - <comment>%command.full_name% --with-platform</comment> |
| 89 | + <info>%command.full_name% --with-platform</info> |
82 | 90 |
|
83 | | -Use <comment>--with-links</comment> to include release and compare URLs in the report: |
| 91 | +Use <info>--with-links</info> to include release and compare URLs in the report: |
84 | 92 |
|
85 | | - <comment>%command.full_name% --with-links</comment> |
| 93 | + <info>%command.full_name% --with-links</info> |
86 | 94 | |
87 | | -You can customize output format by specifying it with <comment>--format</comment> option. Choose between <info>mdtable</info>, <info>mdlist</info> and <info>json</info>: |
| 95 | +You can customize output format by specifying it with <info>--format</info> option. Choose between <comment>mdtable</comment>, <comment>mdlist</comment> and <comment>json</comment>: |
| 96 | +
|
| 97 | + <info>%command.full_name% --format=json</info> |
| 98 | +
|
| 99 | +Hide <info>dev</info> dependencies using <info>--no-dev</info> option: |
| 100 | +
|
| 101 | + <info>%command.full_name% --no-dev</info> |
| 102 | +
|
| 103 | +Passing <info>--strict</info> option may help you to disallow changes or downgrades by returning non-zero exit code: |
| 104 | +
|
| 105 | + <info>%command.full_name% --strict</info> |
88 | 106 |
|
89 | | - <comment>%command.full_name% --format=json</comment> |
| 107 | +Exit code |
| 108 | +--------- |
90 | 109 |
|
91 | | -Hide <info>dev</info> dependencies using <comment>--no-dev</comment> option: |
| 110 | +Exit code of the command is built using following bit flags: |
92 | 111 |
|
93 | | - <comment>%command.full_name% --no-dev</comment> |
| 112 | +* 0 - OK. |
| 113 | +* 1 - General error. |
| 114 | +* 2 - There were changes in prod packages. |
| 115 | +* 4 - There were changes is dev packages. |
| 116 | +* 8 - There were downgrades in prod packages. |
| 117 | +* 16 - There were downgrades in dev packages. |
94 | 118 | EOF |
95 | 119 | ) |
96 | 120 | ; |
@@ -122,7 +146,50 @@ protected function execute(InputInterface $input, OutputInterface $output) |
122 | 146 |
|
123 | 147 | $formatter->render($prodOperations, $devOperations, $withUrls); |
124 | 148 |
|
125 | | - return 0; |
| 149 | + return $input->getOption('strict') ? $this->getExitCode($prodOperations, $devOperations) : 0; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param OperationInterface[] $prodOperations |
| 154 | + * @param OperationInterface[] $devOperations |
| 155 | + * |
| 156 | + * @return int Exit code |
| 157 | + */ |
| 158 | + private function getExitCode(array $prodOperations, array $devOperations) |
| 159 | + { |
| 160 | + $exitCode = 0; |
| 161 | + |
| 162 | + if (!empty($prodOperations)) { |
| 163 | + $exitCode = self::CHANGES_PROD; |
| 164 | + |
| 165 | + if ($this->hasDowngrades($prodOperations)) { |
| 166 | + $exitCode |= self::DOWNGRADES_PROD; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (!empty($devOperations)) { |
| 171 | + $exitCode |= self::CHANGES_DEV; |
| 172 | + |
| 173 | + if ($this->hasDowngrades($devOperations)) { |
| 174 | + $exitCode |= self::DOWNGRADES_DEV; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return $exitCode; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param OperationInterface[] $operations |
| 183 | + * |
| 184 | + * @return bool |
| 185 | + */ |
| 186 | + private function hasDowngrades(array $operations) |
| 187 | + { |
| 188 | + $downgrades = array_filter($operations, function (OperationInterface $operation) { |
| 189 | + return $operation instanceof UpdateOperation && !PackageDiff::isUpgrade($operation); |
| 190 | + }); |
| 191 | + |
| 192 | + return !empty($downgrades); |
126 | 193 | } |
127 | 194 |
|
128 | 195 | /** |
|
0 commit comments