diff --git a/app/Common/CmdRunner.php b/app/Common/CmdRunner.php index f7bbb63..9a7e399 100644 --- a/app/Common/CmdRunner.php +++ b/app/Common/CmdRunner.php @@ -260,7 +260,7 @@ public function run(bool $printOutput = false): AbstractCmdBuilder // stop on error $code = $this->code; if (0 !== $code && false === $this->ignoreError) { - Color::println("\nCommand exit code not equal to 0(code: $code), stop run.", 'red'); + Color::println("\nCommand exit code is equals $code, stop run.", 'red'); return $this; } } @@ -311,7 +311,7 @@ private function runCommands(array $commands): void // stop on error $code = $this->code; if (0 !== $code && false === $this->ignoreError) { - Color::println("\nCommand exit code not equal to 0(code: $code), stop run.", 'red'); + Color::println("\nCommand exit code is equals $code, stop run.", 'red'); break; } } diff --git a/app/Common/IdeaHttp/ClientEnvReader.php b/app/Common/IdeaHttp/ClientEnvReader.php index 404d883..3e5bdb1 100644 --- a/app/Common/IdeaHttp/ClientEnvReader.php +++ b/app/Common/IdeaHttp/ClientEnvReader.php @@ -16,17 +16,17 @@ class ClientEnvReader /** * @var string */ - private $envFile; + private string $envFile; /** * @var array */ - private $envs = []; + private array $envs = []; /** * @var string */ - private $curEnv = ''; + private string $curEnv = ''; /** * @param string $envFile @@ -135,4 +135,12 @@ public function getEnvs(): array { return $this->envs; } + + /** + * @return string + */ + public function getCurEnv(): string + { + return $this->curEnv; + } } diff --git a/app/Console/Controller/FileController.php b/app/Console/Controller/FsController.php similarity index 78% rename from app/Console/Controller/FileController.php rename to app/Console/Controller/FsController.php index 2646580..903e84b 100644 --- a/app/Console/Controller/FileController.php +++ b/app/Console/Controller/FsController.php @@ -14,6 +14,7 @@ use Inhere\Console\IO\Output; use Inhere\Console\Util\Show; use Toolkit\Cli\Util\Download; +use Toolkit\FsUtil\Dir; use Toolkit\PFlag\FlagsParser; use function basename; use function glob; @@ -21,17 +22,17 @@ use const GLOB_MARK; /** - * Class FileController + * Class FsController */ -class FileController extends Controller +class FsController extends Controller { - protected static $name = 'file'; + protected static $name = 'fs'; protected static $description = 'Some useful development tool commands'; public static function aliases(): array { - return ['fs']; + return ['fs', 'file', 'dir']; } protected static function commandAliases(): array @@ -39,6 +40,8 @@ protected static function commandAliases(): array return [ 'ls' => 'list', 'rn' => 'rename', + 'mkdir' => ['create-dir'], + 'mkSubDirs' => ['mk-subDirs', 'mk-subs'], ]; } @@ -115,6 +118,46 @@ public function findCommand(Output $output): void // $output->success('hello'); } + /** + * create directories + * + * @arguments + * dirPaths array;The sub directory names/paths;required + * + * @param FlagsParser $fs + * @param Output $output + */ + public function mkdirCommand(FlagsParser $fs, Output $output): void + { + $dirPaths = $fs->getArg('dirPaths'); + + foreach ($dirPaths as $dirPath) { + Dir::create($dirPath); + } + + $output->colored('OK'); + } + + /** + * create sub directories in the parent dir. + * + * @arguments + * parentDir The parent directory path;required + * subDirs array;The sub directory names/paths. + * + * @param FlagsParser $fs + * @param Output $output + */ + public function mkSubDirsCommand(FlagsParser $fs, Output $output): void + { + $parentDir = $fs->getArg('parentDir'); + $subDirs = $fs->getArg('subDirs'); + + Dir::mkSubDirs($parentDir, $subDirs, 0776); + + $output->colored('OK'); + } + /** * use vim edit an input file * diff --git a/app/Helper/AppHelper.php b/app/Helper/AppHelper.php index d4509d5..3dde46e 100644 --- a/app/Helper/AppHelper.php +++ b/app/Helper/AppHelper.php @@ -3,6 +3,7 @@ namespace Inhere\Kite\Helper; use ArrayAccess; +use Closure; use Inhere\Console\Util\Show; use Inhere\Kite\Console\Component\Clipboard; use Inhere\Kite\Console\Component\ContentsAutoReader; @@ -11,10 +12,14 @@ use Toolkit\FsUtil\File; use Toolkit\Stdlib\OS; use Toolkit\Sys\Sys; +use function array_filter; use function array_shift; +use function array_slice; +use function count; use function defined; use function explode; use function getenv; +use function implode; use function is_array; use function is_file; use function is_object; @@ -47,16 +52,6 @@ public static function isVersion(string $version): bool return 1 === preg_match('#^v?\d{1,2}.\d{1,2}.\d{1,3}[.\w]*$#', $version); } - /** - * @param string $pkgName 'inhere/console' - * - * @return bool - */ - public static function isPhpPkgName(string $pkgName): bool - { - return true; - } - /** * @return bool */ @@ -68,21 +63,6 @@ public static function isInPhar(): bool return false; } - /** - * @param string $tag - * - * @return string - */ - public static function formatTag(string $tag): string - { - $tag = trim($tag, 'v '); - if (!$tag) { - return ''; - } - - return 'v' . $tag; - } - /** * env: LC_CTYPE=zh_CN.UTF-8 * @@ -135,16 +115,6 @@ public static function openBrowser(string $pageUrl): void Sys::execute($cmd); } - /** - * @param string $path - * - * @return string eg: ~/.config/kite.php - */ - public static function userConfigDir(string $path = ''): string - { - return OS::getUserHomeDir() . '/.config' . ($path ? "/$path" : ''); - } - /** * @param string $sname * @param int $length @@ -273,4 +243,25 @@ public static function tryReadContents(string $input, array $opts = []): string { return ContentsAutoReader::readFrom($input, $opts); } + + /** + * @return Closure + */ + public static function json5lineParser(): Closure + { + return static function (string $line, int $fieldNum) { + $nodes = array_filter(explode(' ', $line), 'strlen'); + $count = count($nodes); + if ($count <= $fieldNum) { + return $nodes; + } + + $values = array_slice($nodes, 0, $fieldNum - 1); + $others = array_slice($nodes, $fieldNum - 1); + + // merge others as last elem + $values[] = implode(' ', $others); + return $values; + }; + } } diff --git a/app/Helper/GitUtil.php b/app/Helper/GitUtil.php index 2957534..cf332e6 100644 --- a/app/Helper/GitUtil.php +++ b/app/Helper/GitUtil.php @@ -36,21 +36,36 @@ class GitUtil */ public static function isFullUrl(string $str): bool { - if (strpos($str, 'http:') === 0) { + if (str_starts_with($str, 'http:')) { return true; } - if (strpos($str, 'https:') === 0) { + if (str_starts_with($str, 'https:')) { return true; } - if (strpos($str, 'git@') === 0) { + if (str_starts_with($str, 'git@')) { return true; } return false; } + /** + * @param string $tag + * + * @return string + */ + public static function formatTag(string $tag): string + { + $tag = trim($tag, 'v '); + if (!$tag) { + return ''; + } + + return 'v' . $tag; + } + /** * @param string $workDir * diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 0a2a2af..6a23fb4 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -2,16 +2,49 @@ namespace Inhere\Kite\Helper; +use Leuffen\TextTemplate\TextTemplate; use Toolkit\FsUtil\File; use Toolkit\FsUtil\FS; +use Toolkit\Stdlib\OS; use function dirname; use function is_file; +use function is_string; /** * class KiteUtil */ class KiteUtil { + + /** + * @param string $path + * + * @return string eg: ~/.config/kite.php + */ + public static function userConfigDir(string $path = ''): string + { + return OS::getUserHomeDir() . '/.config' . ($path ? "/$path" : ''); + } + + /** + * @param string $text + * + * @return TextTemplate + */ + public static function newTplEngine(string $text): TextTemplate + { + $tplEng = new TextTemplate($text); + $tplEng->addFilter('default', function ($value, $default) { + if (is_string($value) && $value === '') { + return $default; + } + + return empty($value) ? $default : $value; + }); + + return $tplEng; + } + /** * @param string $dir * diff --git a/app/Lib/Stream/ListStream.php b/app/Lib/Stream/ListStream.php index 08bff80..ac2ac3e 100644 --- a/app/Lib/Stream/ListStream.php +++ b/app/Lib/Stream/ListStream.php @@ -66,12 +66,12 @@ public function eachTo(callable $func, BaseStream $new): BaseStream } /** - * @param callable(array): string $func + * @param callable(array): array $func * @param MapStream $new * * @return MapStream */ - public function eachToMap(callable $func, MapStream $new): MapStream + public function eachToMapStream(callable $func, MapStream $new): MapStream { foreach ($this as $item) { [$key, $val] = $func($item); @@ -81,6 +81,22 @@ public function eachToMap(callable $func, MapStream $new): MapStream return $new; } + /** + * @param callable(array): array $func + * + * @return array + */ + public function eachToMapArray(callable $func): array + { + $map = []; + foreach ($this as $item) { + [$key, $val] = $func($item); + $map[$key] = $val; + } + + return $map; + } + /** * @param callable(array): bool $func * @param bool|mixed $apply