From d89ac7967d854de889d054e71eca15fe11c63faa Mon Sep 17 00:00:00 2001 From: Inhere Date: Wed, 28 Dec 2022 16:57:00 +0800 Subject: [PATCH] :necktie: up: update str, self some commands logic --- app/Console/Controller/SelfController.php | 14 ++++++++++---- app/Console/Controller/StringController.php | 20 +++++++++++++++++--- app/Lib/Convert/JavaProperties.php | 8 ++++---- app/Lib/Generate/AbstractGenCode.php | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/Console/Controller/SelfController.php b/app/Console/Controller/SelfController.php index 723515f..c8b438b 100644 --- a/app/Console/Controller/SelfController.php +++ b/app/Console/Controller/SelfController.php @@ -9,6 +9,7 @@ use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Console\Util\PhpDevServe; +use Inhere\Kite\Console\SubCmd\InitCommand; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\SysCmd; use Inhere\Kite\Kite; @@ -66,6 +67,13 @@ protected static function commandAliases(): array ]; } + protected function subCommands(): array + { + return [ + InitCommand::class, + ]; + } + protected function beforeRun(): void { parent::beforeRun(); @@ -253,7 +261,7 @@ public function objectCommand(FlagsParser $fs, Output $output): void * update {binName} to latest from github repository(by git pull) * * @options - * --no-deps bool;Not update deps by composer update + * --deps bool;whether update vendor deps by `composer update` * * @param FlagsParser $fs * @param Output $output @@ -272,11 +280,9 @@ public function updateCommand(FlagsParser $fs, Output $output): void // [, $msg,] = Sys::run($cmd); SysCmd::quickExec($cmd, $dir); - if (!$fs->getOpt('no-deps')) { + if ($fs->getOpt('deps')) { Color::println('Run composer update:'); $cmd = 'composer update'; - // [, $msg,] = Sys::run($cmd); - // $output->writeln($msg); SysCmd::quickExec($cmd, $dir); } diff --git a/app/Console/Controller/StringController.php b/app/Console/Controller/StringController.php index 68a86c7..ac34d4a 100644 --- a/app/Console/Controller/StringController.php +++ b/app/Console/Controller/StringController.php @@ -29,6 +29,7 @@ use Throwable; use Toolkit\FsUtil\File; use Toolkit\PFlag\FlagsParser; +use Toolkit\Stdlib\Arr; use Toolkit\Stdlib\Json; use Toolkit\Stdlib\Str; use function array_pad; @@ -433,7 +434,7 @@ public function replaceCommand(FlagsParser $fs, Output $output): void * --fields The field names, split by ',' * --get-cols Only get the provide index cols, start is 0. eg: 1,5 * -o, --output The output target. default is stdout. - * --of, --out-fmt The output format. allow: raw, md-table + * --of, --out-fmt The output format. allow: raw, md/md-table, table, json * --is, --item-sep The item sep char. default is NL. * --vn, --value-num int;The item value number. default get from first line. * --vs, --value-sep The item value sep char for 'space' parser. default is SPACE @@ -457,6 +458,12 @@ public function parseCommand(FlagsParser $fs, Output $output): void $p->setItemParser(TextParser::charSplitParser($valueSep)); } + $indexes = []; + $idxString = $fs->getOpt('get-cols'); + if ($idxString && !$indexes = Str::toInts($idxString)) { + throw new InvalidArgumentException('please provide valid column index string.'); + } + switch ($fs->getOpt('item-parser')) { case 'json': case 'json5': @@ -466,7 +473,8 @@ public function parseCommand(FlagsParser $fs, Output $output): void default: $valueSep = $fs->getOpt('value-sep', ' '); $itemParser = TextParser::charSplitParser($valueSep); - break; + // $itemParser = TextItemParser::new($valueSep, $indexes); + break; } $p->setItemParser($itemParser); @@ -475,6 +483,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void $result = ''; $doOutput = true; switch ($fs->getOpt('out-fmt')) { + case 'md': case 'mdtable': case 'mdTable': case 'md-table': @@ -488,7 +497,12 @@ public function parseCommand(FlagsParser $fs, Output $output): void $result = $head . "\n" . $line . "\n" . implode("\n", $rows); break; case 'raw': - $result = $text; + case 'text': + $rows = $p->stream()->eachToArray(function (array $item) use ($indexes) { + return implode(' ', Arr::gets($item, $indexes)); + }); + + $result = implode("\n", $rows); break; case 'table': Table::show($p->getData()); diff --git a/app/Lib/Convert/JavaProperties.php b/app/Lib/Convert/JavaProperties.php index a64ace7..cc4ff17 100644 --- a/app/Lib/Convert/JavaProperties.php +++ b/app/Lib/Convert/JavaProperties.php @@ -84,11 +84,11 @@ private function setNodeValue(array &$array, string $path, mixed $value): void } /** - * @param Traversable|array $data + * @param iterable $data * * @return string */ - public function encode(Traversable|array $data): string + public function encode(iterable $data): string { $flatMap = []; $this->flattenData($data, $flatMap); @@ -97,11 +97,11 @@ public function encode(Traversable|array $data): string } /** - * @param Traversable|array $data + * @param iterable $data * @param string $parentPath * @param array $flatMap */ - private function flattenData(Traversable|array $data, array &$flatMap = [], string $parentPath = ''): void + private function flattenData(iterable $data, array &$flatMap = [], string $parentPath = ''): void { foreach ($data as $key => $val) { if ($parentPath) { diff --git a/app/Lib/Generate/AbstractGenCode.php b/app/Lib/Generate/AbstractGenCode.php index 3c7cf98..d1646b8 100644 --- a/app/Lib/Generate/AbstractGenCode.php +++ b/app/Lib/Generate/AbstractGenCode.php @@ -75,6 +75,7 @@ public function prepareContext(): void 'package' => 'YOUR.PKG.NAME', 'modulePkg' => 'MODULE_PKG', 'pkgName' => 'PKG_NAME', + 'subPkg' => 'SUB_PKG', ]); }