Skip to content

Commit

Permalink
refactor: refactor the plugin run logic and update some command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 24, 2021
1 parent aaea95c commit a5dc380
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 131 deletions.
5 changes: 5 additions & 0 deletions app/Console/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ protected function registerServices(ObjectBox $box): void
$jumpConf = $this->getArrayParam('jumper');
return QuickJump::new($jumpConf);
});

// $box->set('envLoader', function () {
// $jumpConf = $this->getArrayParam('osEnv');
// return QuickJump::new($jumpConf);
// });
}

protected function initAppRun(): void
Expand Down
31 changes: 23 additions & 8 deletions app/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Show;
use Inhere\Kite\Component\ScriptRunner;
use Inhere\Kite\Kite;
use Toolkit\Stdlib\OS;
use function count;
use function is_array;
use function vdump;

/**
* Class RunCommand
Expand Down Expand Up @@ -51,7 +54,7 @@ protected function beforeExecute(): bool
* -l, --list List information for all scripts or script files. type: file, cmd(default)
* -s, --search Display all matched scripts by the input name
* --info Show information for give script name or file
* --dry-run bool;Mock running an script
* --dry-run bool;Mock running, not real execute.
* --proxy bool;Enable proxy ENV setting
*
* @arguments
Expand All @@ -65,7 +68,7 @@ protected function beforeExecute(): bool
*/
protected function execute(Input $input, Output $output)
{
$name = $this->flags->getFirstArg();
$name = $this->flags->getArg('name');

$listType = $this->flags->getOpt('list');
if ($listType === ScriptRunner::TYPE_FILE) {
Expand All @@ -87,18 +90,30 @@ protected function execute(Input $input, Output $output)
}

if (!$name) {
$output->liteError('please input an script name for run or use -l TYPE see all scripts');
$output->liteError('please input an name for run or use -l TYPE see all scripts');
return;
}

$runArgs = $input->getArguments();
unset($runArgs[0]); // first is script name

$dryRun = $this->flags->getOpt('dry-run');
$this->sr->setDryRun($dryRun);

// not found script name
// proxy
$openProxy = $this->flags->getOpt('proxy');
$proxyEnv = $this->app->getArrayParam('proxyEnv');
if ($openProxy && $proxyEnv) {
Show::aList($proxyEnv, 'Set Proxy ENV From Config: "proxyEnv"', [
'ucFirst' => false,
'ucTitleWords' => false,
]);

OS::setEnvVars($proxyEnv);
}

// $runArgs = $this->flags->getRawArgs(); // 会包含 arg: name
$runArgs = $this->flags->getRemainArgs();

if (!$this->sr->isScriptName($name)) {
// - found script file
if ($scriptFile = $this->sr->findScriptFile($name)) {
$this->sr->runScriptFile($scriptFile, $runArgs);
return;
Expand All @@ -107,7 +122,7 @@ protected function execute(Input $input, Output $output)
// - is an plugin
if (Kite::plugManager()->isPlugin($name)) {
$output->notice("input is an plugin name, will run plugin: $name");
Kite::plugManager()->run($name, $this->app);
Kite::plugManager()->run($name, $this->app, $runArgs);
return;
}

Expand Down
30 changes: 20 additions & 10 deletions app/Console/Component/RedirectToGitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Inhere\Kite\Console\Component;

use Inhere\Console\Application;
use Inhere\Console\Console;
use Inhere\Console\Controller;
use Inhere\Kite\Console\Controller\GitController;
use Throwable;
use Toolkit\Cli\Cli;
use Toolkit\Stdlib\Obj\AbstractObj;
use function array_push;
use function in_array;

/**
Expand All @@ -21,25 +21,35 @@ class RedirectToGitGroup extends AbstractObj
public $cmdList = [];

/**
* @param Application $app
* @param Controller $ctrl
* @param string $action
* @param array $args
*
* @return bool
* @throws Throwable
*/
public function handle(Application $app, string $action, array $args):bool
public function handle(Controller $ctrl, string $action, array $args): bool
{
$app = $ctrl->getApp();

// resolve alias
$gitCtrl = $app->getController(GitController::getName());
$command = $gitCtrl->resolveAlias($action);

$redirectList = $this->cmdList;
if (in_array($command, $redirectList, true)) {
Cli::info("NOTICE: will redirect to git group for run `git $command`");
// Console::app()->dispatch("git:$command");
// Console::app()->dispatch("git:$command", $this->flags->getRawArgs());
Console::app()->dispatch("git:$command", $args);
if (in_array($command, $this->cmdList, true)) {
Cli::info("NOTICE: will redirect to git group for run subcommand: $command");

$newArgs = [];
if ($ctrl->getFlags()->getOpt('dry-run')) {
$newArgs[] = '--dry-run';
}

$newArgs[] = $command;
// append remaining args
array_push($newArgs, ...$args);

$app->dispatch('git', $newArgs);
// $app->dispatch("git:$command", $args);
return true;
}

Expand Down
56 changes: 42 additions & 14 deletions app/Console/Controller/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Inhere\Console\IO\Output;
use Inhere\Kite\Common\Cmd;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Common\GitLocal\GitHub;
use Inhere\Kite\Console\Manage\GitBranchManage;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
Expand Down Expand Up @@ -146,7 +147,7 @@ protected function onNotFound(string $action, array $args): bool
* update codes from origin by git pull
*
* @options
* --dir The want updated git repo dir. default is workdir
* --dir The want updated git repo dir. default is workdir
*
* @arguments
* gitArgs Input more args or opts for run git
Expand All @@ -156,25 +157,21 @@ protected function onNotFound(string $action, array $args): bool
* @param Output $output
*
* @example
* {binWithCmd} --all -f --unshallow
* {binWithCmd} --dir /path/to/mydir -- --all -f --unshallow
* {binWithCmd} --all -f --unshallow
* {binWithCmd} --all -f --unshallow
* {binWithCmd} --dir /path/to/mydir -- --all -f --unshallow
*/
public function updateCommand(FlagsParser $fs, Input $input, Output $output): void
{
$repoDir = $fs->getOpt('dir') ?: $input->getWorkDir();

$dir = $fs->getOpt('dir') ?: $input->getWorkDir();
$args = $fs->getRawArgs();

$c = Cmd::git('pull');
$c->setWorkDir($repoDir);
$c->setWorkDir($dir);
$c->setDryRun($this->flags->getOpt('dry-run'));
$c->addArgs(...$args);
$c->run(true);

// $runner = CmdRunner::new();
// $runner->setDryRun($input->getBoolOpt('dry-run'));
// $runner->add('git pull');
// $runner->runAndPrint();

$output->success('Complete');
}

Expand Down Expand Up @@ -378,10 +375,11 @@ public function openCommand(FlagsParser $fs, Output $output): void
* Clone an remote git repository to local
*
* @options
* --gh bool;Define the remote repository is on github
* --gh bool;Define the remote repository is on github
* -w, --workdir The clone work dir, default is current dir.
*
* @arguments
* repo The remote git repo URL or repository name
* repo string;The remote git repo URL or repository name;required
* name The repository name at local, default is same `repo`
*
* @param FlagsParser $fs
Expand All @@ -394,7 +392,37 @@ public function openCommand(FlagsParser $fs, Output $output): void
*/
public function cloneCommand(FlagsParser $fs, Output $output): void
{
$output->success('TODO');
$repo = $fs->getArg('repo');
$name = $fs->getArg('name');
$args = $fs->getRawArgs();

$dir = $fs->getOpt('workdir');

$c = Cmd::git('clone');
$c->setWorkDir($dir);
$c->setDryRun($this->flags->getOpt('dry-run'));

if ($fs->getOpt('gh')) {
$gh = GitHub::new($output);

$repoUrl = $gh->parseRepoUrl($repo);
if (!$repoUrl) {
$output->error("invalid github 'repo' address: $repo");
return;
}
} else {
$repoUrl = $repo;
}

$c->add($repoUrl);
$c->addIf($name, $name);
if ($args) {
$c->addArgs(...$args);
}

$c->runAndPrint();

$output->success('Complete');
}

/**
Expand Down
47 changes: 14 additions & 33 deletions app/Console/Controller/GitHubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Inhere\Kite\Console\Controller;

use Inhere\Console\Console;
use Inhere\Console\Controller;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
Expand All @@ -21,7 +20,6 @@
use PhpComp\Http\Client\Client;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use function in_array;
use function strtoupper;

/**
Expand Down Expand Up @@ -56,6 +54,18 @@ protected static function commandAliases(): array
];
}

/**
* @return string[]
*/
protected function options(): array
{
return [
'--dry-run' => 'bool;Dry-run the workflow, dont real execute',
// '-y, --yes' => 'Direct execution without confirmation',
// '-i, --interactive' => 'Run in an interactive environment[TODO]',
];
}

/**
* @return GitHub
*/
Expand Down Expand Up @@ -91,38 +101,9 @@ protected function onNotFound(string $action, array $args): bool
'cmdList' => $this->settings['redirectGit'] ?? [],
]);

return $h->handle($this->app, $action, $args);

// resolve alias
// $gitCtrl = $this->app->getController(GitController::getName());
// $command = $gitCtrl->resolveAlias($action);
//
// $redirectGitGroup = $this->settings['redirectGit'] ?? [];
// if (in_array($command, $redirectGitGroup, true)) {
// $this->output->notice("will redirect to git group for run `git $command`");
// // Console::app()->dispatch("git:$command");
// Console::app()->dispatch("git:$command", $args);
// return true;
// }
//
// return false;
return $h->handle($this, $action, $args);
}

// protected function beforeAction(): bool
// {
// if ($this->app) {
// $action = $this->getAction();
//
// $loadEnvActions = $this->settings['loadEnvOn'] ?? [];
// if ($loadEnvActions && in_array($action, $loadEnvActions, true)) {
// $this->output->info(self::getName() . ' - will load osEnv setting for command: ' . $action);
// AppHelper::loadOsEnvInfo($this->app);
// }
// }
//
// return true;
// }

/**
* Show a list of commands that will be redirected to git
*
Expand Down Expand Up @@ -244,7 +225,7 @@ public function openCommand(FlagsParser $fs, Output $output): void
* Clone an github repository to local
*
* @options
* -w, --workdir The clone work dir, defualt is current dir.
* -w, --workdir The clone work dir, default is current dir.
*
* @arguments
* repo string;The remote git repo URL or repository name;required
Expand Down
28 changes: 7 additions & 21 deletions app/Console/Controller/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,7 @@ protected function onNotFound(string $action, array $args): bool
'cmdList' => $this->settings['redirectGit'] ?? [],
]);

return $h->handle($this->app, $action, $args);
// resolve alias
// $gitCtrl = $this->app->getController(GitController::getName());
// $command = $gitCtrl->resolveAlias($action);
//
// $redirectGitGroup = $this->settings['redirectGit'] ?? [];
// if (in_array($command, $redirectGitGroup, true)) {
// $this->output->notice("will redirect to git group for run `git $command`");
// // Console::app()->dispatch("git:$command");
// // Console::app()->dispatch("git:$command", $this->flags->getRawArgs());
// Console::app()->dispatch("git:$command", $args);
// return true;
// }
//
// return false;
return $h->handle($this, $action, $args);
}

/**
Expand Down Expand Up @@ -586,19 +572,19 @@ public function resolveCommand(FlagsParser $fs, Output $output): void
* -t, --target The target branch name
* -o, --open Open the generated PR link on browser
* -d, --direct bool;The PR is direct from fork to main repository
* --new Open new pr page on browser http://my.gitlab.com/group/repo/merge_requests/new
* --new bool;Open new pr page on browser. eg: http://my.gitlab.com/group/repo/merge_requests/new
*
* @argument
* project The project key in 'gitlab' config. eg: group-name, name
* project The project key in 'gitlab' config. eg: group-name, name
*
* @param FlagsParser $fs
* @param Output $output
*
* @example
* {binWithCmd} Will generate PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH'
* {binWithCmd} -s 4_16 -t qa Will generate PR link for main 'PREFIX_4_16' to main 'qa'
* {binWithCmd} -t qa Will generate PR link for main 'HEAD_BRANCH' to main 'qa'
* {binWithCmd} -t qa --direct Will generate PR link for fork 'HEAD_BRANCH' to main 'qa'
* {binWithCmd} Will generate PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH'
* {binWithCmd} -s 4_16 -t qa Will generate PR link for main 'PREFIX_4_16' to main 'qa'
* {binWithCmd} -t qa Will generate PR link for main 'HEAD_BRANCH' to main 'qa'
* {binWithCmd} -t qa --direct Will generate PR link for fork 'HEAD_BRANCH' to main 'qa'
*/
public function pullRequestCommand(FlagsParser $fs, Output $output): void
{
Expand Down
Loading

0 comments on commit a5dc380

Please sign in to comment.