|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +use Kirby\CLI\CLI; |
| 6 | + |
| 7 | +return [ |
| 8 | + 'description' => 'Upgrades a Kirby core', |
| 9 | + 'args' => [ |
| 10 | + 'version' => [ |
| 11 | + 'description' => 'The version corresponding with the tag name in the kirby repo', |
| 12 | + 'defaultValue' => 'latest' |
| 13 | + ] |
| 14 | + ], |
| 15 | + 'command' => static function (CLI $cli): void { |
| 16 | + $version = $cli->arg('version'); |
| 17 | + $kirby = $cli->kirby(); |
| 18 | + $kirbyRoot = $kirby->root('kirby'); |
| 19 | + $folder = 'kirby.' . $version; |
| 20 | + |
| 21 | + // if entered version is `latest` |
| 22 | + // get exact version to compare current kirby version |
| 23 | + if ($version === 'latest') { |
| 24 | + $release = json_decode(file_get_contents('https://getkirby.com/security.json')); |
| 25 | + $version = $release->latest; |
| 26 | + } |
| 27 | + |
| 28 | + if (version_compare($version, $kirby->version(), '<=') === true) { |
| 29 | + throw new Exception('Current Kirby version is the same or higher than the version you are trying to upgrade to'); |
| 30 | + } |
| 31 | + |
| 32 | + if (is_dir($cli->dir() . '/' . $folder) === true) { |
| 33 | + throw new Exception('The ' . $folder . ' directory exists'); |
| 34 | + } |
| 35 | + |
| 36 | + $cli->out('Upgrading Kirby from ' . $kirby->version() . ' to ' . $version . ' …'); |
| 37 | + $cli->run('install:repo', 'getkirby/kirby', $folder, $version); |
| 38 | + |
| 39 | + // move current kirby to temp directory as backup |
| 40 | + rename($kirbyRoot, $kirbyRoot . '.old'); |
| 41 | + |
| 42 | + // move new kirby to current root |
| 43 | + rename($cli->dir() . '/' . $folder, $kirbyRoot); |
| 44 | + |
| 45 | + // delete old kirby |
| 46 | + $cli->rmdir($kirbyRoot . '.old'); |
| 47 | + |
| 48 | + // delete temp panel directory |
| 49 | + $cli->rmdir($kirby->root('media') . '/panel'); |
| 50 | + |
| 51 | + $cli->success('The Kirby has been upgraded to ' . $version); |
| 52 | + } |
| 53 | +]; |
0 commit comments