-
Notifications
You must be signed in to change notification settings - Fork 97
/
install-cli.php
executable file
·462 lines (414 loc) · 13.1 KB
/
install-cli.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env php
<?php
/**
* Install / update EGroupware - Command line interface
*
* Usage:
*
* - install-cli.php [-v|--verbose] [--use-prerelease] [<composer-args>] [(master|bugfix|release|<branch>|<tag>)]
* you can use composer install arguments like: --ignore-platform-reqs --no-dev
*
* - install-cli.php [-c|--continue-on-error] --git(-apps) <arguments>
* runs git with given arguments (in main- and) all app-dirs, e.g. tag -a 17.1.20190214 -m 'tagging release'
*
* EGroupware main directory should be either git cloned:
*
* git clone [-b <branch>] https://github.com/EGroupware/egroupware [<target>]
*
* or created via composer create-project
*
* composer create-project --prefer-source --keep-vcs egroupware/egroupware[:(dev-master|17.1.x-dev|<tag>)] <target>
*
* Both will create a git clone, which can be further updated by calling this tool without argument.
*
* We currently use 3 "channels":
* - release: taged maintenance releases only eg. 17.1.20190214
* - bugfix: release-branch incl. latest bugfixes eg. 17.1 or 17.1.x-dev for composer
* - master: latest development for next release
* To change the channel, call install-cli.php <channel-to-update-to>.
*
* This tool requires the following binaries installed at the usually places or in your path:
* - php & git: apt/yum/zypper install php-cli git
* - composer: see https://getcomposer.org/download/ for installation instructions
* The following binaries are needed to minify JavaScript and CSS
* - npm: apt/yum/zypper install npm
* - grunt: npm install -g grunt-cli
*
* @link http://www.egroupware.org
* @package api
* @author Ralf Becker <[email protected]>
* @copyright (c) 2019 by Ralf Becker <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
chdir(__DIR__); // to enable relative pathes to work
if (php_sapi_name() !== 'cli') // security precaution: forbit calling setup-cli as web-page
{
die('<h1>install-cli.php must NOT be called as web-page --> exiting !!!</h1>');
}
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
// parse arguments
$verbose = $use_prerelease = $run_git = $continue_on_error = false;
$composer_args = [];
$argv = $_SERVER['argv'];
$cmd = array_shift($argv);
foreach($argv as $n => $arg)
{
if ($arg[0] === '-')
{
switch($arg)
{
case '-v':
case '--verbose':
$verbose = true;
unset($argv[$n]);
break;
case '--use-prerelease':
$use_prerelease = true;
unset($argv[$n]);
break;
case '-h':
case '--help':
usage();
case '--git':
case '--git-apps':
$run_git = $arg;
unset($argv[$n]);
break 2; // no further argument processing, as they are for git
case '-c':
case '--continue-on-error':
$continue_on_error = true;
unset($argv[$n]);
break;
default: // pass unknown arguments to composer install
$composer_args[] = $arg;
unset($argv[$n]);
break;
}
}
}
if (!$run_git && count($argv) > 1) usage("Too many arguments!");
function usage($err=null)
{
global $cmd;
if ($err)
{
echo "$err\n\n";
}
die("Usage:\t$cmd [-v|--verbose] [--use-prerelease] [<composer-args>] (master|bugfix|release|<branch>|<tag>)\n".
"\t\nyou can use composer install arguments like: --ignore-platform-reqs --no-dev\n".
"\t$cmd [-c|--continue-on-error] --git(-apps) <arguments>\n".
"\truns git with given arguments (in main- and) all app-dirs, e.g. tag -a 17.1.20190214 -m 'tagging release'\n\n");
}
$bins = array(
'php' => PHP_BINARY,
'git' => ['/usr/local/bin/git', '/usr/bin/git'],
'composer' => ['/usr/local/bin/composer', '/usr/bin/composer', '/usr/bin/composer.phar'],
// npm and grunt are no hard requirement and should be the last in the list!
'npm' => ['/usr/local/bin/npm', '/usr/bin/npm'],
'grunt' => [__DIR__.'/node_modules/.bin/grunt', '/usr/local/bin/grunt', '/usr/bin/grunt'],
);
// check if the necessary binaries are installed
foreach($bins as $name => $binaries)
{
foreach((array)$binaries as $bin)
{
if (file_exists($bin) && is_executable($bin))
{
$bins[$name] = $$name = $bin;
continue 2;
}
}
$output = $ret = null;
if (($bin = exec('which '.$name, $output, $ret)) && !$ret &&
(file_exists($bin)) && is_executable($bin))
{
$bins[$name] = $$name = $bin;
}
// check if we can just run it, because it's in the path
elseif (exec($name.' -v', $output, $ret) && !$ret)
{
$bins[$name] = $$name = $num;
}
else
{
$bins[$name] = $$name = false;
error_log("Could not find $name command!");
if (!in_array($name, ['npm','grunt']))
{
exit(1);
}
else
{
error_log("npm and grunt are required to minify JavaScript and CSS files to improve performance.");
break;
}
}
}
if ($verbose) echo "Using following binaries: ".json_encode ($bins, JSON_UNESCAPED_SLASHES)."\n";
if (!extension_loaded('curl')) die("Required PHP extension 'curl' missing! You need to install php-curl package.\n\n");
// check if we are on a git clone
$output = array();
if (!file_exists(__DIR__.'/.git') || !is_dir(__DIR__.'/.git'))
{
error_log("Could not identify git branch (you need to use git clone or composer create-project --prefer-source --keep-vcs egroupware/egroupware)!");
exit(1);
}
// should we only run a git command
if ($run_git)
{
exit (run_git($argv, $run_git === '--git'));
}
if (!exec($git.' branch --no-color', $output, $ret) || $ret)
{
foreach($output as $line)
{
error_log($line);
}
exit($ret);
}
foreach($output as $line)
{
if ($line[0] == '*')
{
$branch = substr($line, 2);
// are we on a tag
if (preg_match('/^\(HEAD .* ([0-9.]+)\)$/', $branch, $matches))
{
$branch = $matches[1];
}
break;
}
}
$channel = 'development';
if (preg_match('/^\d+\.\d+(\.\d{8})?/', $branch, $machtes))
{
$channel = isset($matches[1]) ? 'release' : 'bugfix';
}
if ($verbose) echo "Currently using branch: $branch --> $channel channel\n";
if ($argv)
{
$target = array_shift($argv);
if ($target === 'release')
{
$target = get_latest_release($use_prerelease);
}
elseif ($target === 'bugfix')
{
$target = (string)(float)get_latest_release($use_prerelease);
}
}
else
{
$target = $branch;
// find the latest release
if ($channel == 'release')
{
$target = get_latest_release($use_prerelease);
}
}
// a branch update requires a composer install with --prefer-source
if (count(explode('.', $target)) < 2)
{
$composer_args[] = '--prefer-source';
}
echo "Updating to: $target\n";
// Update EGroupware itself and further apps installed via git
$failed = array();
$succeeded = 0;
foreach(scandir(__DIR__) as $dir)
{
if ($dir !== '..' && file_exists(__DIR__.'/'.$dir.'/.git'))
// these apps / dirs are managed by composer, no need to run manual updates
//!in_array($dir, ['vendor', 'activesync', 'collabora', 'projectmanager', 'tracker']))
{
$cmd = "cd $dir ; $git stash -q ; ";
// switch message about detached head off for release-channel/tags
if (preg_match('/^\d+\.\d+\.\d{8}/', $target))
{
$cmd .= "$git config advice.detachedHead false ; ";
}
if ($branch != $target)
{
$cmd .= "$git fetch && $git checkout $target && ";
}
// no need to pull for release-channel/tags
if (!preg_match('/^\d+\.\d+\.\d{8}/', $target))
{
$cmd .= "$git pull --rebase && ";
}
$cmd .= "(test -z \"$($git stash list)\" || $git stash pop)";
if ($dir !== '.' && !$verbose)
{
echo $dir.': ';
}
run_cmd($cmd, $dir === '.' ? 'egroupware' : $dir);
}
}
// update composer managed dependencies
$cmd = $composer.' install '.implode(' ', $composer_args);
if (run_cmd($cmd, 'composer') === 0 && in_array('--prefer-source', $composer_args))
{
// check composer has not replaced .git checkouts of apps
$composer_json = json_decode(file_get_contents(__DIR__.'/composer.json'), true);
foreach($composer_json['require'] as $package => $version)
{
if (str_starts_with($package, 'egroupware/') && !in_array($package, ['egroupware/mail']) &&
file_exists(substr($package, 11)) && !file_exists(substr($package, 11).'/.git'))
{
--$succeeded;
$failed[] = $cmd.' changed/installed package "'.$package.'" NOT as source / git clone!';
break;
}
}
}
// update npm dependencies, run grunt to minify css and rollup to build javascript
if ($npm)
{
run_cmd($npm.' install --legacy-peer-deps', 'npm install');
if ($grunt) run_cmd($grunt, 'grunt');
if (!file_exists($chunks=__DIR__.'/chunks') || !is_dir($chunks))
{
if (file_exists($chunks) && !is_dir($chunks))
{
unlink($chunks);
}
if (!mkdir($chunks, 0755) && !is_dir($chunks))
{
throw new \RuntimeException(sprintf('Cloud NOT create directory "%s"!', $chunks));
}
}
run_cmd($npm .' run build', 'rollup (npm run build)');
}
// if docs site directory exists, keep it updated
if ($npm && file_exists(__DIR__.'/doc/dist/site'))
{
run_cmd($npm .' run docs', 'build docs (npm run docs)');
}
echo "\n$succeeded tasks successful run".
($failed ? ', '.count($failed).' failed: '.implode(', ', $failed) : '')."\n\n";
exit(count($failed));
/**
* Run a command and collect number of succieded or failed command
*
* @param string $cmd comamnd to run
* @param string $name task name to report on failure
* @return int exit code of command
*/
function run_cmd($cmd, $name)
{
global $verbose, $failed, $succeeded;
if ($verbose) echo "$cmd\n";
$ret = null;
system($cmd, $ret);
if ($ret == 0)
{
$succeeded++;
}
else
{
$failed[] = $name;
}
return $ret;
}
/**
* Run git command with given arguments all app-dirs and (optional) install-dir
*
* cd and git command is echoed to stderr
*
* @param array $argv
* @param boolean $main_too =true true: run in main-dir too, false: only app-dirs
* @return int exit-code of last git command, breaks on first non-zero exit-code
*/
function run_git(array $argv, $main_too=true)
{
global $git, $continue_on_error;
$git_cmd = $git.' '.implode(' ', array_map('escapeshellarg', $argv));
$ret = 0;
foreach(scandir(__DIR__) as $dir)
{
if (!($dir === '..' || $dir === '.' && !$main_too ||
!file_exists(__DIR__.'/'.$dir.'/.git')))
{
$cmd = ($dir !== '.' ? "cd $dir; " : '').$git_cmd;
error_log("\n>>> ".$cmd."\n");
system($cmd, $ret);
// break if command is not successful, unless --continue-on-error
if ($ret && !$continue_on_error) return $ret;
}
}
return $ret;
}
/**
* Get the latest release
*
* @param boolean $prerelease =false include releases taged as prerelease
* @param boolean $return_name =true true: just return name, false: full release object
* @return array|string|null null if no release found
*/
function get_latest_release($prerelease=false, $return_name=true)
{
foreach(github_api('/repos/egroupware/egroupware/releases', [], 'GET') as $release)
{
if ($prerelease || $release['prerelease'] === false)
{
return $return_name ? $release['tag_name'] : $release;
}
}
return null;
}
/**
* Sending a Github API request
*
* @param string $_url url of just path where to send request to (https://api.github.com is added automatic)
* @param string|array $data payload, array get automatic added as get-parameter or json_encoded for POST
* @param string $method ='POST'
* @param string $upload =null path of file to upload, payload for request with $method='FILE'
* @param string $content_type =null
* @throws Exception
* @return array with response
*/
function github_api($_url, $data, $method='POST', $upload=null, $content_type=null)
{
global /*$config,*/ $verbose;
$url = $_url[0] == '/' ? 'https://api.github.com'.$_url : $_url;
$c = curl_init();
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($c, CURLOPT_USERPWD, $config['github_user'].':'.$config['github_token']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERAGENT, basename(__FILE__));
curl_setopt($c, CURLOPT_TIMEOUT, 240);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
switch($method)
{
case 'POST':
curl_setopt($c, CURLOPT_POST, true);
if (is_array($data)) $data = json_encode($data, JSON_FORCE_OBJECT);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
break;
case 'GET':
if(count($data)) $url .= '?' . http_build_query($data);
break;
case 'FILE':
curl_setopt($c, CURLOPT_HTTPHEADER, array("Content-type: $content_type"));
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, file_get_contents($upload));
if(count($data)) $url .= '?' . http_build_query($data);
break;
default:
throw new Exception(__FUNCTION__.": Unknown/unimplemented method=$method!");
}
curl_setopt($c, CURLOPT_URL, $url);
if (is_string($data)) $short_data = strlen($data) > 100 ? substr($data, 0, 100).' ...' : $data;
if ($verbose) echo "Sending $method request to $url ".(isset($short_data)&&$method!='GET'?$short_data:'')."\n";
if (($response = curl_exec($c)) === false)
{
// run failed request again to display response including headers
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, false);
curl_exec($c);
throw new Exception("$method request to $url failed ".(isset($short_data)&&$method!='GET'?$short_data:''));
}
if ($verbose) echo (strlen($response) > 200 ? substr($response, 0, 200).' ...' : $response)."\n";
curl_close($c);
return json_decode($response, true);
}