Skip to content

Commit

Permalink
feat: kite plug run support match keywords for run a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 27, 2021
1 parent e9bfc05 commit b7936de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
23 changes: 20 additions & 3 deletions app/Console/Controller/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Inhere\Console\IO\Output;
use Inhere\Kite\Kite;
use InvalidArgumentException;
use Toolkit\Cli\Util\LineParser;
use Toolkit\PFlag\FlagsParser;
use function array_keys;

Expand All @@ -33,6 +34,13 @@ public static function aliases(): array
return ['plugins', 'plug'];
}

protected static function commandAliases(): array
{
return [
'info' => ['show'],
];
}

/**
* list all plugins dir and file information
*
Expand Down Expand Up @@ -87,7 +95,7 @@ public function infoCommand(FlagsParser $fs, Output $output): void
* name The plugin name for run
*
* @options
* -i, --select bool;Run plugin by select
* -i, -s, --select bool;select a plugin in plugin list for run
*
* @param FlagsParser $fs
* @param Output $output
Expand All @@ -100,16 +108,25 @@ public function runCommand(FlagsParser $fs, Output $output): void
$files = $kpm->loadPluginFiles()->getPluginFiles();

$list = array_keys($files);
$name = $output->select('select an plugin', $list, null, true, [
$name = $output->select('select an plugin for run', $list, null, true, [
'returnVal' => true,
]);

$line = $output->ask("please provide args for the plugin '$name'", '-h');
$args = LineParser::parseIt($line);

$output->title("Run the plugin $name", [
'ucWords' => false,
]);
} else {
$name = $fs->getArg('name');
if (!$name) {
throw new InvalidArgumentException('must provide plugin name for run');
}

$args = $fs->getRemainArgs();
}

$kpm->run($name, $this->app, $fs->getRemainArgs());
$kpm->run($name, $this->app, $args);
}
}
35 changes: 30 additions & 5 deletions app/Console/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
use Inhere\Console\Util\Show;
use RuntimeException;
use SplFileInfo;
use Toolkit\Cli\Cli;
use Toolkit\Cli\Color;
use Toolkit\FsUtil\Dir;
use Toolkit\Stdlib\Obj;
use Toolkit\Stdlib\Str;
use function array_keys;
use function array_search;
use function basename;
use function class_exists;
use function count;
use function is_dir;
use function is_file;
use function key;
use function rtrim;
use function str_contains;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function trim;

Expand All @@ -42,17 +49,17 @@ class PluginManager
*
* @var AbstractPlugin[]
*/
private $plugins = [];
private array $plugins = [];

/**
* @var array
*/
private array $pluginDirs = [];

/**
* @var array
* @var array<string, string>
*/
private $pluginFiles = [];
private array $pluginFiles = [];

/**
* @var array
Expand Down Expand Up @@ -88,7 +95,25 @@ public function run(string $name, Application $app, array $args = []): void
{
$plugin = $this->getPlugin($name);
if (!$plugin) {
throw new RuntimeException("the plugin '$name' is not exists. plugin: ");
$this->loadPluginFiles();

$matched = [];
foreach ($this->pluginFiles as $plugName => $pluginFile) {
// if (str_contains(strtolower($plugName), strtolower($name))) {
if (Str::ihas($plugName, $name)) {
$matched[$plugName] = $pluginFile;
}
}

// if can match only one, run it.
if (count($matched) === 1) {
$pluginName = (string)key($matched);
Cli::info("[Tips] auto match and run the plugin: $pluginName");

$plugin = $this->getPlugin($pluginName);
} else {
throw new RuntimeException("the plugin '$name' is not exists.");
}
}

$plugin->run($app, $args);
Expand Down Expand Up @@ -142,7 +167,7 @@ public function isPlugin(string $name): bool
$name = trim($name);
$name = trim($name, '/');

if (!$name || strpos($name, ' ') !== false) {
if (!$name || str_contains($name, ' ')) {
return false;
}

Expand Down

0 comments on commit b7936de

Please sign in to comment.