Skip to content

Commit

Permalink
change download app command to use releases
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattis committed Jun 22, 2021
1 parent 83204df commit ce81e9a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ laravel-queue-worker.conf
laravel-worker.conf


storage/*.sqlite.db
storage/*.sqlite.db
trellis-app
4 changes: 3 additions & 1 deletion app/Console/Commands/BundleLatestReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
81 changes: 44 additions & 37 deletions app/Console/Commands/DownloadApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -70,28 +79,28 @@ public function handle()
$this->progress($downloadSize, $downloaded, $bar);
});

$rawFile = curl_exec($ch);
curl_exec($ch);

if(curl_errno($ch)) {
$this->error(curl_error($ch));
}

$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\"...");

$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);
Expand All @@ -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;
}

Expand Down

0 comments on commit ce81e9a

Please sign in to comment.