|
| 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