Skip to content

Commit

Permalink
chore: update some util class, add more util methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 4, 2021
1 parent 20f7458 commit e8a3803
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 49 deletions.
4 changes: 2 additions & 2 deletions app/Common/CmdRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
14 changes: 11 additions & 3 deletions app/Common/IdeaHttp/ClientEnvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -135,4 +135,12 @@ public function getEnvs(): array
{
return $this->envs;
}

/**
* @return string
*/
public function getCurEnv(): string
{
return $this->curEnv;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,34 @@
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;
use function preg_match;
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
{
return [
'ls' => 'list',
'rn' => 'rename',
'mkdir' => ['create-dir'],
'mkSubDirs' => ['mk-subDirs', 'mk-subs'],
];
}

Expand Down Expand Up @@ -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
*
Expand Down
61 changes: 26 additions & 35 deletions app/Helper/AppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
}
}
21 changes: 18 additions & 3 deletions app/Helper/GitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
33 changes: 33 additions & 0 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
20 changes: 18 additions & 2 deletions app/Lib/Stream/ListStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,6 +81,22 @@ public function eachToMap(callable $func, MapStream $new): MapStream
return $new;
}

/**
* @param callable(array): array $func
*
* @return array<string, mixed>
*/
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
Expand Down

0 comments on commit e8a3803

Please sign in to comment.