nextcloud-notes/tests/nextcloud-version.php

147 lines
4.0 KiB
PHP
Raw Normal View History

2019-09-19 08:55:17 +02:00
<?php
function getNCVersionFromComposer($path) {
2020-03-29 14:02:58 +02:00
if (!file_exists($path)) {
2019-09-19 08:55:17 +02:00
throw new Exception('Composer file does not exists: '.$path);
}
2020-03-29 14:02:58 +02:00
if (!is_readable($path)) {
2019-09-19 08:55:17 +02:00
throw new Exception('Composer file is not readable: '.$path);
}
$content = file_get_contents($path);
$json = json_decode($content);
2020-03-29 14:02:58 +02:00
if (!is_object($json)) {
2019-09-19 08:55:17 +02:00
throw new Exception('Composer file does not contain valid JSON');
}
2020-02-13 15:22:52 +01:00
$dev = getValidProperty($json, 'require-dev');
$v = getValidProperty($dev, 'christophwurst/nextcloud');
2021-02-03 21:30:50 +01:00
if (substr($v, 0, 1) == '^') {
2020-02-13 15:22:52 +01:00
$v = substr($v, 1);
2019-09-19 08:55:17 +02:00
}
2021-03-02 23:18:35 +01:00
if (substr($v, 0, 2) == '>=') {
$v = substr($v, 2);
}
2020-02-13 15:22:52 +01:00
return $v;
}
function getNCVersionFromComposerBranchAlias($path) {
2020-03-29 14:02:58 +02:00
if (!file_exists($path)) {
2020-02-13 15:22:52 +01:00
throw new Exception('Composer file does not exists: '.$path);
2019-09-19 08:55:17 +02:00
}
2020-03-29 14:02:58 +02:00
if (!is_readable($path)) {
2020-02-13 15:22:52 +01:00
throw new Exception('Composer file is not readable: '.$path);
2019-09-19 08:55:17 +02:00
}
2020-02-13 15:22:52 +01:00
$content = file_get_contents($path);
$json = json_decode($content);
2020-03-29 14:02:58 +02:00
if (!is_object($json)) {
2020-02-13 15:22:52 +01:00
throw new Exception('Composer file does not contain valid JSON');
}
$extra = getValidProperty($json, 'extra');
$branchAlias = getValidProperty($extra, 'branch-alias');
$v = getValidProperty($branchAlias, 'dev-master');
2021-02-03 21:30:50 +01:00
if (substr($v, -4) == '-dev') {
2020-02-13 15:22:52 +01:00
$v = substr($v, 0, -4);
2019-09-19 08:55:17 +02:00
}
return $v;
}
2020-02-13 15:22:52 +01:00
function getValidProperty($json, $prop) {
2020-03-29 14:02:58 +02:00
if (!property_exists($json, $prop)) {
2020-02-13 15:22:52 +01:00
throw new Exception('Composer file has no "'.$prop.'" section');
}
return $json->{$prop};
}
function getDependencyVersionFromAppInfo($path, $type = 'nextcloud', $minmax = 'min') {
2020-03-29 14:02:58 +02:00
if (!file_exists($path)) {
2019-09-19 08:55:17 +02:00
throw new Exception('AppInfo does not exists: '.$path);
}
2020-03-29 14:02:58 +02:00
if (!is_readable($path)) {
2019-09-19 08:55:17 +02:00
throw new Exception('AppInfo is not readable: '.$path);
}
$content = file_get_contents($path);
$info = new SimpleXMLElement($content);
$nc = $info->dependencies->$type;
2020-02-13 15:22:52 +01:00
$v = (string)$nc->attributes()->{$minmax.'-version'};
2019-09-19 08:55:17 +02:00
return $v;
}
function isServerBranch($branch) : bool {
require_once dirname(__FILE__).'/../vendor/autoload.php';
$http = new \GuzzleHttp\Client(['http_errors' => false]);
$response = $http->request('GET', 'https://api.github.com/repos/nextcloud/server/branches/'.$branch);
$status = $response->getStatusCode();
switch ($status) {
case 200:
return true;
case 404:
return false;
default:
throw new \Exception('HTTP Error while checking branch '.$branch.' for Nextcloud server: '.$status);
}
}
function versionCompare($sv1, $sv2, $type) {
2020-02-13 15:22:52 +01:00
$v1 = explode('.', $sv1);
$v2 = explode('.', $sv2);
$count = min(count($v1), count($v2));
2021-02-03 21:30:50 +01:00
for ($i = 0; $i < $count; $i++) {
if ($type == 'max' && $v1[$i] < $v2[$i]) {
return true;
}
2020-03-29 14:02:58 +02:00
if ($v1[$i] !== $v2[$i]) {
2020-02-13 15:22:52 +01:00
return false;
}
}
return true;
}
$pathAppInfo = __DIR__.'/../appinfo/info.xml';
if (in_array('--appinfo', $argv)) {
echo getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'min');
exit;
}
if (in_array('--serverbranch', $argv)) {
$ncVersion = getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'min');
$branch = 'stable' . $ncVersion;
if (!isServerBranch($branch)) {
$branch = 'master';
}
echo $branch;
exit;
}
if (in_array('--php-min', $argv)) {
echo getDependencyVersionFromAppInfo($pathAppInfo, 'php', 'min');
exit;
}
if (in_array('--php-max', $argv)) {
echo getDependencyVersionFromAppInfo($pathAppInfo, 'php', 'max');
exit;
}
2020-02-13 15:22:52 +01:00
echo 'Testing Nextcloud version ';
2019-09-19 08:55:17 +02:00
try {
$vComposer = getNCVersionFromComposer(__DIR__.'/../composer.json');
2020-03-29 14:02:58 +02:00
if ($vComposer === 'dev-master') {
2020-02-13 15:22:52 +01:00
$vComposer = getNCVersionFromComposerBranchAlias(__DIR__.'/../vendor/christophwurst/nextcloud/composer.json');
$vAppInfo = getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'max');
$type = 'max';
2020-02-13 15:22:52 +01:00
} else {
$vAppInfo = getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'min');
$type = 'min';
2020-02-13 15:22:52 +01:00
}
echo $type.': '.$vAppInfo.' (AppInfo) vs. '.$vComposer.' (Composer) => ';
if (versionCompare($vComposer, $vAppInfo, $type)) {
2020-02-13 15:22:52 +01:00
echo 'OK'.PHP_EOL;
2019-09-19 08:55:17 +02:00
} else {
2020-02-13 15:22:52 +01:00
echo 'FAILED'.PHP_EOL;
2019-09-19 08:55:17 +02:00
exit(1);
}
2020-03-29 14:02:58 +02:00
} catch (Exception $e) {
2019-09-19 08:55:17 +02:00
echo $e->getMessage().PHP_EOL;
exit(1);
}