-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathAssertBackwardsCompatible.php
242 lines (201 loc) · 8.97 KB
/
AssertBackwardsCompatible.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
declare(strict_types=1);
namespace Roave\BackwardCompatibility\Command;
use Psl;
use Psl\Env;
use Psl\Iter;
use Psl\Str;
use Psl\Type;
use Roave\BackwardCompatibility\Changes;
use Roave\BackwardCompatibility\CompareApi;
use Roave\BackwardCompatibility\Factory\ComposerInstallationReflectorFactory;
use Roave\BackwardCompatibility\Formatter\GithubActionsFormatter;
use Roave\BackwardCompatibility\Formatter\MarkdownPipedToSymfonyConsoleFormatter;
use Roave\BackwardCompatibility\Formatter\SymfonyConsoleTextFormatter;
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
use Roave\BackwardCompatibility\Git\GetVersionCollection;
use Roave\BackwardCompatibility\Git\ParseRevision;
use Roave\BackwardCompatibility\Git\PerformCheckoutOfRevision;
use Roave\BackwardCompatibility\Git\PickVersionFromVersionCollection;
use Roave\BackwardCompatibility\Git\Revision;
use Roave\BackwardCompatibility\LocateDependencies\LocateDependencies;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class AssertBackwardsCompatible extends Command
{
private PerformCheckoutOfRevision $git;
private ComposerInstallationReflectorFactory $makeComposerInstallationReflector;
private ParseRevision $parseRevision;
private GetVersionCollection $getVersions;
private PickVersionFromVersionCollection $pickFromVersion;
private LocateDependencies $locateDependencies;
private CompareApi $compareApi;
/**
* @throws LogicException
*/
public function __construct(
PerformCheckoutOfRevision $git,
ComposerInstallationReflectorFactory $makeComposerInstallationReflector,
ParseRevision $parseRevision,
GetVersionCollection $getVersions,
PickVersionFromVersionCollection $pickFromVersion,
LocateDependencies $locateDependencies,
CompareApi $compareApi
) {
parent::__construct();
$this->git = $git;
$this->makeComposerInstallationReflector = $makeComposerInstallationReflector;
$this->parseRevision = $parseRevision;
$this->getVersions = $getVersions;
$this->pickFromVersion = $pickFromVersion;
$this->locateDependencies = $locateDependencies;
$this->compareApi = $compareApi;
}
/**
* @throws InvalidArgumentException
*/
protected function configure(): void
{
$this
->setName('roave-backwards-compatibility-check:assert-backwards-compatible')
->setDescription('Verifies that the revision being compared with "from" does not introduce any BC (backwards-incompatible) changes')
->addOption(
'from',
null,
InputOption::VALUE_OPTIONAL,
'Git reference for the base version of the library, which is considered "stable"'
)
->addOption(
'to',
null,
InputOption::VALUE_REQUIRED,
'Git reference for the new version of the library, which is verified against "from" for BC breaks',
'HEAD'
)
->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Currently supports "console", "markdown" or "github-actions"',
['console']
)
->addOption(
'install-development-dependencies',
null,
InputOption::VALUE_NONE,
'Whether to also install "require-dev" dependencies too'
)
->addUsage(
<<<'USAGE'
Without arguments, this command will attempt to detect the
latest stable git tag ("release", according to this tool)
of the repository in your CWD (current working directory),
and will use it as baseline for the defined API.
It will then create two clones of the repository: one at
the "release" version, and one at the version specified
via `--to` ("HEAD" by default).
It will then install all required dependencies in both copies
and compare the APIs, looking for breaking changes in the
defined "autoload" paths in your `composer.json` definition.
Once completed, it will print out the results to `STDERR`
and terminate with `3` if breaking changes were detected.
If you want to produce `STDOUT` output, then please use the
`--format` flag.
USAGE
);
}
/**
* @throws InvalidArgumentException
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$output = Type\object(ConsoleOutputInterface::class)->assert($output);
$stdErr = $output->getErrorOutput();
// @todo fix flaky assumption about the path of the source repo...
$sourceRepo = CheckedOutRepository::fromPath(Env\current_dir());
$fromRevision = $input->getOption('from') !== null
? $this->parseRevisionFromInput($input, $sourceRepo)
: $this->determineFromRevisionFromRepository($sourceRepo, $stdErr);
$to = Type\string()->coerce($input->getOption('to'));
$includeDevelopmentDependencies = Type\bool()->coerce($input->getOption('install-development-dependencies'));
$toRevision = $this->parseRevision->fromStringForRepository($to, $sourceRepo);
$stdErr->writeln(Str\format(
'Comparing from %s to %s...',
Type\string()->coerce($fromRevision),
Type\string()->coerce($toRevision)
));
$fromPath = $this->git->checkout($sourceRepo, $fromRevision);
$toPath = $this->git->checkout($sourceRepo, $toRevision);
try {
$changes = ($this->compareApi)(
($this->makeComposerInstallationReflector)(
$fromPath->__toString(),
new AggregateSourceLocator() // no dependencies
),
($this->makeComposerInstallationReflector)(
$fromPath->__toString(),
($this->locateDependencies)($fromPath->__toString(), $includeDevelopmentDependencies)
),
($this->makeComposerInstallationReflector)(
$toPath->__toString(),
($this->locateDependencies)($toPath->__toString(), $includeDevelopmentDependencies)
)
);
$formatters = [
'console' => new SymfonyConsoleTextFormatter($stdErr),
'markdown' => new MarkdownPipedToSymfonyConsoleFormatter($output),
'github-actions' => new GithubActionsFormatter($output, $toPath),
];
foreach (
Type\vec(Type\union(
Type\literal_scalar('console'),
Type\literal_scalar('markdown'),
Type\literal_scalar('github-actions'),
))->coerce((array) $input->getOption('format')) as $format
) {
$formatters[$format]->write($changes);
}
} finally {
$this->git->remove($fromPath);
$this->git->remove($toPath);
}
return $this->printOutcomeAndExit($changes, $stdErr);
}
private function printOutcomeAndExit(Changes $changes, OutputInterface $stdErr): int
{
$hasBcBreaks = Iter\count($changes);
if ($hasBcBreaks) {
$stdErr->writeln(Str\format('<error>%s backwards-incompatible changes detected</error>', $hasBcBreaks));
} else {
$stdErr->writeln('<info>No backwards-incompatible changes detected</info>', $hasBcBreaks);
}
return $hasBcBreaks ? 3 : 0;
}
/**
* @throws InvalidArgumentException
*/
private function parseRevisionFromInput(InputInterface $input, CheckedOutRepository $repository): Revision
{
$from = Type\string()->coerce($input->getOption('from'));
return $this->parseRevision->fromStringForRepository($from, $repository);
}
private function determineFromRevisionFromRepository(
CheckedOutRepository $repository,
OutputInterface $output
): Revision {
$versions = $this->getVersions->fromRepository($repository);
Psl\invariant(Iter\count($versions) >= 1, 'Could not detect any released versions for the given repository');
$versionString = $this->pickFromVersion->forVersions($versions)->toString();
$output->writeln(Str\format('Detected last minor version: %s', $versionString));
return $this->parseRevision->fromStringForRepository(
$versionString,
$repository
);
}
}