diff --git a/app/Console/Controller/Tool/GenerateController.php b/app/Console/Controller/Tool/GenerateController.php index 38d5c02..044ce3b 100644 --- a/app/Console/Controller/Tool/GenerateController.php +++ b/app/Console/Controller/Tool/GenerateController.php @@ -14,6 +14,7 @@ use PhpPkg\Ini\Ini; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Str; +use Toolkit\Stdlib\Util\UUID; use Toolkit\Sys\Proc\ProcWrapper; use function date; use function explode; @@ -42,8 +43,8 @@ public static function aliases(): array protected static function commandAliases(): array { return [ - 'rpt' => 'repeat', - 'tpl' => 'template', + 'rpt' => 'repeat', + 'tpl' => 'template', ]; } @@ -54,6 +55,7 @@ protected function subCommands(): array RandomCommand::class, ]; } + /** * @param Output $output */ @@ -62,6 +64,29 @@ public function readmeCommand(Output $output): void $output->success('Complete'); } + /** + * Generate a uuid string + * + * @options + * --version, -v int; the version of uuid + * --raw bool; output as raw format + * + * @param FlagsParser $fs + * @param Output $output + */ + public function uuidCommand(FlagsParser $fs, Output $output): void + { + $version = $fs->getOpt('version', 4); + + $u = UUID::new($version); + + if ($fs->getOpt('raw')) { + $output->println($u->getRaw()); + } else { + $output->println($u->getValue()); + } + } + /** * Create an template file on runtime dir. * @@ -72,7 +97,7 @@ public function readmeCommand(Output $output): void * filename The template filename. If not set, will auto generate by datetime. * * @param FlagsParser $fs - * @param Output $output + * @param Output $output */ public function templateCommand(FlagsParser $fs, Output $output): void { @@ -91,7 +116,7 @@ public function templateCommand(FlagsParser $fs, Output $output): void * tpl string;The template filepath;required * * @param FlagsParser $fs - * @param Output $output + * @param Output $output */ public function parseCommand(FlagsParser $fs, Output $output): void { @@ -119,7 +144,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void * tpl string;The template filepath;required * * @param FlagsParser $fs - * @param Output $output + * @param Output $output */ public function repeatCommand(FlagsParser $fs, Output $output): void { @@ -166,7 +191,7 @@ public function repeatCommand(FlagsParser $fs, Output $output): void * -t, --type The type. allow: number, string * * @param FlagsParser $fs - * @param Output $output + * @param Output $output * * @throws Exception */ @@ -192,7 +217,7 @@ public function idCommand(FlagsParser $fs, Output $output): void * -t, --template The sample template name. allow: alpha, alpha_num, alpha_num_c * * @param FlagsParser $fs - * @param Output $output + * @param Output $output * * @throws Exception */ diff --git a/app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php b/app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php index 5dc1546..32248e0 100644 --- a/app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php +++ b/app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php @@ -27,10 +27,11 @@ public static function aliases(): array protected function configure(): void { $this->flags->addOptsByRules([ - 'source,s' => 'string;The class code or filepath to parse. allow use @c for clipboard;required', - 'output,o' => 'string;The output target for result;;stdout', - 'json5,5' => 'bool;set output json5 format data, will with comment', - 'start,pos' => 'int;The start position for parse. 0=header, 1=class, 2=body;;0', + 'source,s' => 'string;The class code or filepath to parse. allow use @c for clipboard;required', + 'output,o' => 'string;The output target for result;;stdout', + 'json5,5' => 'bool;set output json5 format data, will with comment', + 'start,pos' => 'int;The start position for parse. 0=header, 1=class, 2=body;;0', + 'inline-comment,ic' => 'bool;use inline comment on output the json5 data', ]); } @@ -61,15 +62,21 @@ protected function execute(Input $input, Output $output): int $str = Json::pretty($map); } else { // output json5 + $inlineComment = $fs->getOpt('inline-comment'); + $sb = StrBuilder::new("{\n"); $sb->append("\n"); foreach ($m->fields as $field) { - $sb->writef(" // %s\n", $field->comment); - $sb->writef(" \"%s\": %s,\n", $field->name, $field->exampleValue(true)); + $value = $field->exampleValue(true); + if ($inlineComment) { + $sb->writef(" \"%s\": %s, // %s\n", $field->name, $value, $field->comment); + } else { + $sb->writef(" // %s\n", $field->comment); + $sb->writef(" \"%s\": %s,\n", $field->name, $value); + } } $sb->append('}'); - $str = $sb->getAndClear(); } diff --git a/app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php b/app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php index 992db65..3b2de24 100644 --- a/app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php +++ b/app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php @@ -5,6 +5,15 @@ use Inhere\Console\Command; use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; +use Inhere\Kite\Console\Component\ContentsAutoReader; +use Inhere\Kite\Console\Component\ContentsAutoWriter; +use Inhere\Kite\Lib\Defines\FieldMeta; +use Inhere\Kite\Lib\Generate\Json5Data; +use Inhere\Kite\Lib\Parser\DBTable; +use Inhere\Kite\Lib\Parser\MySQL\TableField; +use Inhere\Kite\Lib\Parser\Text\TextParser; +use Inhere\Kite\Lib\Stream\ListStream; +use InvalidArgumentException; /** * Class GenerateDTOCmd @@ -24,6 +33,31 @@ protected function configure(): void // $this->flags->addOptByRule($name, $rule); } + protected function getOptions(): array + { + return [ + '--source, -s' => 'source create-sql/markdown-table/field-map-txt for parse. +allow: +FILEPATH, @clipboard, @stdin + ', + '-t, --source-type' => 'source contents type, allow: sql,md,txt,json;true', + '-c, --config' => 'can load custom config from a file, allow: ini,json,yaml', + '--show-info' => 'bool;show config and context for generate', + '--tpl, --tpl-file' => 'generate for give tpl file name or path', + '--ctx' => 'array;provide context data, format KEY:VALUE', + '--pkg, --module-pkg' => 'the java DTO class package. eg: com.kezhilian.wzl.service.order', + '--sub, --with-subs' => 'bool;on use json data, whether generate sub-objects as inner class', + '--name' => 'the DTO class name, not need suffix DTO', + '--output, -o' => 'string;the output for write result codes;;stdout', + // override, append, skip + '--wflag' => 'string;write contents flag setting, allow: +a - append, append contents to exists file +o - override, override exists file +s - skip, skip write exists file;;s + ', + ]; + } + /** * @options * -s,--source The source code file or contents. if input '@c', will read from clipboard @@ -32,11 +66,121 @@ protected function configure(): void * @param Input $input * @param Output $output * - * @return int */ - protected function execute(Input $input, Output $output): int + protected function execute(Input $input, Output $output): void { - $output->success('TODO'); - return 0; + $fs = $this->flags; + + $source = $fs->getOpt('source'); + $source = ContentsAutoReader::readFrom($source); + + $this->loadContextData($fs); + + $srcType = $fs->getOpt('source-type'); + if (!$srcType) { + $srcType = 'txt'; + if (stripos($source, 'create table')) { + $srcType = 'sql'; + } elseif (str_contains($source, '--|--')) { + $srcType = 'md'; + } + } + + $subObjects = []; + switch ($srcType) { + case 'txt': + $p = TextParser::new($source)->parse(); + $mp = ListStream::new($p->getData()) + ->filter(function (array $item) { + return count($item) >= 3; + }) + ->eachToMap(function (array $item) { + return [ + $item[0], + FieldMeta::new([ + 'name' => $item[0], + 'type' => $item[1], + 'comment' => $item[2], + ]) + ]; + }); + + break; + case 'sql': + $dbt = DBTable::fromSchemeSQL($source); + $mp = $dbt->getObjFields(TableField::class); + + break; + case 'md': + $dbt = DBTable::fromMdTable($source); + $mp = $dbt->getObjFields(TableField::class); + + // $mp = MapStream::new($dbt->getFields()) + // ->eachToMap($this->dbTableInfoHandler()); + break; + case 'json': + case 'json5': + $jd = Json5Data::new()->loadFrom($source); + $mp = $jd->getFields(); + + $subObjects = $jd->getSubObjects(); + $this->config->load($jd->getSettings()); + break; + default: + throw new InvalidArgumentException("unsupported source type: $srcType"); + } + + $mainName = $this->config->getString('name'); + if (!$mainName) { + $mainName = $fs->getMustOpt('name'); + } elseif ($inName = $fs->getOpt('name')) { + $mainName = $inName; + } + + $this->config->set('mainName', lcfirst($mainName)); + $this->config->set('className', ucfirst($mainName)); + + $tplFile = $fs->getOpt('tpl-file'); + $tplFile = $tplFile ?: "@custom/template/java-code-tpl/req_dto.tpl"; + $outFile = $fs->getOpt('output'); + + if ($fs->getOpt('show-info')) { + $ctx = $this->config->toArray(); + + $ctx['tplFile'] = $tplFile; + $ctx['outFile'] = $outFile; + $output->mList([ + 'info' => $ctx, + 'fields' => $mp, + ]); + return; + } + + $tpl = $this->createRenderer(); + $ctx = $this->config->toArray(); + $output->mList([ + 'template' => $tplFile, + 'context' => $ctx, + 'fields' => $mp, + ]); + + $ctx['fields'] = $mp; + + $result = $tpl->renderFile($tplFile, $ctx); + + if ($subObjects) { + $ctx['withHead'] = false; + $ctx['classSfx'] = ''; + $ctx['classMark'] = 'static '; + foreach ($subObjects as $name => $fields) { + $ctx['mainName'] = $name; + $ctx['className'] = ucfirst($name); + $ctx['fields'] = $fields; + + $result .= $tpl->renderFile($tplFile, $ctx); + } + } + + ContentsAutoWriter::writeTo($outFile, $result); } } diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 83f8394..84caae8 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -125,6 +125,14 @@ public static function newTplEngine(array $config = []): EasyTemplate 'snake' => function (string $str): string { return Str::toSnake($str); }, + // prepend on not empty. + 'prepend' => function ($str, $char) { + return $str ? $char . $str : $str; + }, + // append on not empty. + 'append' => function ($str, $char) { + return $str ? $str . $char : $str; + }, ]); } diff --git a/app/Lib/Parser/Java/JavaDTOParser.php b/app/Lib/Parser/Java/JavaDTOParser.php index b1e87de..c3807f6 100644 --- a/app/Lib/Parser/Java/JavaDTOParser.php +++ b/app/Lib/Parser/Java/JavaDTOParser.php @@ -137,7 +137,6 @@ public function doParse(): JavaDTOMeta if ($field) { $meta->fields[] = $field; } - return $meta; }