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 plugin commands #63

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ This should print the Kirby CLI version and a list of available commands
- kirby make:snippet
- kirby make:template
- kirby make:user
- kirby plugin:install
- kirby plugin:remove
- kirby plugin:upgrade
- kirby register
- kirby remove:command
- kirby roots
Expand Down
59 changes: 59 additions & 0 deletions commands/plugin/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\F;

return [
afbora marked this conversation as resolved.
Show resolved Hide resolved
'description' => 'Installs a Kirby plugin repository from Github',
'args' => [
'repo' => [
'description' => 'The Github repo path (i.e. getkirby/kql)',
'required' => true
],
'version' => [
'description' => 'The version corresponding with the tag name in the repo',
'defaultValue' => 'latest'
]
],
'command' => static function (CLI $cli): void {
$repo = $cli->arg('repo');
$version = $cli->arg('version');
$archiveUrl = 'https://github.com/' . $repo . '/archive';
$composerUrl = 'https://github.com/' . $repo . '/raw/HEAD/composer.json';

// make sure only `kirby-plugin` installable
$composer = json_decode(file_get_contents($composerUrl));
if (($composer?->type ?? null) !== 'kirby-plugin') {
throw new Exception('The GitHub repository should be a Kirby plugin');
}

if ($version === 'latest') {
$url = $archiveUrl . '/main.zip';
} else {
$url = $archiveUrl . '/refs/tags/' . $cli->arg('version') . '.zip';
}

list($vendor, $plugin) = explode('/', $repo);

$zip = $cli->dir() . '/' . $vendor . '-' . $plugin . '-' . time() . '.zip';
$dir = $cli->kirby()->root('plugins') . '/' . $plugin;

$cli->confirmToDelete($zip, 'The zip file exists. Do you want to delete it?');
$cli->confirmToDelete($dir, 'The directory exists. Do you want to delete it?');

$cli->out('Installing ' . $repo . ' plugin …');

// download the zip file
$cli->run('download', $url, $zip);

// unzip the repo
$cli->run('unzip', $zip, $dir);

// remove the zip
F::unlink($zip);

$cli->success('The ' . $repo . ' plugin has been installed');
}
];
26 changes: 26 additions & 0 deletions commands/plugin/remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\Dir;

return [
'description' => 'Removes a Kirby plugin',
'args' => [
'repo' => [
'description' => 'The Kirby plugin registry name (i.e. getkirby/kql)',
'required' => true
]
],
'command' => static function (CLI $cli): void {
$repo = $cli->arg('repo');

if ($plugin = $cli->kirby()->plugin($repo)) {
Dir::remove($plugin->root());
$cli->success('The ' . $repo . ' plugin has been removed');
} else {
$cli->error('The ' . $repo . ' plugin could not be found');
}
}
];
39 changes: 39 additions & 0 deletions commands/plugin/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\Dir;

return [
'description' => 'Upgrades a Kirby plugin',
'args' => [
'repo' => [
'description' => 'The Kirby plugin registry name (i.e. getkirby/kql)',
'required' => true
],
'version' => [
'description' => 'The version corresponding with the tag name in the repo',
'defaultValue' => 'latest'
]
],
'command' => static function (CLI $cli): void {
$repo = $cli->arg('repo');
$version = $cli->arg('version');

if ($plugin = $cli->kirby()->plugin($repo)) {
try {
// move plugin directory to prevent overwrite
Dir::move($plugin->root(), $plugin->root() . '.bak');
$cli->run('plugin:install', $repo, $version);
Dir::remove($plugin->root() . '.bak');
$cli->success('The ' . $repo . ' plugin has been updated to ' . $version . ' version');
} catch (Throwable) {
Dir::move($plugin->root() . '.bak', $plugin->root());
$cli->error('The ' . $repo . ' plugin could not updated');
}
} else {
$cli->error('The ' . $repo . ' plugin could not found');
}
}
];
Loading