Skip to content

Commit

Permalink
prof: update some console commands logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 29, 2021
1 parent 3c64b3f commit aa708b9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0] # 7.2,7.4,
php: [8.0, 8.1] # 7.2,7.4,
os: [ubuntu-latest, macOS-latest] # windows-latest,
# include: # will not testing on php 7.2
# - os: 'ubuntu-latest'
Expand Down
30 changes: 12 additions & 18 deletions app/Console/Command/Json5Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use InvalidArgumentException;
use function file_get_contents;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use JsonException;
use function file_put_contents;
use function is_file;
use function json_encode;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
Expand All @@ -38,32 +38,26 @@ public static function aliases(): array

/**
* @options
* -o, --output Output the decoded contents to the file
* -o, --output string;Output the decoded contents to the file;;stdout
*
* @arguments
* json5file string;The json5 file name for parse;required
* json5 string;The json5 source for parse, allow: FILEPATH,@clipboard;required
*
* @param Input $input
* @param Input $input
* @param Output $output
*
* @throws JsonException
*/
protected function execute(Input $input, Output $output)
{
$j5file = $this->flags->getArg('json5file');
if (!is_file($j5file)) {
throw new InvalidArgumentException("the json5 file '$j5file' is not exists");
}

$source = file_get_contents($j5file);
$source = ContentsAutoReader::readFrom($this->flags->getArg('json5'));
$decoded = Json5Decoder::decode($source);

$encFlag = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
$jsonStr = json_encode($decoded, JSON_THROW_ON_ERROR | $encFlag);

$outFile = $this->flags->getOpt('output');
$jsonStr = json_encode($decoded, $encFlag);
if ($outFile) {
file_put_contents($outFile, $jsonStr);
$output->success('write contents to output file');
}

echo $jsonStr, "\n";
ContentsAutoWriter::writeTo($outFile, $jsonStr);
}
}
2 changes: 1 addition & 1 deletion app/Console/Component/ContentsAutoWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ContentsAutoWriter
/**
* @var string
*/
private string $output = '';
private string $output;

/**
* @param string $output
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function newBranchCommand(FlagsParser $fs, Output $output): void
/** @see GitFlowController::newBranchCommand() */
$command = 'gitflow:newBranch';

$output->notice("input $cmdName, will redirect to $command");
$output->notice("gitlab: input '$cmdName', will redirect to '$command'");

// Console::app()->dispatch($command, $input->getArgs());
// Console::app()->dispatch($command, $this->flags->getRawArgs());
Expand Down
20 changes: 12 additions & 8 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Generate\JsonToCode;
Expand Down Expand Up @@ -145,6 +146,7 @@ public function loadCommand(FlagsParser $fs, Output $output): void
* @options
* --type The search type. allow: keys, path
* -s, --source The json data source, default read stdin, allow: @load, @clipboard, @stdin
* -o, --output The output, default is stdout, allow: @load, @clipboard, @stdout
*
* @throws Throwable
*/
Expand All @@ -156,12 +158,14 @@ public function getCommand(FlagsParser $fs, Output $output): void
$path = $fs->getArg('path');
$ret = Arr::getByPath($this->data, $path);

if (is_scalar($ret)) {
$output->println($ret);
if (!$ret || is_scalar($ret)) {
$str = $ret;
} else {
// $output->prettyJSON($ret);
$output->write($this->jsonRender()->renderData($ret));
$str = $this->jsonRender()->renderData($ret);
}

$outFile = $fs->getOpt('output');
ContentsAutoWriter::writeTo($outFile, $str);
}

/**
Expand Down Expand Up @@ -276,10 +280,10 @@ public function ml2lineCommand(): void
* source The source json contents
*
* @options
* -o, --output The output target. default is STDOUT.
* --tpl-dir The custom template file dir.
* --tpl-file The custom template file path.
* -t, --type string;the generate code language type, allow: java, php;;php
* -o, --output The output target. default is STDOUT.
* --tpl-dir The custom template file dir.
* --tpl, --tpl-file The custom template file path.
* -t, --type string;the generate code language type, allow: java, php;;php
*
* @param FlagsParser $fs
* @param Output $output
Expand Down
10 changes: 5 additions & 5 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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 Inhere\Kite\Lib\Stream\StringStream;
use InvalidArgumentException;
use Throwable;
use Toolkit\FsUtil\File;
Expand Down Expand Up @@ -339,9 +339,9 @@ public function processCommand(FlagsParser $fs, Output $output): void
}
}

$s = StringsStream::new(explode($sep, $text))
->eachIf('trim', $trim)
->eachIf(function (string $line) use ($cutPos, $cutChar) {
$s = StringStream::new(explode($sep, $text))
->mapIf('trim', $trim)
->mapIf(function (string $line) use ($cutPos, $cutChar) {
if (!str_contains($line, $cutChar)) {
return $line;
}
Expand All @@ -355,7 +355,7 @@ public function processCommand(FlagsParser $fs, Output $output): void
->filterIf(function (string $line) use ($in) { // include
return Str::has($line, $in);
}, count($in) > 0)
->eachIf(function (string $line) use ($filters) { // filters
->mapIf(function (string $line) use ($filters) { // filters
return $this->applyFilters($line, $filters);
}, $filters);

Expand Down
6 changes: 3 additions & 3 deletions test/unittest/BaseKiteTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected static function getMethod($class, string $method): ReflectionMethod
// $class = new \ReflectionClass($class);
// $method = $class->getMethod($method);

$method = new ReflectionMethod($class, $method);
$method->setAccessible(true);
$rftMth = new ReflectionMethod($class, $method);
$rftMth->setAccessible(true);

return $method;
return $rftMth;
}

/**
Expand Down

0 comments on commit aa708b9

Please sign in to comment.