Skip to content

Commit

Permalink
Merge pull request #63 from getkirby/feature/plugin
Browse files Browse the repository at this point in the history
New `plugin` commands
  • Loading branch information
bastianallgeier authored Feb 28, 2024
2 parents 73827af + 9633337 commit 956ae92
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
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 [
'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');
}
}
];

0 comments on commit 956ae92

Please sign in to comment.