Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nextcloud-version.php « tests - github.com/nextcloud/notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c615e9dcb7fa38f2e37b931b6a54e6162787e55f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

function getNCVersionFromComposer($path) {
	if (!file_exists($path)) {
		throw new Exception('Composer file does not exists: '.$path);
	}
	if (!is_readable($path)) {
		throw new Exception('Composer file is not readable: '.$path);
	}
	$content = file_get_contents($path);
	$json = json_decode($content);
	if (!is_object($json)) {
		throw new Exception('Composer file does not contain valid JSON');
	}
	$dev = getValidProperty($json, 'require-dev');
	$v = getValidProperty($dev, 'christophwurst/nextcloud');
	if (substr($v, 0, 1) == '^') {
		$v = substr($v, 1);
	}
	if (substr($v, 0, 2) == '>=') {
		$v = substr($v, 2);
	}
	return $v;
}


function getNCVersionFromComposerBranchAlias($path) {
	if (!file_exists($path)) {
		throw new Exception('Composer file does not exists: '.$path);
	}
	if (!is_readable($path)) {
		throw new Exception('Composer file is not readable: '.$path);
	}
	$content = file_get_contents($path);
	$json = json_decode($content);
	if (!is_object($json)) {
		throw new Exception('Composer file does not contain valid JSON');
	}
	$extra = getValidProperty($json, 'extra');
	$branchAlias = getValidProperty($extra, 'branch-alias');
	$v = getValidProperty($branchAlias, 'dev-master');
	if (substr($v, -4) == '-dev') {
		$v = substr($v, 0, -4);
	}
	return $v;
}


function getValidProperty($json, $prop) {
	if (!property_exists($json, $prop)) {
		throw new Exception('Composer file has no "'.$prop.'" section');
	}
	return $json->{$prop};
}

function getDependencyVersionFromAppInfo($path, $type = 'nextcloud', $minmax = 'min') {
	if (!file_exists($path)) {
		throw new Exception('AppInfo does not exists: '.$path);
	}
	if (!is_readable($path)) {
		throw new Exception('AppInfo is not readable: '.$path);
	}
	$content = file_get_contents($path);
	$info = new SimpleXMLElement($content);
	$nc = $info->dependencies->$type;
	$v = (string)$nc->attributes()->{$minmax.'-version'};
	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) {
	$v1 = explode('.', $sv1);
	$v2 = explode('.', $sv2);
	$count = min(count($v1), count($v2));
	for ($i = 0; $i < $count; $i++) {
		if ($type == 'max' && $v1[$i] < $v2[$i]) {
			return true;
		}
		if ($v1[$i] !== $v2[$i]) {
			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;
}

echo 'Testing Nextcloud version ';
try {
	$vComposer = getNCVersionFromComposer(__DIR__.'/../composer.json');
	if ($vComposer === 'dev-master') {
		$vComposer = getNCVersionFromComposerBranchAlias(__DIR__.'/../vendor/christophwurst/nextcloud/composer.json');
		$vAppInfo = getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'max');
		$type = 'max';
	} else {
		$vAppInfo = getDependencyVersionFromAppInfo($pathAppInfo, 'nextcloud', 'min');
		$type = 'min';
	}
	echo $type.': '.$vAppInfo.' (AppInfo) vs. '.$vComposer.' (Composer) => ';
	if (versionCompare($vComposer, $vAppInfo, $type)) {
		echo 'OK'.PHP_EOL;
	} else {
		echo 'FAILED'.PHP_EOL;
		exit(1);
	}
} catch (Exception $e) {
	echo $e->getMessage().PHP_EOL;
	exit(1);
}