From ce81e9ae81da9d9169641987fa92f8e91451102b Mon Sep 17 00:00:00 2001 From: wyattis Date: Tue, 22 Jun 2021 10:06:35 -0600 Subject: [PATCH] change download app command to use releases --- .gitignore | 3 +- app/Console/Commands/BundleLatestReports.php | 4 +- app/Console/Commands/DownloadApp.php | 81 +++++++++++--------- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 8f99c83e..e2b4ec57 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,5 @@ laravel-queue-worker.conf laravel-worker.conf -storage/*.sqlite.db \ No newline at end of file +storage/*.sqlite.db +trellis-app \ No newline at end of file diff --git a/app/Console/Commands/BundleLatestReports.php b/app/Console/Commands/BundleLatestReports.php index 2682de07..c2c076ad 100644 --- a/app/Console/Commands/BundleLatestReports.php +++ b/app/Console/Commands/BundleLatestReports.php @@ -17,7 +17,9 @@ class BundleLatestReports extends Command * * @var string */ - protected $signature = 'trellis:bundle:reports {study} {--location=exports : The location to save the export at} {--name=reports.zip : The filename to use}'; + protected $signature = 'trellis:bundle:reports {study} + {--location=exports : The location to save the export at} + {--name=reports.zip : The filename to use}'; /** * The console command description. diff --git a/app/Console/Commands/DownloadApp.php b/app/Console/Commands/DownloadApp.php index 87941ba8..d3735593 100644 --- a/app/Console/Commands/DownloadApp.php +++ b/app/Console/Commands/DownloadApp.php @@ -14,7 +14,7 @@ class DownloadApp extends Command * * @var string */ - protected $signature = 'trellis:download-app'; + protected $signature = 'trellis:download-app {--asset-name=trellis-web.zip} {--timeout=120}'; /** * The console command description. @@ -33,34 +33,43 @@ public function handle() { /* First, let's get the RELEASES.md from github and parse the list of releases. */ $this->info("Checking for trellis app releases..."); + $assetName = $this->option('asset-name'); + $timeout = $this->option('timeout'); $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, "https://raw.githubusercontent.com/human-nature-lab/trellis/master/RELEASES.md"); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $output = curl_exec($ch); - /* Get everything up to the --- horizontal rule */ - $versionListString = trim(substr($output, 0, strpos($output, "---"))); - $versionList = explode("\n", $versionListString); - $versions = array(); - $choices = array(); - foreach ($versionList as $versionString) { - $version = array(); - $start = strpos($versionString, "[") + 1; - $end = strpos($versionString, "]"); - $version["name"] = substr($versionString, $start, $end - $start); - $start = strpos($versionString, "(") + 1; - $end = strpos($versionString, ")"); - $version["url"] = substr($versionString, $start, $end - $start); - $choices[] = $version["name"]; - $versions[] = $version; - } + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, 'Trellis API'); + curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/human-nature-lab/trellis-app/releases?prerelease=true'); + $result = curl_exec($ch); + curl_close($ch); + $versions = json_decode($result, true); + $versions = array_filter($versions, function ($v) use ($assetName) { + if (!isset($v['tag_name']) || !isset($v['assets'])) return false; + $filteredAssets = array_filter($v['assets'], function ($a) use ($assetName) { + return $a['name'] === $assetName; + }); + $webAsset = array_pop($filteredAssets); + return isset($webAsset); + }); + $choices = array_map(function ($v) { + return $v['tag_name']; + }, $versions); $chosenVersionName = $this->choice("Which version of the Trellis app do you want to download and install?", $choices, (count($choices) - 1)); - $chosenVersion = array_values(array_filter($versions, function($v) use ($chosenVersionName) { return $v["name"] == $chosenVersionName; })); - - $this->info($chosenVersion[0]["url"]); - $chosenVersionUrl = $chosenVersion[0]["url"]; - $this->info("Downloading..."); - curl_setopt($ch, CURLOPT_URL, $chosenVersionUrl); - curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); + $this->info($chosenVersionName); + $chosenVersion = array_filter($versions, function($v) use ($chosenVersionName) { return $v["tag_name"] == $chosenVersionName; })[0]; + $chosenAsset = array_values(array_filter($chosenVersion['assets'], function ($a) use ($assetName) { + return $a['name'] === $assetName; + }))[0]; + $chosenVersionUrl = $chosenAsset['browser_download_url']; + $this->info($chosenVersionUrl); + + $zipPath = storage_path('temp/trellis-web.zip'); + $this->info("Downloading to $zipPath..."); + + $ch = curl_init( $chosenVersionUrl); + $fp = fopen($zipPath, 'w+'); + + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $bar = $this->output->createProgressBar(100); @@ -70,7 +79,7 @@ public function handle() $this->progress($downloadSize, $downloaded, $bar); }); - $rawFile = curl_exec($ch); + curl_exec($ch); if(curl_errno($ch)) { $this->error(curl_error($ch)); @@ -78,10 +87,9 @@ public function handle() $bar->finish(); - $zipPath = storage_path('temp/trellis-app.zip'); - file_put_contents($zipPath, $rawFile); + curl_close($ch); + fclose($fp); - $this->info(""); $trellisAppDir = $this->ask('Where do you want to install the trellis web application?', '/var/www/trellis-app'); $this->info("Extracting \"$chosenVersionName\" to \"$trellisAppDir\"..."); @@ -89,9 +97,10 @@ public function handle() $zip = new ZipArchive; $res = $zip->open($zipPath); if ($res === TRUE) { - $zip->extractTo($trellisAppDir); + $zip->extractTo($trellisAppDir); } else { - $this->error("Unable to open zip archive."); + $this->error("Unable to open zip archive."); + return; } unlink($zipPath); @@ -105,11 +114,9 @@ public function handle() apiRoot: '$apiEndpoint' }\n"; - file_put_contents($trellisAppDir . '/config.js', $configFile); + file_put_contents($trellisAppDir . '/www/config.js', $configFile); - curl_close($ch); - $this->info(""); - $this->info("Done!"); + $this->info("\nDone!"); return 0; }