-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate-packagist-versions.php
185 lines (139 loc) · 4.35 KB
/
generate-packagist-versions.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
<?php
function downloadPackagistDb(): string
{
$url = 'https://osv-vulnerabilities.storage.googleapis.com/Packagist/all.zip';
$file = './packagist-db.zip';
$current = file_get_contents($url);
file_put_contents($file, $current);
return $file;
}
/**
* @throws RuntimeException
*/
function openDbZip(string $path): ZipArchive
{
$zip = new ZipArchive();
if ($zip->open($path, ZipArchive::RDONLY) === false) {
throw new RuntimeException('failed to read zip archive');
}
return $zip;
}
/**
* @throws JsonException
* @throws RuntimeException
*/
function fetchPackageVersions(): array
{
$dbPath = downloadPackagistDb();
$dbZip = openDbZip($dbPath);
$osvs = [];
for ($i = 0; $i < $dbZip->numFiles; $i++) {
$file = $dbZip->getFromIndex($i);
if ($file === false) {
throw new RuntimeException('failed to read a file from db zip');
}
$osvs[] = json_decode($file, true, 512, JSON_THROW_ON_ERROR);
}
$packages = [];
foreach ($osvs as $osv) {
foreach ($osv['affected'] as $affected) {
$package = $affected['package']['name'];
if (!isset($packages[$package])) {
$packages[$package] = [];
}
if (empty($affected['versions'])) {
continue;
}
foreach ($affected['versions'] as $version) {
$packages[$package][] = $version;
}
}
}
return array_map(static function ($versions) {
$uniq = array_unique($versions);
usort($uniq, static fn($a, $b) => version_compare(ltrim($a, "vV"), ltrim($b, "vV")));
return $uniq;
}, $packages);
}
/**
* Normalizes the previous version such that it will compare "correctly" to the current version,
* by ensuring that they both have the same "v" prefix (or lack of).
*
* Whether the "v" prefix is present on the normalized previous version depends on
* its presences in the current version; this ensure we will have _some_ versions that
* do have the "v" prefix, rather than it being present on _none_ or _all_ versions.
*
* @param string $currentVersion
* @param string $previousVersion
*
* @return string
*/
function normalizePrevVersion(string $currentVersion, string $previousVersion): string
{
if (str_starts_with($currentVersion, "v")) {
$previousVersion = ltrim($previousVersion, "vV");
return "v$previousVersion";
}
if (str_starts_with($currentVersion, "V")) {
$previousVersion = ltrim($previousVersion, "vV");
return "V$previousVersion";
}
return ltrim($previousVersion, "vV");
}
function generateVersionCompares(array $versions): array
{
$comparisons = [];
foreach ($versions as $index => $version) {
if ($index === 0) {
continue;
}
$prevVersion = normalizePrevVersion($version, $versions[$index - 1]);
$op = version_compare($prevVersion, $version) === 0 ? "=" : "<";
$comparisons[] = "$prevVersion $op $version";
}
return $comparisons;
}
function generatePackageCompares(array $packages): array
{
$comparisons = [];
foreach ($packages as $versions) {
$comparisons[] = generateVersionCompares($versions);
}
return array_merge(...$comparisons);
}
function compareVersions(array $lines, string $select = "all"): bool
{
$hasAnyFailed = false;
foreach ($lines as $line) {
$line = trim($line);
if (empty($line) || str_starts_with($line, "#") || str_starts_with($line, "//")) {
continue;
}
[$v1, $op, $v2] = explode(" ", $line);
$r = version_compare($v1, $v2, $op);
if (!$r) {
$hasAnyFailed = true;
}
if ($select === "failures" && $r === true) {
continue;
}
if ($select === "successes" && $r !== true) {
continue;
}
$color = $r ? "\033[92m" : "\033[91m";
$rs = $r ? "T" : "F";
echo "$color$rs\033[0m: \033[93m$line\033[0m\n";
}
return $hasAnyFailed;
}
$outfile = "pkg/semantic/fixtures/packagist-versions-generated.txt";
/** @noinspection PhpUnhandledExceptionInspection */
$packages = fetchPackageVersions();
file_put_contents($outfile, implode("\n", array_unique(generatePackageCompares($packages))) . "\n");
// set this to either "failures" or "successes" to only have those comparison results
// printed; setting it to anything else will have all comparison results printed
$show = getenv("VERSION_GENERATOR_PRINT") ?: "failures";
$didAnyFail = compareVersions(explode("\n", file_get_contents($outfile)), $show);
if ($didAnyFail === true) {
exit(1);
}