From 0c7a044e224d7ba74e69679ad80fad4ac2f34542 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 22 May 2022 12:11:46 +0800 Subject: [PATCH] up: update the tool manage and config logic --- app/Component/ScriptRunner.php | 15 ++- app/Console/Command/RunCommand.php | 14 ++- app/Console/Model/BinTool.php | 116 ++++++++++++++++++ app/Console/Model/ToolCmdMeta.php | 35 ++++++ app/Console/SubCmd/ToolCmd/InstallCommand.php | 4 +- .../SubCmd/ToolCmd/ListToolCommand.php | 2 +- app/Console/SubCmd/ToolCmd/UpdateCommand.php | 2 +- config/config.php | 5 + config/tools.php | 21 ++++ 9 files changed, 203 insertions(+), 11 deletions(-) create mode 100644 app/Console/Model/ToolCmdMeta.php create mode 100644 config/tools.php diff --git a/app/Component/ScriptRunner.php b/app/Component/ScriptRunner.php index 201d924..57b2667 100644 --- a/app/Component/ScriptRunner.php +++ b/app/Component/ScriptRunner.php @@ -62,6 +62,11 @@ class ScriptRunner extends AbstractObj */ private bool $enable = true; + /** + * @var int exit code of exec script + */ + private int $errCode = 0; + /** * @var array */ @@ -353,7 +358,7 @@ private function executeScript(string $command, bool $onlyOne = false, string $w if ($this->dryRun) { Cli::colored('DRY-RUN: ' . $command, 'cyan'); } else { - SysCmd::quickExec($command, $workdir); + $this->errCode = SysCmd::quickExec($command, $workdir); } if ($onlyOne) { @@ -607,4 +612,12 @@ public function setEnable(bool|int $enable): void $this->enable = (bool)$enable; } + /** + * @return int + */ + public function getErrCode(): int + { + return $this->errCode; + } + } diff --git a/app/Console/Command/RunCommand.php b/app/Console/Command/RunCommand.php index ea9c868..5d15e01 100644 --- a/app/Console/Command/RunCommand.php +++ b/app/Console/Command/RunCommand.php @@ -100,6 +100,7 @@ protected function execute(Input $input, Output $output): int // default list script commands if ($listType) { + $name = $this->flags->getOpt('info', $name); $this->listScripts($output, $name); return 0; } @@ -175,10 +176,13 @@ private function listScriptFiles(Output $output, string $name): void private function listScripts(Output $output, string $name): void { $listOpt = [ - 'ucFirst' => false, + 'ucFirst' => false, + 'ucTitleWords' => false, + 'filterEmpty' => true, ]; if ($name && $this->sr->isScriptName($name)) { + $desc = ''; $item = $this->sr->getScript($name); // [_meta => [desc, ]] @@ -187,15 +191,14 @@ private function listScripts(Output $output, string $name): void unset($item['_meta']); $desc = $meta['desc'] ?? ''; - if ($desc) { - $output->colored($desc . "\n", 'cyan'); - } + // $output->colored(ucfirst($desc), 'cyan'); } $output->aList([ 'name' => $name, + 'desc' => $desc, 'command' => $item, - ], 'script information', $listOpt); + ], "Script: $name", $listOpt); } else { $count = $this->sr->getScriptCount(); $output->aList($this->sr->getScripts(), "registered scripts(total: $count)", $listOpt); @@ -208,7 +211,6 @@ private function listScripts(Output $output, string $name): void */ private function searchScripts(Output $output, string $kw): void { - // search $matched = $this->sr->searchScripts($kw); $count = count($matched); diff --git a/app/Console/Model/BinTool.php b/app/Console/Model/BinTool.php index a57991e..6761c2c 100644 --- a/app/Console/Model/BinTool.php +++ b/app/Console/Model/BinTool.php @@ -7,6 +7,7 @@ use Toolkit\Cli\Cli; use Toolkit\Stdlib\Obj; use Toolkit\Stdlib\Obj\AbstractObj; +use function in_array; use function is_string; use function substr; @@ -18,6 +19,8 @@ */ class BinTool extends AbstractObj { + public const BUILT_IN = ['install', 'update', 'remove']; + public string $name = ''; public string $desc = ''; @@ -26,6 +29,29 @@ class BinTool extends AbstractObj public array $envVars = []; + /** + * @var string[] + */ + protected array $deps = []; + + /** + * @var array{run: array, deps: array} + * @see ToolCmdMeta + */ + protected array $install = []; + + /** + * @var array{run: array, deps: array} + * @see ToolCmdMeta + */ + protected array $update = []; + + /** + * @var array{run: array, deps: array} + * @see ToolCmdMeta + */ + protected array $remove = []; + public array $commands = []; public array $beforeTips = []; @@ -107,6 +133,7 @@ public function getCmdScripts(string $command): string|array public function getCommand(string $command): string|array { $this->mustCommand($command); + return $this->commands[$command]; } @@ -127,11 +154,25 @@ public function hasCommand(string $command): bool */ private function mustCommand(string $command): void { + if ($this->isBuiltIn($command)) { + return; + } + if (!isset($this->commands[$command])) { throw new InvalidArgumentException("command '$command' is not found in tool: " . $this->name); } } + /** + * @param string $cmd + * + * @return bool + */ + public function isBuiltIn(string $cmd): bool + { + return in_array($cmd, self::BUILT_IN, true); + } + /** * @param array $commands * @@ -162,4 +203,79 @@ public function getAfterTip(string $command): array|string { return $this->afterTips[$command] ?? ''; } + + /** + * @param array|string $deps + */ + public function setDeps(array|string $deps): void + { + $this->deps = (array)$deps; + } + + /** + * @return array + */ + public function getInstall(): array + { + return $this->install; + } + + /** + * @param array|string $install + */ + public function setInstall(array|string $install): void + { + if (is_string($install)) { + $install = [ + 'run' => $install, + ]; + } + + $this->install = $install; + } + + /** + * @return array + */ + public function getUpdate(): array + { + return $this->update; + } + + /** + * @param array|string $update + */ + public function setUpdate(array|string $update): void + { + if (is_string($update)) { + $update = [ + 'run' => $update, + ]; + } + + $this->update = $update; + } + + /** + * @return array + */ + public function getRemove(): array + { + return $this->remove; + } + + /** + * @param array|string $remove + */ + public function setRemove(array|string $remove): void + { + if (is_string($remove)) { + $remove = [ + 'run' => $remove, + ]; + } + + $this->remove = $remove; + } + } diff --git a/app/Console/Model/ToolCmdMeta.php b/app/Console/Model/ToolCmdMeta.php new file mode 100644 index 0000000..f1c1a6e --- /dev/null +++ b/app/Console/Model/ToolCmdMeta.php @@ -0,0 +1,35 @@ +deps = (array)$deps; + } + + /** + * @param array|string $run + */ + public function setRun(array|string $run): void + { + $this->run = (array)$run; + } +} diff --git a/app/Console/SubCmd/ToolCmd/InstallCommand.php b/app/Console/SubCmd/ToolCmd/InstallCommand.php index 76c1c20..3e62bc0 100644 --- a/app/Console/SubCmd/ToolCmd/InstallCommand.php +++ b/app/Console/SubCmd/ToolCmd/InstallCommand.php @@ -21,7 +21,7 @@ class InstallCommand extends Command public static function aliases(): array { - return ['ins', 'i']; + return ['ins', 'in', 'i']; } protected function configure(): void @@ -57,7 +57,7 @@ protected function execute(Input $input, Output $output) } $command = 'install'; - $output->info("Will run '$command' for the tool: $name"); + $output->info("Will $command the tool: $name"); $cr = $tool->buildCmdRunner($command); diff --git a/app/Console/SubCmd/ToolCmd/ListToolCommand.php b/app/Console/SubCmd/ToolCmd/ListToolCommand.php index 9301cad..502e21d 100644 --- a/app/Console/SubCmd/ToolCmd/ListToolCommand.php +++ b/app/Console/SubCmd/ToolCmd/ListToolCommand.php @@ -45,6 +45,6 @@ protected function execute(Input $input, Output $output) return; } - $output->aList($tm->getToolsInfo(), 'Tools'); + $output->aList($tm->getToolsInfo(), 'Tools management'); } } diff --git a/app/Console/SubCmd/ToolCmd/UpdateCommand.php b/app/Console/SubCmd/ToolCmd/UpdateCommand.php index e4c609c..e939ae2 100644 --- a/app/Console/SubCmd/ToolCmd/UpdateCommand.php +++ b/app/Console/SubCmd/ToolCmd/UpdateCommand.php @@ -46,7 +46,7 @@ protected function execute(Input $input, Output $output) $tool = $tm->getToolModel($name); $command = 'update'; - $output->info("Will run '$command' for the tool: $name"); + $output->info("Will $command the tool: $name"); $cr = $tool->buildCmdRunner($command); diff --git a/config/config.php b/config/config.php index 8a76a70..f182e73 100644 --- a/config/config.php +++ b/config/config.php @@ -100,4 +100,9 @@ 'scripts' => require 'scripts.php', // command aliases. element is: alias command => real command 'aliases' => require 'aliases.php', + // custom tools management + 'toolManager' => [ + 'workdir' => '', + ], + 'tools' => require 'tools.php', ]; diff --git a/config/tools.php b/config/tools.php new file mode 100644 index 0000000..4b83fe6 --- /dev/null +++ b/config/tools.php @@ -0,0 +1,21 @@ + [ + 'desc' => 'Run your GitHub Actions locally 🚀', + 'deps' => 'brew', // all(install, update) required brew + // 'workdir' => '/var/tmp', + 'homepage' => 'https://github.com/nektos/act', + 'afterTips' => [ + 'install' => 'tool has been installed to the OS' + ], + 'install' => [ + // run, command, script + 'run' => 'brew install act', + // 'deps' => 'brew', // install required brew + ], + 'update' => [ + 'run' => 'brew upgrade act', + ], + ], +];