Skip to content

Commit

Permalink
👔 up: update some for java dto class to json logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 25, 2024
1 parent a815bbb commit 16552f0
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 19 deletions.
39 changes: 32 additions & 7 deletions app/Console/Controller/Tool/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,8 +43,8 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'rpt' => 'repeat',
'tpl' => 'template',
'rpt' => 'repeat',
'tpl' => 'template',
];
}

Expand All @@ -54,6 +55,7 @@ protected function subCommands(): array
RandomCommand::class,
];
}

/**
* @param Output $output
*/
Expand All @@ -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.
*
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
21 changes: 14 additions & 7 deletions app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}

Expand Down Expand Up @@ -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();
}

Expand Down
152 changes: 148 additions & 4 deletions app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}
8 changes: 8 additions & 0 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
]);
}

Expand Down
1 change: 0 additions & 1 deletion app/Lib/Parser/Java/JavaDTOParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public function doParse(): JavaDTOMeta
if ($field) {
$meta->fields[] = $field;
}

return $meta;
}

Expand Down

0 comments on commit 16552f0

Please sign in to comment.