-
Notifications
You must be signed in to change notification settings - Fork 0
/
console
executable file
·420 lines (357 loc) · 13.8 KB
/
console
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env php
<?php
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
use Github\AuthMethod;
use Github\Client;
use Github\ResultPager;
use GuzzleHttp\Client as GuzzleClient;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Silly\Application;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
require_once __DIR__ . '/vendor/autoload.php';
if (file_exists(__DIR__ . '/.env')) {
(new \Symfony\Component\Dotenv\Dotenv())->load(__DIR__ . '/.env');
}
const TEMP_DIR = __DIR__ . '/temp';
const BUILD_DIR = __DIR__ . '/build';
const OVERLAY_DIR = __DIR__ . '/overlay';
const KEYRING_FILE = __DIR__ . '/.gpgkeyring';
const MATOMO_KEYS = [
'F529A27008477483777FC23D63BB30D0E5D2C749',
'814E346FA01A20DBB04B6807B5DBD5925590A237',
];
function http(): Psr\Http\Client\ClientInterface
{
static $http = new GuzzleClient();
return $http;
}
function request(string $method, \Psr\Http\Message\UriInterface|string $uri): RequestInterface
{
static $factory = new \Http\Factory\Guzzle\RequestFactory();
return $factory->createRequest($method, $uri)->withAddedHeader('x-matomo-release-repository', '1')->withAddedHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36');
}
function stream(): \Psr\Http\Message\StreamFactoryInterface
{
static $stream = new \Http\Factory\Guzzle\StreamFactory();
return $stream;
}
function fs(): \Symfony\Component\Filesystem\Filesystem
{
static $filesystem = new \Symfony\Component\Filesystem\Filesystem();
return $filesystem;
}
function github(): Client
{
static $client = Client::createWithHttpClient(http());
$token = $_ENV['GITHUB_TOKEN'] ?? null;
if ($token) {
$client->authenticate($token, authMethod: AuthMethod::ACCESS_TOKEN);
}
return $client;
}
function tag_exists(string $directory, string $tag): bool
{
$process = new Process(['git', 'tag', '-l', $tag], $directory);
$process->run();
return trim($process->getOutput()) === $tag;
}
function create_tag(string $unzipDir, string $tag): void
{
(new Process(['git', 'config', 'user.email', '[email protected]'], BUILD_DIR))->mustRun();
(new Process(['git', 'config', 'user.name', 'PortlandLabs Matomo Builder'], BUILD_DIR))->mustRun();
(new Process(['git', 'checkout', '--orphan', $tag], BUILD_DIR))->mustRun();
(new Process(['git', 'reset', '.', $tag], BUILD_DIR))->mustRun();
(new Process(['git', 'clean', '-fd', $tag], BUILD_DIR))->mustRun();
fs()->remove(Finder::create()->in(BUILD_DIR)->exclude('.git')->ignoreDotFiles(false)->depth(0));
fs()->mirror($unzipDir, BUILD_DIR);
(new Process(['git', 'add', '--all'], BUILD_DIR))->mustRun();
(new Process(['git', 'commit', '-m', "Release {$tag}"], BUILD_DIR))->mustRun();
(new Process(['git', 'tag', $tag], BUILD_DIR))->mustRun();
}
function valid_signature(string $asc, string $file): bool
{
$key = signing_key($asc, $file);
return $key !== null && in_array(strtoupper($key), MATOMO_KEYS, true);
}
function signing_key(string $asc, string $file): ?string
{
$process = new Symfony\Component\Process\Process([
'gpg',
'--no-default-keyring',
'--keyring',
KEYRING_FILE,
'--verify',
realpath($asc),
realpath($file),
]);
$process->mustRun();
$output = $process->getOutput() . $process->getErrorOutput();
if (!str_contains($output, 'gpg: Good signature from ')) {
return null;
}
if (!preg_match('/Primary key fingerprint: ([a-fA-F0-9 ]+)/', $output, $matches)) {
return null;
}
return str_replace(' ', '', $matches[1]);
}
function download(RequestInterface $request, string $sink, int $chunkSize = 1024000, ?callable $validate = null, ?SymfonyStyle $io = null, ?ClientInterface $client = null): void
{
$io?->writeln(' Downloading ' . basename($sink) . '...');
$client ??= http();
try {
$response = $client->sendRequest($request);
} catch (ClientExceptionInterface $e) {
throw new \RuntimeException('Unable to send request.');
}
// Validate the status code
$statusCode = (string) $response->getStatusCode();
match ((int) $statusCode[0]) {
4, 5 => throw new \RuntimeException($response->getReasonPhrase(), $statusCode),
2 => null,
default => throw new RuntimeException("Unexpected response code {$statusCode}: {$response->getReasonPhrase()}", $statusCode),
};
if ($validate !== null && !$validate($response)) {
throw new \RuntimeException("Response didn't pass validation.");
}
// Determine the total length
$body = $response->getBody();
$output = stream()->createStreamFromFile($sink, 'w+');
while (!$body->eof() && $chunk = $body->read($chunkSize)) {
$output->write($chunk);
}
}
function unzip(string $zip, string $to)
{
(new Process([
'unzip',
$zip,
'-d',
$to,
]))->mustRun();
}
function parse_manifest(string $file): array
{
$parser = (new \PhpParser\ParserFactory())->createForNewestSupportedVersion();
try {
$ast = $parser->parse(file_get_contents($file));
} catch (Error $error) {
throw new \RuntimeException('Unable to parse manifest file.');
}
$manifest = [];
assert($ast[0] instanceof \PhpParser\Node\Stmt\Namespace_);
$class = $ast[0]->stmts[0] ?? null;
assert($class instanceof \PhpParser\Node\Stmt\Class_);
$files = $class->getProperty('files')->props[0]->default ?? null;
assert($files instanceof \PhpParser\Node\Expr\Array_);
foreach ($files->items as $arrayItem) {
assert($arrayItem->key instanceof \PhpParser\Node\Scalar\String_);
$key = $arrayItem->key->value;
$data = [];
assert($arrayItem->value instanceof \PhpParser\Node\Expr\Array_);
foreach ($arrayItem->value->items as $arrayItemValueItem) {
assert($arrayItemValueItem->value instanceof \PhpParser\Node\Scalar\String_);
$data[] = $arrayItemValueItem->value->value;
}
$manifest[$key] = $data;
}
return $manifest;
}
function build_manifest(array $manifest, bool $shortArrays): string
{
$parser = (new \PhpParser\ParserFactory())->createForNewestSupportedVersion();
try {
$ast = $parser->parse(file_get_contents(__DIR__ . '/overlay/manifest.inc.php'));
$tokens = $parser->getTokens();
} catch (Error $error) {
throw new \RuntimeException('Unable to parse manifest template.');
}
$manifestNodes = (new \PhpParser\BuilderFactory())->val($manifest);
$traverser = new NodeTraverser();
$traverser->addVisitor(new class ($manifestNodes) extends NodeVisitorAbstract {
public function __construct(public $manifest) {}
public function enterNode(Node $node)
{
if (!$node instanceof Node\PropertyItem) {
return;
}
$node->default = $this->manifest;
}
});
$printer = (new \PortlandLabs\Mpl\Printer(['shortArraySyntax' => $shortArrays]));
return $printer->printFormatPreserving($traverser->traverse($ast), $ast, $tokens);
}
$app = new Application();
$app->command('build [constraint]', function (SymfonyStyle $io, ?string $constraint) {
fs()->remove(TEMP_DIR);
fs()->mkdir(TEMP_DIR);
$versions = (new ResultPager(github(), 100))->fetchAllLazy(github()->repo()->releases(), 'all', [
'matomo-org', 'matomo',
]);
$versionParser = new VersionParser();
$tags = [];
foreach ($versions as $release) {
try {
$version = $release['name'];
$normalVersion = $versionParser->normalize($release['name']);
if ($constraint !== null && !Semver::satisfies($normalVersion, $constraint)) {
continue;
}
if ($versionParser::parseStability($normalVersion) === 'stable') {
$tags[$normalVersion] = $version;
}
} catch (UnexpectedValueException $e) {
// Ignore
}
}
$skip = [
'2.2.1' => 'Invalid config/manifest.inc.php file in release',
'2.18.1' => 'Release zip doesn\'t exist',
'3.8.1' => 'Invalid composer.json https://github.com/matomo-org/matomo/blob/3.8.1/composer.json#L31C10-L31C17',
];
$sorted = array_map(fn($k) => $tags[$k], Semver::sort(array_keys($tags)));
foreach ($sorted as $tag) {
$io->writeln("<info>Processing {$tag}</>");
if (tag_exists(BUILD_DIR, $tag)) {
$io->writeln(' <comment>Tag exists.</comment>');
continue;
}
if (isset($skip[$tag])) {
$io->writeln(" <comment>Skipping: {$skip[$tag]}</comment>");
continue;
}
// Download and unzip the release from matomo
$type = Semver::satisfies($tag, '>=3.5') ? 'matomo' : 'piwik';
$baseDir = TEMP_DIR . "/{$tag}";
$name = "{$type}-{$tag}.zip";
$file = "{$baseDir}/matomo.zip";
$asc = "{$file}.asc";
$unzip = "{$baseDir}/unzip";
$composerJson = "{$baseDir}/composer.json";
fs()->mkdir([$baseDir, $unzip]);
fs()->touch([$file, $asc, $composerJson]);
fs()->dumpFile($composerJson, '{}');
// Download the composer.json and zip release
try {
download(request('GET', "https://raw.githubusercontent.com/matomo-org/matomo/refs/tags/{$tag}/composer.json"), $composerJson, io: $io);
} catch (\RuntimeException $e) {
$io->writeln(" <error>Unable to download composer.json: {$e->getMessage()}</error>");
continue;
}
try {
download(request('GET', "https://builds.matomo.org/{$name}"), $file, io: $io);
} catch (\RuntimeException $e) {
$io->writeln(" <error>Unable to download matomo: {$e->getMessage()}</error>");
continue;
}
if (Semver::satisfies($tag, '>=2.9')) {
try {
download(request('GET', "https://builds.matomo.org/{$name}.asc"), $asc, io: $io);
} catch (\RuntimeException $e) {
$io->writeln(" <error>Unable to download signature: {$e->getMessage()}</error>");
continue;
}
if (!valid_signature($asc, $file)) {
$io->writeln(" <error>Invalid signature</error>");
continue;
}
}
// Unzip
try {
$io->writeln(' Unzipping...');
unzip($file, $unzip);
} catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
$io->writeln(' <error>Unable to unzip</error>');
continue;
}
// Normalize
$io->writeln(' Normalizing...');
try {
$matomo = $unzip . '/matomo';
if (fs()->exists($unzip . '/piwik')) {
fs()->rename($unzip . '/piwik', $matomo);
}
fs()->remove([$matomo . '/vendor', $matomo . '/composer.lock']);
// Update readme
fs()->dumpFile($matomo . '/README.md', implode("\n\n------\n\n", [
fs()->readFile(OVERLAY_DIR . '/README.md'),
fs()->readFile($matomo . '/README.md'),
]));
// Update composer.json
$decoded = json_decode(fs()->readFile($composerJson), true, 512, JSON_THROW_ON_ERROR);
$composer = [
...$decoded,
'name' => 'mpl/matomo',
'type' => 'mpl-matomo',
'replace' => [
...($decoded['replace'] ?? []),
'matomo/matomo' => 'self.version',
],
'_comment' => [
...($decoded['_comment'] ?? []),
'Composer based build by PortlandLabs',
],
];
fs()->dumpFile($matomo . '/composer.json', json_encode($composer, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
} catch (\Throwable $e) {
$io->writeln(' <error>Unable to normalize</error>');
continue;
}
// Update manifest
$removeFiles = fn($file) => (str_starts_with($file, 'vendor/') || $file === 'composer.lock');
$rehashFiles = ['composer.json', 'README.md'];
try {
$manifest = parse_manifest($matomo . '/config/manifest.inc.php');
} catch (\Throwable $e) {
$io->writeln(' <error>Unable to parse manifest</error>');
continue;
}
foreach (array_keys($manifest) as $key) {
if ($removeFiles($key)) {
unset($manifest[$key]);
}
}
foreach ($rehashFiles as $key) {
$manifest[$key] = [
(string) filesize($matomo . '/' . $key),
(string) md5_file($matomo . '/' . $key),
];
}
try {
fs()->dumpFile($matomo . '/config/manifest.inc.php', build_manifest($manifest, false));
} catch (\Throwable $e) {
$io->writeln(' <error>Unable to write manifest</error>');
continue;
}
// Build new tag
$io->writeln(' Tagging...');
try {
create_tag($matomo, $tag);
} catch (\Throwable $e) {
$io->writeln(' <error>Unable to create tag</error>');
continue;
}
}
return 0;
});
$app->command('keys', function () {
(new Process([
'gpg',
'--no-default-keyring',
'--keyring',
KEYRING_FILE,
'--keyserver',
'hkps://keyserver.ubuntu.com',
'--recv-keys',
...MATOMO_KEYS,
]))->mustRun();
return 0;
});
$app->run();