-
Notifications
You must be signed in to change notification settings - Fork 42
/
Downloader.php
91 lines (74 loc) · 3.19 KB
/
Downloader.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
<?php
declare(strict_types=1);
namespace Temporal\Testing;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class Downloader
{
public const TAG_LATEST = 'latest';
private const JAVA_SDK_URL_RELEASES = 'https://api.github.com/repos/temporalio/sdk-java/releases/';
private Filesystem $filesystem;
private HttpClientInterface $httpClient;
private string $javaSdkUrl;
public function __construct(
Filesystem $filesystem,
HttpClientInterface $httpClient,
string $javaSdkVersion = self::TAG_LATEST,
) {
$this->filesystem = $filesystem;
$this->httpClient = $httpClient;
$this->javaSdkUrl = self::JAVA_SDK_URL_RELEASES . match (true) {
$javaSdkVersion === self::TAG_LATEST => self::TAG_LATEST,
default => "tags/$javaSdkVersion",
};
}
private function findAsset(array $assets, string $systemPlatform, string $systemArch): array
{
foreach ($assets as $asset) {
preg_match('/^temporal-test-server_[^_]+_([^_]+)_([^.]+)\.(?:zip|tar.gz)$/', $asset['name'], $match);
[, $assetPlatform, $assetArch] = $match;
if ($assetPlatform === $systemPlatform) {
// TODO: assetArch === systemArch (no arm builds for test server yet)
return $asset;
}
}
throw new \RuntimeException("Asset for $systemPlatform not found");
}
public function download(SystemInfo $systemInfo): void
{
$asset = $this->getAsset($systemInfo->platform, $systemInfo->arch);
$assetUrl = $asset['browser_download_url'];
$pathToExtractedAsset = $this->downloadAsset($assetUrl);
$targetPath = getcwd() . DIRECTORY_SEPARATOR . $systemInfo->temporalServerExecutable;
$this->filesystem->copy($pathToExtractedAsset . DIRECTORY_SEPARATOR . $systemInfo->temporalServerExecutable, $targetPath);
$this->filesystem->chmod($targetPath, 0755);
$this->filesystem->remove($pathToExtractedAsset);
}
public function check(string $filename): bool
{
return $this->filesystem->exists($filename);
}
private function downloadAsset(string $assetUrl): string
{
$response = $this->httpClient->request('GET', $assetUrl);
$assetPath = getcwd() . DIRECTORY_SEPARATOR . basename($assetUrl);
if ($this->filesystem->exists($assetPath)) {
$this->filesystem->remove($assetPath);
}
$this->filesystem->touch($assetPath);
$this->filesystem->appendToFile($assetPath, $response->getContent());
$phar = new \PharData($assetPath);
$extractedPath = getcwd() . DIRECTORY_SEPARATOR . $phar->getFilename();
if (!$this->filesystem->exists($extractedPath)) {
$phar->extractTo(getcwd());
}
$this->filesystem->remove($phar->getPath());
return $extractedPath;
}
private function getAsset(string $systemPlatform, string $systemArch): array
{
$response = $this->httpClient->request('GET', $this->javaSdkUrl);
$assets = $response->toArray()['assets'];
return $this->findAsset($assets, $systemPlatform, $systemArch);
}
}