-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.php
70 lines (65 loc) · 2.36 KB
/
build.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
<?php
namespace JPB;
use CzProject\GitPhp\GitRepository;
use GuzzleHttp\Client;
use Throwable;
require_once __DIR__ . '/vendor/autoload.php';
$git = new GitRepository(__DIR__);
try {
$tags = $git->getTags() ?? [];
} catch (Throwable) {
$tags = [];
}
$releases = getGithubJson('repos/wp-cli/wp-cli/releases');
if (!is_array($releases)) {
echo "Could not find any releases!\n";
exit(1);
}
collect($releases)
->reverse()
->filter(fn($release) => !in_array($release->tag_name, $tags))
->map(function ($release) {
$assets = collect($release->assets);
$phar = $assets->first(fn($asset) => str_ends_with($asset->browser_download_url, '.phar'))->browser_download_url ?? false;
$sig = $assets->first(fn($asset) => str_ends_with($asset->browser_download_url, '.sha512'))->browser_download_url ?? false;
$tag = $release->tag_name;
return ($phar && $sig) ? (object)compact('phar', 'sig', 'tag') : false;
})
->filter()
->each(function ($item) use ($git) {
$pharFile = __DIR__ . DIRECTORY_SEPARATOR . 'wp';
$sigFile = __DIR__ . DIRECTORY_SEPARATOR . 'wp-cli.phar.sha512';
try {
$client = new Client();
echo "Downloading $item->tag phar...\n";
$client->get($item->phar, ['sink' => $pharFile]);
echo "Downloading $item->tag signature...\n";
$client->get($item->sig, ['sink' => $sigFile]);
echo "Verifying $item->tag phar...\n";
$signature = trim(file_get_contents($sigFile));
$hash = hash_file('sha512', $pharFile);
if ($hash !== $signature) {
echo "Signature verification failed! Not committing!\n";
$git->execute('checkout', '--', $pharFile);
} else {
echo "Signature verified. Committing new phar...\n";
$git->addFile($pharFile);
$git->commit("Add $item->tag phar");
$git->createTag($item->tag);
}
} catch (Throwable) {
}
});
function getGithubJson($endpoint)
{
static $client;
if (!$client) {
$client = new Client(['base_uri' => 'https://api.github.com']);
}
try {
$response = $client->get($endpoint);
$content = $response->getBody()->getContents();
} catch (Throwable) {
}
return json_decode($content ?? 'null');
}