Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 162 additions & 18 deletions _bin/generate-downloads-json-data.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,188 @@
#!/usr/bin/env php
<?php

if (!isset($argv[1]) || !isset($argv[2])) {
echo 'Usage: ' . basename(__FILE__) . " BASEDIR_OF_TARBALLS VERSION\n";
exit(1);
/**
* Retrieve release assest from the GitHub API
*/
define('BASE_PATH', realpath(__DIR__ . '/..') . '/');
define('GITHUB_API', 'https://api.github.com/repos/roundcube/roundcubemail');

$downloads_file = BASE_PATH . '_data/downloads.json';
if (!file_exists($downloads_file) || !is_writable($downloads_file)) {
throw new RuntimeException(
'_data/downloads.json file not found or not writable'
);
}

if (!is_dir($argv[1])) {
echo "Error! Invalid directory: {$argv[1]}\n";
$downloads = json_decode(file_get_contents($downloads_file), true);

if (!isset($argv[1]) || !isset($argv[2])) {
echo 'Usage: ' . basename(__FILE__) . " PACKAGE_TYPE VERSION\n";
echo 'Supported package types: ' . implode('|', array_keys($downloads)) ."\n";
exit(1);
}

$basedir = $argv[1];
$type = $argv[1];
$version = $argv[2];

function human_file_size($byte)
if (!array_key_exists($type, $downloads)) {
throw new RuntimeException(
sprintf(
'Unknown package type: %s',
$type
)
);
}

/**
* Make an HTTP GET request and decode the JSON response.
*/
function apiRequest($url)
{
$ch = curl_init($url);

if ($ch === false) {
throw new RuntimeException('Unable to initialise cURL');
}

curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
],
CURLOPT_USERAGENT => 'roundcube-website/1.0',
]);

$response = curl_exec($ch);

if ($response === false) {
$error = curl_error($ch);
curl_close($ch);

throw new RuntimeException(
sprintf('Request failed: %s', $error)
);
}

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($status < 200 || $status >= 300) {
throw new RuntimeException(
sprintf(
'Request failed with HTTP %d: %s',
$status,
$url
)
);
}

$data = json_decode($response, true);

if (!is_array($data)) {
throw new RuntimeException(
sprintf(
'Invalid JSON returned by %s',
$url
)
);
}

return $data;
}

function retrieveAssets($version)
{
$url = GITHUB_API . '/releases/tags/' . rawurlencode($version);
$data = apiRequest($url);

if (!isset($data['assets']) || !is_array($data['assets'])) {
throw new RuntimeException(
sprintf(
'Unable to resolve version "%s" for %s',
$constraint,
$package
)
);
}

return $data['assets'];
}

function humanFileSize($byte)
{
$factor = floor((strlen($byte) - 1) / 3);
if ($factor > 0) {
$sz = 'KMGT';
}

return sprintf('%.1f', $byte / 1024 ** $factor) . ' ' . @$sz[$factor - 1] . 'B';
}

function generate_data($version, $package_name, $basedir, $filename)
function generateData($github_assets, $version, $package_name, $filename)
{
$filepath = join(DIRECTORY_SEPARATOR, [$basedir, $filename]);
$sum = hash_file('sha256', $filepath);
$size = human_file_size(filesize($filepath));
$asset = array_filter($github_assets, function ($item) use ($filename) {
return ($item['name'] ?? null) === $filename;
});

if (empty($asset)) {
throw new RuntimeException(
sprintf(
'Cannot locate file %s in asset list',
$filename
)
);
}

$meta = reset($asset);
if (!str_starts_with($meta['digest'], 'sha256:')) {
throw new RuntimeException(
sprintf(
'Cannot locate SHA256 checksom for %s',
$filename
)
);
}

$url = $meta['browser_download_url'];
$size = humanFileSize($meta['size']);
$checksum = substr($meta['digest'], 7);

return [
'package' => $package_name,
'url' => "https://github.com/roundcube/roundcubemail/releases/download/{$version}/{$filename}",
'url' => $url,
'version' => $version,
'size' => $size,
'checksum' => $sum,
'checksum' => $checksum,
];
}

echo json_encode([
generate_data($version, 'Dependent', $basedir, "roundcubemail-{$version}.tar.gz"),
generate_data($version, 'Complete', $basedir, "roundcubemail-{$version}-complete.tar.gz"),
generate_data($version, 'Framework', $basedir, "roundcube-framework-{$version}.tar.gz"),
], \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) . "\n";
$github_assets = retrieveAssets($version);

switch ($type) {
case 'lts':
$packages = [
generateData($github_assets, $version, 'Complete', "roundcubemail-{$version}-complete.tar.gz"),
];
break;
case 'old-stable':
$packages = [
generateData($github_assets, $version, 'Dependent', "roundcubemail-{$version}.tar.gz"),
generateData($github_assets, $version, 'Complete', "roundcubemail-{$version}-complete.tar.gz"),
];
break;
default:
$packages = [
generateData($github_assets, $version, 'Dependent', "roundcubemail-{$version}.tar.gz"),
generateData($github_assets, $version, 'Complete', "roundcubemail-{$version}-complete.tar.gz"),
generateData($github_assets, $version, 'Framework', "roundcube-framework-{$version}.tar.gz"),
];
}

$downloads[$type]['packages'] = $packages;
file_put_contents($downloads_file, json_encode($downloads, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) . "\n");

echo "_data/downloads.json updated.\n";
13 changes: 5 additions & 8 deletions _data/downloads.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"old-stable": {
"title": "Old stable",
"titleVersion": true,
"packages": [
]
"packages": []
},
"lts": {
"title": "LTS versions",
Expand All @@ -41,23 +40,21 @@
"package": "Complete",
"url": "https://github.com/roundcube/roundcubemail/releases/download/1.6.22/roundcubemail-1.6.22-complete.tar.gz",
"version": "1.6.22",
"size": "5.6 MB",
"size": "5.7 MB",
"checksum": "09bb4c7f0cf24039e28c73f349d5eb99a1297a423c886230fd8d4e32b091b1c2"
},
}
]
},
"beta": {
"title": "Beta version",
"titleVersion": true,
"description": "We recommend to test pre-release versions on a separate environment, and don't forget to backup your data before installing.",
"packages": [
]
"packages": []
},
"rc": {
"title": "Release candidate",
"titleVersion": true,
"description": "We recommend to test pre-release versions on a separate environment, and don't forget to backup your data before installing.",
"packages": [
]
"packages": []
}
}