Skip to content

Commit

Permalink
add more script case, update some tool command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 20, 2021
1 parent 8fc9f35 commit f855c43
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 49 deletions.
11 changes: 5 additions & 6 deletions app/Component/ScriptRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use function strpos;
use function substr;
use function trim;
use function vdump;

/**
* class ScriptRunner
Expand Down Expand Up @@ -72,7 +71,7 @@ class ScriptRunner extends AbstractObj
/**
* @var array
*/
private array $scriptExts = ['.sh', '.zsh', '.bash', '.php', '.go', '.gop'];
private array $scriptExts = ['.sh', '.zsh', '.bash', '.php', '.go', '.gop', '.kts', '.gry', '.groovy'];

/**
* @var array
Expand Down Expand Up @@ -313,15 +312,15 @@ private function executeScript(string $command, bool $onlyOne = false): void
*/
public function findScriptFile(string $name): string
{
if (is_file($name)) {
return $name;
}

$ext = File::getExtension($name);
if (!$ext || !in_array($ext, $this->scriptExts, true)) {
return '';
}

if (is_file($name)) {
return $name;
}

foreach ($this->scriptDirs as $scriptDir) {
$relativeFile = $scriptDir . '/' . $name;
if (is_file($relativeFile)) {
Expand Down
24 changes: 21 additions & 3 deletions app/Console/Command/FindCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use SplFileInfo;
use Toolkit\FsUtil\FileFinder;
use Toolkit\Stdlib\Helper\DataHelper;

/**
* Class FindCommand
Expand All @@ -28,6 +31,8 @@ public static function aliases(): array
}

/**
* @arguments
* paths array;Find in the paths;true
*
* @param Input $input
* @param Output $output
Expand All @@ -36,10 +41,23 @@ public static function aliases(): array
*/
protected function execute(Input $input, Output $output): int
{
// $f = new FileFinder();
// $f->getIterator();
$paths = $this->flags->getArg('paths');
$output->info('find in the paths:' . DataHelper::toString($paths));

$output->info('recommended install fzf for search file');
$ff = FileFinder::create()->in($paths)
->skipUnreadableDirs()
->notFollowLinks()
->name('plugins')
->notPaths(['System', 'node_modules', 'bin/'])
// ->notNames(['System', 'node_modules', 'bin/'])
->onlyDirs();

$output->aList($ff->getInfo());
$ff->each(function (SplFileInfo $info) {
echo $info->getPathname(), "\n";
});

$output->info('Completed!');
return 0;
}
}
92 changes: 73 additions & 19 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Inhere\Kite\Console\Controller;

use Inhere\Console\Component\Formatter\Table;
use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
Expand All @@ -17,12 +18,18 @@
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\KiteUtil;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Parser\Text\Json5ItemParser;
use Inhere\Kite\Lib\Parser\Text\TextParser;
use Inhere\Kite\Lib\Stream\ListStream;
use Inhere\Kite\Lib\Stream\StringsStream;
use InvalidArgumentException;
use Throwable;
use Toolkit\FsUtil\File;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Json;
use Toolkit\Stdlib\Str;
use function array_pad;
use function array_shift;
use function count;
use function explode;
use function implode;
Expand Down Expand Up @@ -67,6 +74,7 @@ protected static function commandAliases(): array
'split' => ['s'],
'process' => ['p', 'filter', 'f'],
'replace' => ['r'],
'parse' => ['fields'],
];
}

Expand Down Expand Up @@ -391,39 +399,85 @@ public function replaceCommand(FlagsParser $fs, Output $output): void
}

/**
* collect field and comments from multi line contents
* parse and collect fields from multi line contents
*
* @arguments
* text The source text contents.
* source The source text for parse. allow: FILEPATH, @clipboard
*
* @options
* --fields The field names, split by ','
* --get-cols Only get the provide index cols, eg: 1,5
* -o, --output The output target. default is stdout.
* --of, --out-fmt The output format. allow: raw, md-table
* --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
* --parser, --item-parser The item parser name. allow:
* space - parser substr by space
* json, json5 - parser json(5) line
*
*/
public function fieldsCommand(FlagsParser $fs, Output $output): void
public function parseCommand(FlagsParser $fs, Output $output): void
{
$text = $fs->getArg('text');
$text = $fs->getArg('source');
$text = ContentsAutoReader::readFrom($text, [
'loadedFile' => $this->dumpfile,
]);

if (!$text) {
throw new InvalidArgumentException('please input text for handle');
}
$p = TextParser::new($text);
$p->setItemSep($fs->getOpt('item-sep'));
$p->setFieldNum($fs->getOpt('value-num'));

$fields = [];
foreach (explode("\n", $text) as $line) {
if (!str_contains($line, '//')) {
continue;
}
if ($valueSep = $fs->getOpt('value-sep')) {
$p->setItemParser(TextParser::charSplitParser($valueSep));
}

[$jsonLine, $comments] = Str::explode($line, '//', 2);
if (!preg_match('/[a-zA-Z][\w_]+/', $jsonLine, $matches)) {
continue;
}
switch ($fs->getOpt('item-parser')) {
case 'json':
case 'json5':
$itemParser = new Json5ItemParser;
break;
case 'space':
default:
$valueSep = $fs->getOpt('value-sep', ' ');
$itemParser = TextParser::charSplitParser($valueSep);
break;
}

// vdump($matches);
$fields[$matches[0]] = $comments;
$p->setItemParser($itemParser);
$p->parse();

$result = '';
$doOutput = true;
switch ($fs->getOpt('out-fmt')) {
case 'mdtable':
case 'mdTable':
case 'md-table':
$rows = ListStream::new($p->getData())
->eachToArray(function (array $item) {
return implode(' | ', $item);
});
$head = array_shift($rows);
$line = implode('|', array_pad(['-----'], $p->fieldNum, '-----'));

$result = $head . "\n" . $line . "\n". implode("\n", $rows);
break;
case 'raw':
$result = $text;
break;
case 'table':
Table::show($p->getData());
$doOutput = false;
break;
default:
$result = Json::pretty($p->getData());
break;
}

$output->aList($fields);
if ($doOutput) {
$outFile = $fs->getOpt('output');
ContentsAutoWriter::writeTo($outFile, $result);
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions app/Console/Controller/UtilController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,27 @@ public function dateCommand(Output $output): void
/**
* find IDEA in the machine
*
* @arguments
* name ide name, such as: phpStorm
*
* @param FlagsParser $fs
* @param Output $output
*/
public function findJetBrainsCommand(FlagsParser $fs, Output $output): void
{
// doc: https://www.jetbrains.com/help/idea/directories-used-by-the-ide-to-store-settings-caches-plugins-and-logs.html
$dirs = [
// '~/Library/Preferences/PhpStorm2019.3/',
'~/Library/Application\ Support/JetBrains/',
'~/Library/Application\ Support/JetBrains/GoLand2020.1/eval',
// '~/Library/Application\ Support/JetBrains/GoLand2020.1/eval',
'~/Library/Application\ Support/JetBrains/Toolbox/apps/',
'logs' => '~/Library/Logs/JetBrains/',
'cache' => ' ~/Library/Caches/JetBrains/',
];

$ideName = $fs->getArg('name', 'all');
// rm -rf ~/Library/Application\ Support/${NAME}*/eval
vdump($dirs);
$output->aList($dirs, $ideName);
}

/**
Expand Down
15 changes: 8 additions & 7 deletions app/Lib/Generate/Json5Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,31 @@ protected function collectObjectFields(string $name, array $map): void

$elemType = $elemSfx = '';
if ($type === 'array' && !empty($value)) {
if ($key && isset($this->subObjects[$key])) {
$elemType = $key;
if (isset($this->subObjects[$key])) {
$elemSfx = '_' . count($this->subObjects);
}

// is object
if (!isset($value[0])) {
$type = Type::OBJECT;
$this->collectObjectFields($key, $value);
$this->collectObjectFields($key . $elemSfx, $value);
} elseif (!empty($value[0])) { // is array
// collect first item on it's object
if (is_array($value[0])) {
$elemSfx = 'Item' . $elemSfx;
$this->collectObjectFields($key, $value[0]);
$this->collectObjectFields($key . $elemSfx, $value[0]);
} else {
$elemType = gettype($value[0]);
}
}
}

$fields[$key] = JsonField::new([
'name' => $key,
'type' => $type,
'elemType' => $elemType . $elemSfx,
'comment' => $this->comments[$key] ?? $key,
'name' => $key,
'type' => $type,
'subType' => $elemType . $elemSfx,
'comment' => $this->comments[$key] ?? $key,
]);
}

Expand Down
18 changes: 14 additions & 4 deletions app/Lib/Parser/Item/FieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Toolkit\Stdlib\Obj\AbstractObj;
use Toolkit\Stdlib\Str;
use Toolkit\Stdlib\Type;
use function in_array;
use function preg_match;
use function sprintf;

Expand All @@ -27,7 +28,7 @@ class FieldItem extends AbstractObj implements JsonSerializable
*
* @var string
*/
public string $elemType = '';
public string $subType = '';

public string $comment = '';

Expand Down Expand Up @@ -74,7 +75,7 @@ public function toJavaType(string $type, string $name): string
}

if ($type === Type::ARRAY) {
$elemType = $this->elemType ?: $name;
$elemType = $this->subType ?: $name;
if ($elemType === 'List') {
$elemType .= '_KW';
}
Expand All @@ -89,6 +90,14 @@ public function toJavaType(string $type, string $name): string
return Str::upFirst($type);
}

/**
* @return bool
*/
public function isComplexType(): bool
{
return in_array($this->type, [Type::ARRAY, Type::OBJECT], true);
}

/**
* @return bool
*/
Expand All @@ -111,9 +120,10 @@ public function isMultiWords(): bool
public function toArray(): array
{
return [
'name' => $this->name,
'type' => $this->type,
'name' => $this->name,
'type' => $this->type,
'comment' => $this->comment,
'subType' => $this->subType,
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Lib/Parser/Text/Json5ItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __invoke(string $line): array
}

// match field
if (!preg_match('/[a-zA-Z][\w_]+/', $line, $matches)) {
if (!preg_match('/^\s*[\'"]?([a-zA-Z][\w_]+)/', $line, $matches)) {
return [];
}

Expand Down
29 changes: 29 additions & 0 deletions app/Lib/Parser/Text/JsonItemParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Lib\Parser\Text;

use function preg_match;

/**
* class JsonItemParser
*
* @author inhere
*/
class JsonItemParser
{
public const KEY_FIELD = 'field';

/**
* @param string $str
*
* @return array
*/
public function __invoke(string $str): array
{
if (!preg_match('/[a-zA-Z][\w_]+/', $str, $matches)) {
return [];
}

return [self::KEY_FIELD => $matches[1]];
}
}
Loading

0 comments on commit f855c43

Please sign in to comment.