-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
485 additions
and
114 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
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,76 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* This file is part of Kite. | ||
* | ||
* @link https://github.com/inhere | ||
* @author https://github.com/inhere | ||
* @license MIT | ||
*/ | ||
|
||
namespace Inhere\Kite\Console\Controller; | ||
|
||
use Inhere\Console\Controller; | ||
use Inhere\Console\IO\Input; | ||
use Inhere\Console\IO\Output; | ||
use Inhere\Kite\Kite; | ||
|
||
/** | ||
* Class PluginController | ||
*/ | ||
class PluginController extends Controller | ||
{ | ||
protected static $name = 'plugin'; | ||
|
||
protected static $description = 'kite plugins manage tools'; | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function aliases(): array | ||
{ | ||
return ['plugins', 'plug']; | ||
} | ||
|
||
/** | ||
* run an plugin by input name | ||
* | ||
* @arguments | ||
* name The plugin name for run | ||
* | ||
* @param Input $input | ||
* @param Output $output | ||
*/ | ||
public function runCommand(Input $input, Output $output): void | ||
{ | ||
$input->bindArgument('name', 0); | ||
$name = $input->getRequiredArg('name'); | ||
|
||
$kpm = Kite::plugManager(); | ||
$kpm->run($name, $this->app); | ||
|
||
$output->success('completed'); | ||
} | ||
|
||
/** | ||
* list all plugins dir and file information | ||
* | ||
* @options | ||
* --only-files Only list all plugin names | ||
* | ||
* @param Input $input | ||
* @param Output $output | ||
*/ | ||
public function listCommand(Input $input, Output $output): void | ||
{ | ||
$kpm = Kite::plugManager(); | ||
$opts = ['ucFirst' => false]; | ||
|
||
if (!$input->getBoolOpt('only-files')) { | ||
$dirs = $kpm->getPluginDirs(); | ||
$output->aList($dirs, 'plugin dirs', $opts); | ||
} | ||
|
||
$files = $kpm->loadPluginFiles()->getPluginFiles(); | ||
$output->aList($files, 'Plugin Files', $opts); | ||
} | ||
} |
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,86 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Inhere\Kite\Console\Listener; | ||
|
||
use Inhere\Kite\Common\CmdRunner; | ||
use Inhere\Kite\Console\Application; | ||
|
||
/** | ||
* Class NotFoundListener | ||
* | ||
* @package Inhere\Kite\Console\Listener | ||
*/ | ||
final class NotFoundListener | ||
{ | ||
/** | ||
* @param string $cmd | ||
* @param Application $app | ||
* | ||
* @return bool | ||
*/ | ||
public function __invoke(string $cmd, Application $app): bool | ||
{ | ||
$aliases = $app->getParam('aliases', []); | ||
|
||
// - is an command alias. | ||
if ($aliases && isset($aliases[$cmd])) { | ||
$realCmd = $aliases[$cmd]; | ||
|
||
$app->notice("input command is alias name, will redirect to the real command '$realCmd'"); | ||
$app->dispatch($realCmd); | ||
return true; | ||
} | ||
|
||
// check custom scripts | ||
$scripts = $app->getParam('scripts', []); | ||
if (!$scripts || !isset($scripts[$cmd])) { | ||
// - run plugin | ||
if ($app->getPlugManager()->isPlugin($cmd)) { | ||
$app->notice("input is an plugin name, will run plugin: $cmd"); | ||
$app->getPlugManager()->run($cmd, $app); | ||
return true; | ||
} | ||
|
||
// - call system command. | ||
$this->callSystemCmd($cmd, $app); | ||
return true; | ||
} | ||
|
||
// - run custom scripts. | ||
$this->runCustomScript($cmd, $app); | ||
return true; | ||
} | ||
|
||
/** | ||
* @param string $cmd | ||
* @param Application $app | ||
*/ | ||
private function callSystemCmd(string $cmd, Application $app): void | ||
{ | ||
if ($cmd[0] === '\\') { | ||
$cmd = substr($cmd, 1); | ||
} | ||
|
||
$cmdLine = $app->getInput()->getFullScript(); | ||
$app->notice("input command is not found, will call system command: $cmdLine"); | ||
|
||
// call system command | ||
CmdRunner::new($cmdLine)->do(true); | ||
} | ||
|
||
/** | ||
* @param string $cmd | ||
* @param Application $app | ||
*/ | ||
private function runCustomScript(string $cmd, Application $app): void | ||
{ | ||
/** @see \Inhere\Kite\Console\Command\RunCommand::execute() */ | ||
$app->note("command not found, redirect to run script: $cmd"); | ||
|
||
$args = $app->getInput()->getArgs(); | ||
$args = array_merge([$cmd], $args); | ||
|
||
$app->getInput()->setArgs($args, true); | ||
$app->dispatch('run'); | ||
} | ||
} |
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,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Inhere\Kite\Console\Plugin; | ||
|
||
use Inhere\Kite\Console\Application; | ||
|
||
/** | ||
* Class AbstractPlugin | ||
* | ||
* @package Inhere\Kite\Plugin | ||
*/ | ||
abstract class AbstractPlugin | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function metadata(): array | ||
{ | ||
return [ | ||
// 'author' => 'inhere', | ||
// 'version' => '', | ||
// 'desc' => '', | ||
]; | ||
} | ||
|
||
/** | ||
* @param Application $app | ||
*/ | ||
public function run(Application $app): void | ||
{ | ||
$this->exec($app); | ||
} | ||
|
||
/** | ||
* @param Application $app | ||
*/ | ||
abstract public function exec(Application $app): void; | ||
} |
Oops, something went wrong.