Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New upgrade command #71

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ This should print the Kirby CLI version and a list of available commands
- kirby remove:command
- kirby roots
- kirby unzip
- kirby upgrade
- kirby uuid:generate
- kirby uuid:populate
- kirby uuid:remove
Expand Down
62 changes: 62 additions & 0 deletions commands/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;

return [
'description' => 'Upgrades the Kirby core',
'args' => [
'version' => [
'description' => 'The version corresponding with the tag name in the kirby repo',
'defaultValue' => 'latest'
]
],
'command' => static function (CLI $cli): void {
$version = $cli->arg('version');
$kirby = $cli->kirby();
$kirbyRoot = $kirby->root('kirby');
$folder = 'kirby.' . $version;

// if entered version is `latest`
// get exact version to compare current kirby version
if ($version === 'latest') {
$release = json_decode(file_get_contents('https://getkirby.com/security.json'));
$version = $release->latest;
}

// checks current kirby version whether same or higher
if (version_compare($version, $kirby->version(), '<=') === true) {
throw new Exception('Current Kirby version is the same or higher than the version you are trying to upgrade to');
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
}

// confirms the process when major version upgrade available
if ((int)$version > (int)$kirby->version()) {
$cli->confirmToContinue(
'Major version upgrade detected. Are you sure you want to proceed?',
fn () => throw new Exception('Major version upgrade has been canceled')
);
}

if (is_dir($cli->dir() . '/' . $folder) === true) {
throw new Exception('The ' . $folder . ' directory exists');
}

$cli->out('Upgrading Kirby from ' . $kirby->version() . ' to ' . $version . ' …');
$cli->run('install:repo', 'getkirby/kirby', $folder, $version);

// move current kirby to temp directory as backup
rename($kirbyRoot, $kirbyRoot . '.old');

// move new kirby to current root
rename($cli->dir() . '/' . $folder, $kirbyRoot);

// delete old kirby
$cli->rmdir($kirbyRoot . '.old');

// delete temp panel directory
$cli->rmdir($kirby->root('media') . '/panel');

$cli->success('The Kirby has been upgraded to ' . $version);
}
];
Loading