Skip to content

Commit

Permalink
update some plugin logic and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 27, 2021
1 parent 79accc0 commit 14f6bb1
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 10 deletions.
83 changes: 80 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ kite env
kite env path
```

## 使用简单脚本
## 扩展使用

### 使用简单脚本

除了使用内部提供的命令,`kite` 也提供了快速的 `scripts` 脚本配置。

Expand Down Expand Up @@ -298,7 +300,7 @@ kite gst

![scripts-gst](resource/images/scripts-gst.png)

## 命令别名配置
### 命令别名配置

默认的命令别名请看 [config/config.php](config/config.php) 文件中的 `aliaes` 配置

Expand All @@ -316,7 +318,82 @@ kite gst

![self-config-aliases](resource/images/self-config-aliases.png)

## 更新
### 命令插件

kite 里除了提供 `scripts` 访问执行外部命令,还可以编写自定义插件命令实现一些自定义功能

**配置**

首先配置插件目录,支持配置多个目录。

```php
'pluginDirs' => [
'~/.kite/plugin/'
],
```

**编写插件命令**

每个插件命令文件都是一个 php 类文件,需要继承 `Inhere\Kite\Console\Plugin\AbstractPlugin`

这是 [demo-pulgin](plugin/demo-plugin.php) 文件示例:

```php
<?php

use Inhere\Console\IO\Input;
use Inhere\Kite\Console\Application;
use Inhere\Kite\Console\Plugin\AbstractPlugin;

/**
* Class DemoPlugin
*/
class DemoPlugin extends AbstractPlugin
{
public function metadata(): array
{
return [
'desc' => 'this is am demo plugin',
];
}

public function exec(Application $app, Input $input): void
{
vdump(__METHOD__);
}
}
```

> 注意:插件文件名必须跟里面的插件类保持一致。kite会自动将插件名进行驼峰格式转换当做类名称使用,因此可以在文件名使用连字符 `-`
**运行插件命令**

kite 通过内部的 `plugin run` 命令紧跟插件名或者插件路径来运行一个插件。

```bash
kite plugin run PLUGIN_NAME
```

kite将会在插件目录里找到对应插件文件并执行它的 `exec` 方法。如下面的命令就会找到 `demo-plugin` 并运行它。

```bash
kite plugin run demo-plugin
```

可以省略 `plugin run` 直接跟插件名称来快速执行插件。

> TIPS: 其原理跟运行script类似,kite找不到命令,就会自动尝试检查是否是一个插件命令,能找到插件文件,就会当做插件执行。
```bash
# 后缀 .php 可以忽略
kite demo-plugin
kite demo-plugin.php
# 直接写完整路径也是可以的
kite plugin/demo-plugin
kite plugin/demo-plugin.php
```

## 更新kite

**内置命令**

Expand Down
5 changes: 3 additions & 2 deletions app/Console/Controller/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public function infoCommand(Input $input, Output $output): void
return;
}

$opts = ['ucFirst' => false];
$output->aList($plg->getInfo(), 'Plugin Info', $opts);
// $opts = ['ucFirst' => false];
// $output->aList($plg->getInfo(), 'Plugin Info', $opts);
$kpm->showInfo($plg);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/SelfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SelfController extends Controller
{
protected static $name = 'self';

protected static $description = 'Operate Kite self commands';
protected static $description = 'Operate and manage kite self commands';

/**
* @var string
Expand Down
23 changes: 23 additions & 0 deletions app/Console/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public function getInfo(): array
];
}

/**
* @return array
*/
public function getHelpInfo(): array
{
// $meta = $this->metadata();

return [
'name' => $this->name,
// 'desc' => $meta['desc'] ?? '',
'class' => $this->classname,
'path' => $this->filepath,
];
}

/**
* @param Application $app
* @param Input $input
Expand All @@ -90,6 +105,14 @@ public function getName(): string
return $this->name;
}

/**
* @return string
*/
public function getDesc(): string
{
return $this->metadata()['desc'] ?? 'no description';
}

/**
* @param string $name
*/
Expand Down
31 changes: 29 additions & 2 deletions app/Console/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Inhere\Kite\Console\Plugin;

use Inhere\Console\Util\Helper;
use Inhere\Console\Util\Show;
use Inhere\Kite\Console\Application;
use RuntimeException;
use SplFileInfo;
use Toolkit\Cli\Color;
use Toolkit\Stdlib\Str;
use function basename;
use function class_exists;
Expand Down Expand Up @@ -81,7 +83,25 @@ public function run(string $name, Application $app): void
throw new RuntimeException('the plugin is not exists. plugin: ' . $name);
}

$plugin->run($app, $app->getInput());
$input = $app->getInput();
if ($input->getSameBoolOpt('h,help')) {
$this->showInfo($plugin);
return;
}

$plugin->run($app, $input);
}

/**
* @param AbstractPlugin $plugin
*/
public function showInfo(AbstractPlugin $plugin): void
{
Color::println($plugin->getName() . ':', 'comment');
Color::println(' ' . $plugin->getDesc());

Show::aList($plugin->getHelpInfo(), 'Information');

}

/**
Expand Down Expand Up @@ -161,14 +181,21 @@ protected function loadPlugin(string $name): ?AbstractPlugin
protected function loadPluginFile(string $name): bool
{
// is an exists php file
if (Str::has($name, '.php') && is_file($name)) {
$hasPhpSuffix = Str::has($name, '.php');
if ($hasPhpSuffix && is_file($name)) {
// $path = $name;
// $name = substr($name, 0, -4);

$this->pluginFiles[$name] = $name;
return true;
}

// `$name . '.php'` is an exists php file
if (!$hasPhpSuffix && is_file($name . '.php')) {
$this->pluginFiles[$name] = $name . '.php';
return true;
}

// find in all plugin dirs
$founded = false;
foreach ($this->pluginDirs as $dir) {
Expand Down
2 changes: 1 addition & 1 deletion app/Console/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$app->registerCommands('Inhere\\Kite\\Console\\Command', __DIR__ . '/Command');
$app->registerGroups('Inhere\\Kite\\Console\\Controller', __DIR__ . '/Controller');

$app->addCommand(Inhere\Console\BuiltIn\DevServerCommand::class);
// $app->addCommand(Inhere\Console\BuiltIn\DevServerCommand::class);

// internal group
$app->addController(PharController::class);
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
'scripts' => require 'scripts.php',

'pluginDirs' => [
// BASE_PATH . '/plugin/'
// '/plugin/'
],
];

0 comments on commit 14f6bb1

Please sign in to comment.