-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from getkirby/feature/plugin
New `plugin` commands
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
]; |