From 00837cb7bc3cba1a9dae127d5a1625f12f7f9466 Mon Sep 17 00:00:00 2001 From: inhere Date: Wed, 20 Mar 2024 13:18:24 +0800 Subject: [PATCH] :necktie: up: add new dep toolkit/extlib and move cmd runner to extlib --- .github/workflows/php.yml | 2 +- app/Common/Cmd.php | 31 +-- app/Common/CmdRunner.php | 343 +-------------------------------- composer.json | 1 + script/update-kite-deps.sh | 10 +- script/upinit-kite-dev-deps.sh | 2 +- 6 files changed, 15 insertions(+), 374 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index eca2c12..80678cf 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.1, 8.2] # 7.2,7.4,8.0, + php: [8.1, 8.2, 8.3] # 7.2,7.4,8.0, os: [ubuntu-latest, macOS-latest] # windows-latest, # include: # will not testing on php 7.2 # - os: 'ubuntu-latest' diff --git a/app/Common/Cmd.php b/app/Common/Cmd.php index 963c574..f7177c0 100644 --- a/app/Common/Cmd.php +++ b/app/Common/Cmd.php @@ -2,38 +2,11 @@ namespace Inhere\Kite\Common; -use Toolkit\Cli\Color; -use Toolkit\Sys\Cmd\CmdBuilder; - /** * class Cmd builder + * @deprecated use \Toolkit\Extlib\Exec\Cmd */ -class Cmd extends CmdBuilder +class Cmd extends \Toolkit\Extlib\Exec\Cmd { - /** - * @param string $msg - * @param string $scene - */ - protected function printMessage(string $msg, string $scene): void - { - self::printByScene($msg, $scene); - } - - /** - * @param string $msg - * @param string $scene - */ - public static function printByScene(string $msg, string $scene): void - { - $color = 'info'; - if ($scene === self::PRINT_CMD) { - $color = 'yellow'; - } elseif ($scene === self::PRINT_DRY_RUN) { - $color = 'cyan'; - } elseif ($scene === self::PRINT_ERROR) { - $color = 'red'; - } - Color::println($msg, $color); - } } diff --git a/app/Common/CmdRunner.php b/app/Common/CmdRunner.php index e7e2f3c..8dc8714 100644 --- a/app/Common/CmdRunner.php +++ b/app/Common/CmdRunner.php @@ -2,351 +2,12 @@ namespace Inhere\Kite\Common; -use RuntimeException; -use Toolkit\Cli\Cli; -use Toolkit\Cli\Color; -use Toolkit\Sys\Cmd\AbstractCmdBuilder; -use function is_array; -use function is_string; -use function sprintf; - /** * Class CmdRunner - batch run multi commands * - * @package Inhere\Kite\Common + * @deprecated use \Toolkit\Extlib\Exec\CmdRunner */ -class CmdRunner extends AbstractCmdBuilder +class CmdRunner extends \Toolkit\Extlib\Exec\CmdRunner { - /** - * [ - * 'name' => 'command line', - * 'test' => [ - * 'echo hi', - * 'do something', - * ] - * ] - * - * @var array[]|array - */ - private array $commands = []; - - /** - * @param array|string $cmd - * @param string $workDir - * - * @return static - */ - public static function new(array|string $cmd = '', string $workDir = ''): self - { - return new self($cmd, $workDir); - } - - /** - * Class constructor. - * - * @param string|array $command One or multi commands - * @param string $workDir - */ - public function __construct($command = null, string $workDir = '') - { - parent::__construct('', $workDir); - - if (is_string($command)) { - $this->cmdline = $command; - } elseif (is_array($command)) { - $this->commands = (array)$command; - } - } - - /** - * @param string $msg - * @param string $scene - */ - protected function printMessage(string $msg, string $scene): void - { - Cmd::printByScene($msg, $scene); - } - - /************************************************************************** - * add ant run - *************************************************************************/ - - /** - * @param bool $printOutput - * - * @return array - */ - public function exec(bool $printOutput = false): array - { - $this->run($printOutput); - - return $this->getResult(); - } - - /** - * @param bool $printOutput - * - * @return $this - */ - public function do(bool $printOutput = false): self - { - return $this->run($printOutput); - } - - /** - * @param string $cmd - * - * @return $this - */ - public function afterDo(string $cmd): self - { - return $this->setCmdline($cmd)->do($this->printOutput); - } - - /** - * @param string $cmd - * @param callable $whereFunc - * - * @return $this - */ - public function whereDo(string $cmd, callable $whereFunc): self - { - // only run on return TRUE - if (true === $whereFunc()) { - $this->setCmdline($cmd)->do($this->printOutput); - } - - return $this; - } - - /** - * @param string $cmd - * @param string|null $workDir - * - * @return $this - */ - public function afterOkDo(string $cmd, string $workDir = null): self - { - if (0 !== $this->code) { - return $this; - } - - if ($workDir !== null) { - $this->workDir = $workDir; - } - - return $this->setCmdline($cmd)->do($this->printOutput); - } - - /************************************************************************** - * batch add then run - *************************************************************************/ - - /** - * @param array $commands - * - * @return $this - */ - public function batch(array $commands): self - { - $this->commands = $commands; - - return $this; - } - - /** - * @param string $subCmd - * @param ...$args - * - * @return $this - */ - public function git(string $subCmd, ...$args): self - { - return $this->add(Cli::toCmdline($args, "git $subCmd")); - } - - /** - * @param string $cmdTpl - * @param mixed ...$args - * - * @return $this - */ - public function addf(string $cmdTpl, ...$args): self - { - if ($args) { - return $this->add(sprintf($cmdTpl, ...$args)); - } - - return $this->add($cmdTpl); - } - - /** - * @param callable $checker - * @param string $cmdTpl - * @param mixed ...$args - * - * @return $this - */ - public function addWheref(callable $checker, string $cmdTpl, ...$args): self - { - return $this->addWhere($checker, sprintf($cmdTpl, ...$args)); - } - - /** - * @param callable $checker - * @param string $command - * @param string $key - * - * @return $this - */ - public function addWhere(callable $checker, string $command, string $key = ''): self - { - $item = [ - 'where' => $checker, - 'command' => $command, - ]; - - if ($key) { - $this->commands[$key] = $item; - } else { - $this->commands[] = $item; - } - - return $this; - } - - /** - * @param string $command - * @param string $key - * - * @return $this - */ - public function add(string $command, string $key = ''): self - { - if ($key) { - $this->commands[$key] = $command; - } else { - $this->commands[] = $command; - } - - return $this; - } - - /** - * @param array $config = [ - * 'command' => 'STRING|ARRAY', - * 'workDir' => 'STRING', - * 'where' => 'callable', - * ] - * @param string $key - * - * @return $this - */ - public function addByArray(array $config, string $key = ''): self - { - if (!isset($config['command'])) { - throw new RuntimeException('must be setting "command" in the config'); - } - - if ($key) { - $this->commands[$key] = $config; - } else { - $this->commands[] = $config; - } - - return $this; - } - - /** - * Run all added commands - * - * @param bool $printOutput - * - * @return $this - */ - public function run(bool $printOutput = false): static - { - $this->printOutput = $printOutput; - - if ($command = $this->cmdline) { - $this->innerExecute($command, $this->workDir); - - // stop on error - $code = $this->code; - if (0 !== $code && false === $this->ignoreError) { - Color::println("\nCommand exit code is equals $code, stop run.", 'red'); - return $this; - } - } - - if ($commands = $this->commands) { - $this->runCommands($commands); - } - - return $this; - } - - /** - * @param array $commands - */ - private function runCommands(array $commands): void - { - Color::println('=========== Starting Handle ===========', 'suc'); - - $step = 1; - foreach ($commands as $command) { - $workDir = $this->workDir; - - // see addWhere() - if (is_array($command)) { - $item = $command; - - $func = $item['where'] ?? ''; - if ($func && false === $func()) { - Color::println("Skip $step ...", 'cyan'); - Color::println("- Does not meet the conditions", 'cyan'); - continue; - } - - $workDir = $item['workDir'] ?? $workDir; - $command = $item['command']; - } - - Color::println("STEP $step:", 'mga0'); - - // custom work dir - if ($workDir) { - Color::println('- work dir is ' . $workDir, 'italic'); - } - - $this->innerExecute($command, $workDir); - $step++; - - // stop on error - $code = $this->code; - if (0 !== $code && false === $this->ignoreError) { - Color::println("\nCommand exit code is equals $code, stop run.", 'red'); - break; - } - } - } - - /** - * @return array - */ - public function getCommands(): array - { - return $this->commands; - } - /** - * @param array $commands - * - * @return CmdRunner - */ - public function setCommands(array $commands): CmdRunner - { - $this->commands = $commands; - return $this; - } } diff --git a/composer.json b/composer.json index 2b61dda..bdb821f 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "phppkg/hucron": "dev-master", "phppkg/phpgit": "dev-master", "phppkg/cli-markdown": "^2.0", + "toolkit/extlib": "dev-main", "toolkit/fsutil": "^2.0", "laktak/hjson": "^2.2", "knplabs/github-api": "^3.5", diff --git a/script/update-kite-deps.sh b/script/update-kite-deps.sh index 57dd16b..105a342 100644 --- a/script/update-kite-deps.sh +++ b/script/update-kite-deps.sh @@ -6,7 +6,7 @@ # - clone no .git existed dev package # -# run: kite run --proxy update-kite-deps.sh +# run: kitep run --proxy update-kite-deps.sh # run: bash script/update-kite-deps.sh # run: proxy_on; bash script/update-kite-deps.sh @@ -20,12 +20,18 @@ if [ "$osName" == "Darwin" ]; then tmpKiteDir=~/Workspace/my-github/inhere/kite # windows: MINGW64_NT-10.0-19043 elif [ "${osName:0:5}" == "MINGW" ]; then - tmpKiteDir=/f/work/php/inhere/kite-tmp + tmpKiteDir=/c/Users/inhere/workspace/phpdev/tmp/kite-latest else tmpKiteDir=/tmp/kite-tmp fi set -x +if ! test -d $tmpKiteDir; then + echo "- Source dir not exist, clone kite ..." + mkdir -p $tmpKiteDir + git clone https://github.com/inhere/kite $tmpKiteDir +fi + #kite env proxy cd $tmpKiteDir || exit 2 git checkout . diff --git a/script/upinit-kite-dev-deps.sh b/script/upinit-kite-dev-deps.sh index da723ed..e88cb0d 100644 --- a/script/upinit-kite-dev-deps.sh +++ b/script/upinit-kite-dev-deps.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # -# each dev packages and run composer update +# Into each dev package dir and run git update # #