From 669256f35c7a23b6105f0c625cba6b0a3c7085bb Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Thu, 22 Feb 2024 22:54:44 +0300 Subject: [PATCH 1/8] New `plugin:install` command --- commands/plugin/install.php | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 commands/plugin/install.php diff --git a/commands/plugin/install.php b/commands/plugin/install.php new file mode 100644 index 0000000..bc3ba34 --- /dev/null +++ b/commands/plugin/install.php @@ -0,0 +1,52 @@ + 'Installs a kirby plugin repository from the 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'); + $archive = 'https://github.com/' . $repo . '/archive'; + + if ($version === 'latest') { + $url = $archive . '/main.zip'; + } else { + $url = $archive . '/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 ' . $version . ' version'); + } +]; From 4e20dab9a854774d5dd82bdda7e1128a454102d9 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Fri, 23 Feb 2024 01:58:18 +0300 Subject: [PATCH 2/8] New `plugin:remove` command --- commands/plugin/remove.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 commands/plugin/remove.php diff --git a/commands/plugin/remove.php b/commands/plugin/remove.php new file mode 100644 index 0000000..5bad099 --- /dev/null +++ b/commands/plugin/remove.php @@ -0,0 +1,26 @@ + '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 found'); + } + } +]; From b76128a7a3239108c568bb77679eecea468adaa1 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Fri, 23 Feb 2024 10:48:51 +0300 Subject: [PATCH 3/8] New `plugin:upgrade` command --- commands/plugin/upgrade.php | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 commands/plugin/upgrade.php diff --git a/commands/plugin/upgrade.php b/commands/plugin/upgrade.php new file mode 100644 index 0000000..6f75b9c --- /dev/null +++ b/commands/plugin/upgrade.php @@ -0,0 +1,39 @@ + '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'); + } + } +]; From eafcb5bf23ccb50c2a88984b73fb311cfc4593cf Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Fri, 23 Feb 2024 10:53:16 +0300 Subject: [PATCH 4/8] Add plugin commands --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index d0e86ee..5585f07 100644 --- a/README.md +++ b/README.md @@ -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 From afbc5c32a61bd2b3cb37b9f667b7fbb303ba3f16 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Wed, 28 Feb 2024 11:27:17 +0100 Subject: [PATCH 5/8] Small wording adjustments for the install command --- commands/plugin/install.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/plugin/install.php b/commands/plugin/install.php index bc3ba34..97a26fc 100644 --- a/commands/plugin/install.php +++ b/commands/plugin/install.php @@ -6,7 +6,7 @@ use Kirby\Filesystem\F; return [ - 'description' => 'Installs a kirby plugin repository from the Github', + 'description' => 'Installs a kirby plugin repository from Github', 'args' => [ 'repo' => [ 'description' => 'The Github repo path (i.e. getkirby/kql)', @@ -47,6 +47,6 @@ // remove the zip F::unlink($zip); - $cli->success('The ' . $repo . ' plugin has been installed ' . $version . ' version'); + $cli->success('The ' . $repo . ' plugin has been installed'); } ]; From 95872d4b954cffb01cd627a656ed451b2ccc9b77 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Wed, 28 Feb 2024 11:31:16 +0100 Subject: [PATCH 6/8] Fix wording in the remove command --- commands/plugin/remove.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/plugin/remove.php b/commands/plugin/remove.php index 5bad099..8a42d12 100644 --- a/commands/plugin/remove.php +++ b/commands/plugin/remove.php @@ -20,7 +20,7 @@ Dir::remove($plugin->root()); $cli->success('The ' . $repo . ' plugin has been removed'); } else { - $cli->error('The ' . $repo . ' plugin could not found'); + $cli->error('The ' . $repo . ' plugin could not be found'); } } ]; From 4a179b4dcc07dcd04dfb04414c32ad00070c45c3 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Wed, 28 Feb 2024 13:53:01 +0300 Subject: [PATCH 7/8] Make sure only `kirby-plugin` installable --- commands/plugin/install.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/commands/plugin/install.php b/commands/plugin/install.php index 97a26fc..1d4b8d4 100644 --- a/commands/plugin/install.php +++ b/commands/plugin/install.php @@ -18,14 +18,21 @@ ] ], 'command' => static function (CLI $cli): void { - $repo = $cli->arg('repo'); - $version = $cli->arg('version'); - $archive = 'https://github.com/' . $repo . '/archive'; + $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 = $archive . '/main.zip'; + $url = $archiveUrl . '/main.zip'; } else { - $url = $archive . '/refs/tags/' . $cli->arg('version') . '.zip'; + $url = $archiveUrl . '/refs/tags/' . $cli->arg('version') . '.zip'; } list($vendor, $plugin) = explode('/', $repo); From 963333722db6de89b216a38c68fab37d76b64bb0 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Wed, 28 Feb 2024 12:48:08 +0100 Subject: [PATCH 8/8] Use uppercase for Kirby --- commands/plugin/install.php | 4 ++-- commands/plugin/remove.php | 2 +- commands/plugin/upgrade.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commands/plugin/install.php b/commands/plugin/install.php index 1d4b8d4..2882ce7 100644 --- a/commands/plugin/install.php +++ b/commands/plugin/install.php @@ -6,7 +6,7 @@ use Kirby\Filesystem\F; return [ - 'description' => 'Installs a kirby plugin repository from Github', + 'description' => 'Installs a Kirby plugin repository from Github', 'args' => [ 'repo' => [ 'description' => 'The Github repo path (i.e. getkirby/kql)', @@ -26,7 +26,7 @@ // 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'); + throw new Exception('The GitHub repository should be a Kirby plugin'); } if ($version === 'latest') { diff --git a/commands/plugin/remove.php b/commands/plugin/remove.php index 8a42d12..6129fcd 100644 --- a/commands/plugin/remove.php +++ b/commands/plugin/remove.php @@ -6,7 +6,7 @@ use Kirby\Filesystem\Dir; return [ - 'description' => 'Removes a kirby plugin', + 'description' => 'Removes a Kirby plugin', 'args' => [ 'repo' => [ 'description' => 'The Kirby plugin registry name (i.e. getkirby/kql)', diff --git a/commands/plugin/upgrade.php b/commands/plugin/upgrade.php index 6f75b9c..e5c40ad 100644 --- a/commands/plugin/upgrade.php +++ b/commands/plugin/upgrade.php @@ -6,7 +6,7 @@ use Kirby\Filesystem\Dir; return [ - 'description' => 'Upgrades a kirby plugin', + 'description' => 'Upgrades a Kirby plugin', 'args' => [ 'repo' => [ 'description' => 'The Kirby plugin registry name (i.e. getkirby/kql)',