diff --git a/app/Console/Controller/DemoController.php b/app/Console/Controller/DemoController.php index 926a59d..b058c31 100644 --- a/app/Console/Controller/DemoController.php +++ b/app/Console/Controller/DemoController.php @@ -10,8 +10,8 @@ namespace Inhere\Kite\Console\Controller; use Inhere\Console\Controller; -use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; +use Toolkit\PFlag\FlagsParser; /** * Class DemoController @@ -27,6 +27,14 @@ public static function isEnabled(): bool return false; } + /** + * @return array{string: list} + */ + protected static function commandAliases(): array + { + return []; + } + /** * run a php built-in server for development(is alias of the command 'server:dev') * @@ -39,10 +47,10 @@ public static function isEnabled(): bool * -H,--host The server host address. e.g 127.0.0.1 * -p,--port The server host address. e.g 5577 * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function serveCommand(Input $input, Output $output): void + public function serveCommand(FlagsParser $fs, Output $output): void { $output->success('Complete'); } diff --git a/app/Console/Controller/HttpController.php b/app/Console/Controller/HttpController.php new file mode 100644 index 0000000..b32520b --- /dev/null +++ b/app/Console/Controller/HttpController.php @@ -0,0 +1,63 @@ +} + */ + protected static function commandAliases(): array + { + return [ + 'bulk2query' => ['2query', 'to-query'], + ]; + } + + /** + * convert k-v map text to http url query string. + * + * @options + * -s,--source string;The source k-v map text. allow: FILEPATH, @clipboard;true + * --not-enc,--not-encode bool;Not apply url-encode for generated string + * + * @param FlagsParser $fs + * @param Output $output + */ + public function bulk2queryCommand(FlagsParser $fs, Output $output): void + { + $source = $fs->getOpt('source'); + $source = ContentsAutoReader::readFrom($source); + + $output->colored('SOURCE:'); + $output->writeRaw($source); + + $query = str_replace([': ', "\n", ':'], ['=', '&', '='], $source); + if (!$fs->getOpt('not-encode')) { + $query = UrlHelper::encode($query); + } + + $output->colored('RESULT:'); + $output->writeRaw($query); + } +} diff --git a/app/Console/Controller/JsonController.php b/app/Console/Controller/JsonController.php index 5069db1..5e497e2 100644 --- a/app/Console/Controller/JsonController.php +++ b/app/Console/Controller/JsonController.php @@ -32,8 +32,8 @@ use function is_scalar; use function json_decode; use function str_contains; +use function str_replace; use function trim; -use function vdump; use const JSON_THROW_ON_ERROR; /** @@ -292,8 +292,6 @@ public function ml2lineCommand(): void */ public function toClassCommand(FlagsParser $fs, Output $output): void { - $config = Kite::cliApp()->getArrayParam('json_toClass'); - $type = $fs->getOpt('type'); $json = $fs->getArg('source'); $json = ContentsAutoReader::readFrom($json, [ @@ -304,11 +302,13 @@ public function toClassCommand(FlagsParser $fs, Output $output): void throw new InvalidArgumentException('empty input json(5) text for handle'); } - $tplDir = $fs->getOpt('tpl-dir'); + $config = Kite::cliApp()->getArrayParam('json_toClass'); + $tplDir = $fs->getOpt('tpl-dir', $config['tplDir'] ?? ''); + $tplDir = str_replace('{type}', $type, $tplDir); + $tplFile = $fs->getOpt('tpl-file'); if (!$tplFile) { - // $tplFile = "@resource-tpl/dto-class/$type-data-dto.tpl"; - $tplFile = "@user-custom/template/$type-code-tpl/req-resp-dto.tpl"; + $tplFile = "$tplDir/req-resp-dto.tpl"; } $config = array_merge($config, array_filter([ @@ -316,6 +316,8 @@ public function toClassCommand(FlagsParser $fs, Output $output): void 'tplFile' => $tplFile, ])); + $output->aList($config); + // @user-custom/template/java-service-tpl/req-resp-dto.tpl $gen = JsonToCode::create($type) ->setSource($json) diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 677959f..7a46686 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -103,7 +103,7 @@ public static function userConfigDir(string $path = ''): string } /** - * @param array $config + * @param array{tplDir: string, allowExt: array, globalVars: array} $config * * @return EasyTemplate */ diff --git a/app/Lib/Generate/AbstractJsonToCode.php b/app/Lib/Generate/AbstractJsonToCode.php index d49537a..9594111 100644 --- a/app/Lib/Generate/AbstractJsonToCode.php +++ b/app/Lib/Generate/AbstractJsonToCode.php @@ -57,11 +57,24 @@ abstract class AbstractJsonToCode */ // private array $jsonData = []; + /** + * @var string + */ + public string $className = 'YourClass'; + /** * @var array */ private array $fields = []; + /** + * @return string + */ + public function getLang(): string + { + return 'java'; + } + /** * @return string */ @@ -88,6 +101,9 @@ public function prepare(): self throw new InvalidArgumentException('empty source json(5) data for generate'); } + // defaults + $this->contexts['className'] = $this->className; + $jd = Json5Data::new()->loadFrom($json); $this->fields = $jd->getFields(); @@ -103,18 +119,16 @@ protected function renderTplText(): string { $tplText = $this->readSourceFromFile(); $settings = array_merge([ - 'lang' => 'java', + 'lang' => $this->getLang(), 'user' => OS::getUserName(), 'date' => date('Y-m-d'), ], $this->contexts); $settings['fields'] = $this->fields; - // $tplVars = [ - // 'ctx' => $settings, - // 'fields' => $this->fields, - // ]; - return KiteUtil::newTplEngine()->renderString($tplText, $settings); + return KiteUtil::newTplEngine([ + 'tplDir' => $this->tplDir, + ])->renderString($tplText, $settings); } /** @@ -228,4 +242,15 @@ public function setPathResolver(callable $pathResolver): self $this->pathResolver = $pathResolver; return $this; } + + /** + * @param string $className + * + * @return AbstractJsonToCode + */ + public function setClassName(string $className): self + { + $this->className = $className; + return $this; +} } diff --git a/app/Lib/Generate/JsonToJavaClass.php b/app/Lib/Generate/JsonToJavaClass.php index 505409d..f27ab53 100644 --- a/app/Lib/Generate/JsonToJavaClass.php +++ b/app/Lib/Generate/JsonToJavaClass.php @@ -18,9 +18,7 @@ public function generate(): string // todo $this->contexts['package'] = 'YOUR.PKG.NAME'; $this->contexts['pkgName'] = 'PKG_NAME'; - $this->contexts['className'] = 'YourClass'; return parent::generate(); } - } diff --git a/app/Lib/Generate/JsonToPHPClass.php b/app/Lib/Generate/JsonToPHPClass.php index 660147d..f4caaf0 100644 --- a/app/Lib/Generate/JsonToPHPClass.php +++ b/app/Lib/Generate/JsonToPHPClass.php @@ -2,8 +2,6 @@ namespace Inhere\Kite\Lib\Generate; -use function random_int; - /** * class JsonToPHPClass */ @@ -11,4 +9,11 @@ class JsonToPHPClass extends AbstractJsonToCode { public const TYPE = 'php'; + /** + * @return string + */ + public function getLang(): string + { + return self::TYPE; + } } diff --git a/script/intall-go.sh b/script/intall-go.sh new file mode 100644 index 0000000..0b9c20f --- /dev/null +++ b/script/intall-go.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# install go on linux + +GO_VER=1.17.6 +GO_TAR=go$GO_VER.linux-amd64.tar.gz +echo "Will install the $GO_TAR" + +cd /tmp || exit 2 +# mac https://studygolang.com/dl/golang/go1.17.6.darwin-amd64.pkg +# linux https://studygolang.com/dl/golang/go1.17.6.linux-amd64.tar.gz +wget https://studygolang.com/dl/golang/$GO_TAR + +# decompress +tar -C /usr/local -xzf $GO_TAR + +cat <