Skip to content

Commit 397d709

Browse files
committed
Add downloaders
1 parent 8086c54 commit 397d709

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/Commands/DownloadRuntimes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\RuntimeDownloader;
6+
use Webteractive\Devstack\ShouldConfigure;
7+
8+
class DownloadRuntimes extends Base
9+
{
10+
use ShouldConfigure;
11+
12+
protected $signature = 'download';
13+
protected $description = 'Download latest runtimes';
14+
15+
public function handle(): int
16+
{
17+
$this->ensureConfigurationIsSet();
18+
19+
(new RuntimeDownloader($this, $this->config))->download();
20+
21+
return static::SUCCESS;
22+
}
23+
}

src/RuntimeDownloader.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack;
4+
5+
use ZipArchive;
6+
7+
class RuntimeDownloader
8+
{
9+
use ShouldConfigure,
10+
WithHttp;
11+
12+
protected $command;
13+
14+
public function __construct($command, $config)
15+
{
16+
$this->command = $command;
17+
$this->config = $config;
18+
}
19+
20+
public function download($shouldGetTheLatestRuntimes = true)
21+
{
22+
if ($shouldGetTheLatestRuntimes) {
23+
$this->devstackStorage()->deleteDirectory('runtimes');
24+
$this->command->info("Downloading fresh runtimes from {$this->config['repository']}.");
25+
}
26+
27+
$archive = $this->homePath('runtimes.zip');
28+
$downloadUrl = join('/', [$this->config['repository'], 'zipball', $this->config['branch']]);
29+
$response = $this->http()->get($downloadUrl, [
30+
'headers' => [
31+
'Authorization' => 'token ' . $this->config['token'],
32+
],
33+
'sink' => $archive
34+
]);
35+
36+
if ($response->getStatusCode() == 200) {
37+
$zip = new ZipArchive;
38+
if ($zip->open($archive)) {
39+
$zip->extractTo($this->homePath('tmp'));
40+
$zip->close();
41+
$this->devstackStorage()->delete('runtimes.zip');
42+
$extracted = $this->devstackStorage()->directories('tmp')[0];
43+
$extractedName = explode('-', pathinfo($extracted, PATHINFO_BASENAME));
44+
$hash = array_pop($extractedName);
45+
$this->command->info("Now using runtimes from the commit {$hash}");
46+
$runtimes = $this->devstackStorage()->directories($extracted);
47+
foreach ($runtimes as $runtime) {
48+
$runtimeName = pathinfo($runtime, PATHINFO_BASENAME);
49+
$this->runtimes[$runtimeName] = $runtime;
50+
$this->devstackStorage()->move($runtime, 'runtimes/' . $runtimeName);
51+
}
52+
$this->devstackStorage()->deleteDirectory('tmp');
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)