From 7ca54914041f4e9ded328a4f6a53659fdbaf67e6 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 7 Nov 2021 20:32:53 +0800 Subject: [PATCH] up: update some for json to java class gen --- app/Console/Component/ContentsAutoReader.php | 2 +- app/Console/Component/ContentsAutoWriter.php | 2 +- app/Console/Controller/JsonController.php | 11 +++-- app/Helper/KiteUtil.php | 44 ++++---------------- app/Lib/Generate/AbstractJsonToCode.php | 43 +++---------------- app/Lib/Generate/Json/JsonField.php | 35 ++++++++++++++++ app/Lib/Generate/JsonToJavaClass.php | 3 +- app/Lib/Template/EasyTemplate.php | 13 ++++++ app/Lib/Template/TextTemplate.php | 16 +++++-- 9 files changed, 82 insertions(+), 87 deletions(-) diff --git a/app/Console/Component/ContentsAutoReader.php b/app/Console/Component/ContentsAutoReader.php index d09cfee..85a6b27 100644 --- a/app/Console/Component/ContentsAutoReader.php +++ b/app/Console/Component/ContentsAutoReader.php @@ -87,7 +87,7 @@ public function read(string $source, array $opts = []): string $print && Cli::info('try read contents from file: ' . $lFile); $str = File::readAll($lFile); } else { - $filepath = Kite::alias($source); + $filepath = Kite::resolve($source); if ($filepath[0] === '@') { $filepath = substr($filepath, 1); } diff --git a/app/Console/Component/ContentsAutoWriter.php b/app/Console/Component/ContentsAutoWriter.php index 9daf99a..9784034 100644 --- a/app/Console/Component/ContentsAutoWriter.php +++ b/app/Console/Component/ContentsAutoWriter.php @@ -73,7 +73,7 @@ public function write(string $contents): bool Kite::cliApp()->getOutput()->info('write results to file: ' . $this->output); } - $filepath = Kite::alias($this->output); + $filepath = Kite::resolve($this->output); // write File::mkdirSave($contents, $filepath); diff --git a/app/Console/Controller/JsonController.php b/app/Console/Controller/JsonController.php index 69fa1d8..8c6db12 100644 --- a/app/Console/Controller/JsonController.php +++ b/app/Console/Controller/JsonController.php @@ -311,20 +311,19 @@ public function toClassCommand(FlagsParser $fs, Output $output): void 'tplDir' => $tplDir, 'tplFile' => $tplFile, ])); -vdump($config); +// vdump($config); // @user-custom/template/java-service-tpl/req-resp-dto.tpl $gen = JsonToCode::create($type) ->setSource($json) - ->configObj($config) - ->setPathResolver([Kite::class, 'alias']) + ->configThis($config) + ->setPathResolver([Kite::class, 'resolve']) ->prepare(); $output->aList($gen->getFields(), 'field list'); - $contents = $gen->generate(); - + $result = $gen->generate(); $output->colored('------------------ Generated Codes -------------------'); - $output->writeRaw($contents); + $output->writeRaw($result); } /** diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 9ee30a4..67d30a7 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -2,6 +2,8 @@ namespace Inhere\Kite\Helper; +use Inhere\Kite\Kite; +use Inhere\Kite\Lib\Template\EasyTemplate; use Leuffen\TextTemplate\TextTemplate; use Toolkit\FsUtil\FS; use Toolkit\Stdlib\OS; @@ -91,45 +93,15 @@ public static function userConfigDir(string $path = ''): string } /** - * @param string $text - * - * @return TextTemplate + * @return EasyTemplate */ - public static function newTplEngine(string $text): TextTemplate + public static function newTplEngine(): EasyTemplate { - $tplEng = new TextTemplate($text); - // default value on empty. usage: {= ctx.user | default:inhere} - $tplEng->addFilter('default', function ($value, $default) { - if ($value === '') { - return $default; - } - return empty($value) ? $default : $value; - }); - - // upper first char. usage: {= ctx.user | upFirst} - $tplEng->addFilter('upFirst', function ($value) { - if ($value === '') { - return ''; - } - return Str::upFirst($value); - }); - - // snake to camel. usage: {= ctx.user | toCamel} - $tplEng->addFilter('toCamel', function ($value) { - if ($value === '') { - return ''; - } - return Str::toCamel($value); - }); - - // camel to snake. usage: {= ctx.user | toSnake} - $tplEng->addFilter('toSnake', function ($value) { - if ($value === '') { - return ''; - } - return Str::toSnake($value); - }); + // $tplEng = new TextTemplate($text); + $tplEng = new EasyTemplate(); + $tplEng->tmpDir = Kite::getTmpPath('tplCache'); + // some config return $tplEng; } diff --git a/app/Lib/Generate/AbstractJsonToCode.php b/app/Lib/Generate/AbstractJsonToCode.php index eb0c42f..e4034b6 100644 --- a/app/Lib/Generate/AbstractJsonToCode.php +++ b/app/Lib/Generate/AbstractJsonToCode.php @@ -26,6 +26,7 @@ use function strpos; use function substr; use function trim; +use function vdump; /** * class AbstractJsonToCode @@ -78,7 +79,6 @@ abstract class AbstractJsonToCode /** * @return string - * @throws Throwable */ public function generate(): string { @@ -142,55 +142,22 @@ public function prepare(): self /** * @return string - * @throws Throwable */ protected function renderTplText(): string { $tplText = $this->readSourceFromFile(); - $tplEng = KiteUtil::newTplEngine($tplText); - - $tplEng->addFunction( - 'needToSnake', - function ($paramArr) { - $name = $paramArr['name']; - if (str_contains($name, '_')) { - return true; - } - - if (preg_match('/[A-Z]/', $name)) { - return true; - } - - return false; - }); - - // {toJavaType type=field.type name=field.name} - $tplEng->addFunction( - 'toJavaType', - function ($paramArr, $command, $context, $cmdParam, $self) { - $type = $paramArr['type']; - $name = $paramArr['name']; - if ($type === Type::ARRAY) { - return JavaType::OBJECT; - } - - if (str_ends_with($name, 'id') || str_ends_with($name, 'Id')) { - return JavaType::LONG; - } - - return Str::upFirst($type); - }); + $tplEng = KiteUtil::newTplEngine(); $settings = array_merge([ 'user' => OS::getUserName(), ], $this->contexts); - +vdump($settings); $tplVars = [ 'ctx' => $settings, 'fields' => $this->fields, ]; - return $tplEng->apply($tplVars, false); + return $tplEng->renderString($tplText, $tplVars); } /** @@ -212,7 +179,7 @@ public function generateTo(string $outFile): bool * * @return self */ - public function configObj(array $config): self + public function configThis(array $config): self { Obj::init($this, $config); diff --git a/app/Lib/Generate/Json/JsonField.php b/app/Lib/Generate/Json/JsonField.php index 69ca4d6..53dda28 100644 --- a/app/Lib/Generate/Json/JsonField.php +++ b/app/Lib/Generate/Json/JsonField.php @@ -2,12 +2,15 @@ namespace Inhere\Kite\Lib\Generate\Json; +use Inhere\Kite\Lib\Generate\Java\JavaType; use JsonSerializable; use Toolkit\Stdlib\Json; use Toolkit\Stdlib\Obj\AbstractObj; +use Toolkit\Stdlib\Str; use Toolkit\Stdlib\Type; use function gettype; use function json_encode; +use function preg_match; /** * class JsonField @@ -18,6 +21,38 @@ class JsonField extends AbstractObj implements JsonSerializable public string $type; public string $desc; + /** + * @return string + */ + public function toJavaType(): string + { + if ($this->type === Type::ARRAY) { + return JavaType::OBJECT; + } + + if (str_ends_with($this->name, 'id') || str_ends_with($this->name, 'Id')) { + return JavaType::LONG; + } + + return Str::upFirst($this->type); + } + + /** + * @return bool + */ + public function isMultiWords(): bool + { + if (str_contains($this->name, '_')) { + return true; + } + + if (preg_match('/[A-Z]/', $this->name)) { + return true; + } + + return false; + } + /** * @return array */ diff --git a/app/Lib/Generate/JsonToJavaClass.php b/app/Lib/Generate/JsonToJavaClass.php index eab0e63..21a4e76 100644 --- a/app/Lib/Generate/JsonToJavaClass.php +++ b/app/Lib/Generate/JsonToJavaClass.php @@ -18,7 +18,8 @@ class JsonToJavaClass extends AbstractJsonToCode public function generate(): string { // todo - $this->contexts['pkgName'] = ''; + $this->contexts['pkgName'] = 'YOUR.PKG.NAME'; + $this->contexts['className'] = 'YourClass'; return parent::generate(); } diff --git a/app/Lib/Template/EasyTemplate.php b/app/Lib/Template/EasyTemplate.php index a619176..8751bbb 100644 --- a/app/Lib/Template/EasyTemplate.php +++ b/app/Lib/Template/EasyTemplate.php @@ -95,6 +95,19 @@ public function compileCode(string $code): string return $this->compiler->compile($code); } + /** + * @param string $name + * @param callable $handler + * + * @return $this + */ + public function addFilter(string $name, callable $handler): self + { + // + // $this->compiler->addFilter(); + return $this; + } + /** * @param string $open * @param string $close diff --git a/app/Lib/Template/TextTemplate.php b/app/Lib/Template/TextTemplate.php index f61ba21..4512a60 100644 --- a/app/Lib/Template/TextTemplate.php +++ b/app/Lib/Template/TextTemplate.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use RuntimeException; +use Throwable; use Toolkit\FsUtil\Dir; use Toolkit\Stdlib\Obj; use Toolkit\Sys\Sys; @@ -12,7 +13,9 @@ use function extract; use function file_exists; use function file_put_contents; +use function hash; use function md5; +use function ob_clean; use function ob_get_clean; use function ob_start; use function sprintf; @@ -98,9 +101,14 @@ protected function doRenderFile(string $tplFile, array $tplVars): string ob_start(); extract($tplVars, EXTR_OVERWRITE); - // require \BASE_PATH . '/runtime/go-snippets-0709.tpl.php'; - require $tplFile; - return ob_get_clean(); + try { + // require \BASE_PATH . '/runtime/go-snippets-0709.tpl.php'; + require $tplFile; + return ob_get_clean(); + } catch (Throwable $e) { + ob_clean(); + throw $e; + } } /** @@ -113,7 +121,7 @@ protected function doRenderFile(string $tplFile, array $tplVars): string protected function genTempPhpFile(string $tplCode): string { $tmpDir = $this->tmpDir ?: Sys::getTempDir() . '/php-tpl-gen'; - $tmpFile = sprintf('%s/%s-%s.php', $tmpDir, date('ymd'), md5($tplCode)); + $tmpFile = sprintf('%s/%s-%s.php', $tmpDir, date('ymd_his'), md5($tplCode)); if (!file_exists($tmpFile)) { Dir::create($tmpDir);