From f81002653f84dd24112ec073aa615228537dd9a0 Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 24 May 2022 19:29:27 +0800 Subject: [PATCH] up: update some for the tool manage and install logic --- app/Console/Command/ToolCommand.php | 6 +- app/Console/Component/AutoSetProxyEnv.php | 15 ++++ app/Console/Model/BinTool.php | 74 +++++++++++++------ app/Console/SubCmd/ToolCmd/InstallCommand.php | 7 +- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/app/Console/Command/ToolCommand.php b/app/Console/Command/ToolCommand.php index bff8072..939b2a4 100644 --- a/app/Console/Command/ToolCommand.php +++ b/app/Console/Command/ToolCommand.php @@ -24,6 +24,9 @@ */ class ToolCommand extends Command { + public const OPT_DRY_RUN = 'dry-run'; + public const OPT_PROXY_ENV = 'proxy-env'; + protected static string $name = 'tool'; protected static string $desc = 'some little tool commands'; @@ -41,7 +44,8 @@ protected function subCommands(): array protected function configure(): void { - $this->flags->addOptByRule('dry-run,try', 'bool;Dry-run the workflow, dont real execute'); + $this->flags->addOptByRule(self::OPT_DRY_RUN . ',try', 'bool;Dry-run the workflow, dont real execute'); + $this->flags->addOptByRule(self::OPT_PROXY_ENV, 'bool;open proxy env settings on run command'); } /** diff --git a/app/Console/Component/AutoSetProxyEnv.php b/app/Console/Component/AutoSetProxyEnv.php index ddba9fa..03d3b79 100644 --- a/app/Console/Component/AutoSetProxyEnv.php +++ b/app/Console/Component/AutoSetProxyEnv.php @@ -87,6 +87,21 @@ public function applyProxyEnv(string $realCName, string $realGName = ''): bool return false; } + /** + * @param string $cmdId + * + * @return bool + */ + public function directApply(string $cmdId): bool + { + if (!$this->envSettings) { + return false; + } + + $this->setProxyEnv($this->envSettings, $cmdId); + return true; + } + /** * @param array $settings * @param string $command diff --git a/app/Console/Model/BinTool.php b/app/Console/Model/BinTool.php index 6761c2c..ad59604 100644 --- a/app/Console/Model/BinTool.php +++ b/app/Console/Model/BinTool.php @@ -89,14 +89,14 @@ public function buildCmdRunner(string $command): CmdRunner public function run(string $command, CmdRunner $runner): void { if ($tips = $this->getBeforeTip($command)) { - Cli::cyan('Beginning.tips:'); + Cli::info('Beginning.tips:'); Cli::writeln($tips); } $runner->runAndPrint(); if ($tips = $this->getAfterTip($command)) { - Cli::cyan('Complete.tips:'); + Cli::magenta("\n[Complete.tips]:"); Cli::writeln($tips); } } @@ -109,17 +109,33 @@ public function run(string $command, CmdRunner $runner): void public function getCmdScripts(string $command): string|array { $this->mustCommand($command); - $name = $this->name; - $cmd = $this->commands[$command]; + switch ($command) { + case 'install': + $info = $this->install; + $cmd = $info['run']; + break; + case 'update': + $info = $this->update; + $cmd = $info['run']; + break; + case 'remove': + $info = $this->remove; + $cmd = $info['run']; + break; + default: + $cmd = $this->commands[$command]; + } + + $name = $this->name; if (is_string($cmd) && str_starts_with($cmd, '@')) { - $refCmd = substr($cmd, 1); - if (!$this->hasCommand($refCmd)) { - throw new InvalidArgumentException("not found refer command '$refCmd' in the tool '$name'"); + $refName = substr($cmd, 1); + if (!$this->isExtraCmd($refName)) { + throw new InvalidArgumentException("not found refer command '$refName' in the tool '$name'"); } - // use refer command - $cmd = $this->commands[$refCmd]; + // use refer command info + $cmd = $this->getCmdScripts($refName); } return $cmd; @@ -137,6 +153,26 @@ public function getCommand(string $command): string|array return $this->commands[$command]; } + /** + * @param string $cmd + * + * @return bool + */ + public function isBuiltIn(string $cmd): bool + { + return in_array($cmd, self::BUILT_IN, true); + } + + /** + * @param string $command + * + * @return bool + */ + public function isExtraCmd(string $command): bool + { + return isset($this->commands[$command]); + } + /** * @param string $command * @@ -144,6 +180,10 @@ public function getCommand(string $command): string|array */ public function hasCommand(string $command): bool { + if ($this->isBuiltIn($command)) { + return true; + } + return isset($this->commands[$command]); } @@ -163,16 +203,6 @@ private function mustCommand(string $command): void } } - /** - * @param string $cmd - * - * @return bool - */ - public function isBuiltIn(string $cmd): bool - { - return in_array($cmd, self::BUILT_IN, true); - } - /** * @param array $commands * @@ -225,7 +255,7 @@ public function getInstall(): array */ public function setInstall(array|string $install): void { - if (is_string($install)) { + if (is_string($install) || !isset($install['run'])) { $install = [ 'run' => $install, ]; @@ -247,7 +277,7 @@ public function getUpdate(): array */ public function setUpdate(array|string $update): void { - if (is_string($update)) { + if (is_string($update) || !isset($update['run'])) { $update = [ 'run' => $update, ]; @@ -269,7 +299,7 @@ public function getRemove(): array */ public function setRemove(array|string $remove): void { - if (is_string($remove)) { + if (is_string($remove) || !isset($remove['run'])) { $remove = [ 'run' => $remove, ]; diff --git a/app/Console/SubCmd/ToolCmd/InstallCommand.php b/app/Console/SubCmd/ToolCmd/InstallCommand.php index 3e62bc0..11acfa6 100644 --- a/app/Console/SubCmd/ToolCmd/InstallCommand.php +++ b/app/Console/SubCmd/ToolCmd/InstallCommand.php @@ -5,6 +5,7 @@ use Inhere\Console\Command; use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; +use Inhere\Kite\Console\Command\ToolCommand; use Inhere\Kite\Console\Manager\ToolManager; use Inhere\Kite\Kite; use Toolkit\Stdlib\Helper\Assert; @@ -62,7 +63,11 @@ protected function execute(Input $input, Output $output) $cr = $tool->buildCmdRunner($command); if ($p = $this->getParent()) { - $cr->setDryRun($p->getFlags()->getOpt('dry-run')); + $cr->setDryRun($p->getFlags()->getOpt(ToolCommand::OPT_DRY_RUN)); + + if ($p->getFlags()->getOpt(ToolCommand::OPT_PROXY_ENV)) { + Kite::autoProxy()->directApply($this->getCommandId()); + } } // $cr->setEnvVars();