Skip to content

Commit

Permalink
feat: complate the sql to md tool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 2, 2021
1 parent c1c2573 commit 7a08c1c
Show file tree
Hide file tree
Showing 24 changed files with 994 additions and 575 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Common\Traits;
namespace Inhere\Kite\Concern;

use Inhere\Kite\Common\GitAPI\GitHubV3API;
use Inhere\Kite\Common\GitAPI\GitLabV4API;
use Inhere\Kite\Kite;
use Leuffen\TextTemplate\TextTemplate;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Toolkit\Stdlib\Arr\ArrayHelper;
Expand All @@ -14,7 +15,6 @@
use function defined;
use function file_exists;
use function is_dir;
use function is_file;

/**
* Trait InitApplicationTrait
Expand Down Expand Up @@ -103,6 +103,10 @@ protected function registerComServices(ObjectBox $box): void
return $logger;
});

$box->set('txtRender', function () {
return new TextTemplate();
});

$box->set('glApi', function () {
$config = $this->getArrayParam('gitlab');

Expand Down
91 changes: 91 additions & 0 deletions app/Concern/StaticPathAliasTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Concern;

use InvalidArgumentException;
use function explode;
use function str_contains;
use function str_replace;

/**
* Class PathAliasTrait
*/
trait StaticPathAliasTrait
{
/**
* @var array
*/
protected static array $aliases = [];

/**
* get real value by alias
*
* @param string $alias
* @return string
*/
public static function alias(string $alias): string
{
// Not an alias
if (!$alias || $alias[0] !== '@') {
return $alias;
}

$sep = '/';
$other = '';
$alias = str_replace('\\', $sep, $alias);

// have other partial. e.g: @project/temp/logs
if (str_contains($alias, $sep)) {
[$alias, $other] = explode($sep, $alias, 2);
}

if (!isset(self::$aliases[$alias])) {
throw new InvalidArgumentException("The alias name '$alias' is not registered!");
}

return self::$aliases[$alias] . ($other ? $sep . $other : '');
}

/**
* @param string $name
* @return bool
*/
public static function hasAlias(string $name): bool
{
return isset(self::$aliases[$name]);
}

/**
* @param string $alias
* @param string $value
* @throws InvalidArgumentException
*/
public static function setAlias(string $alias, string $value): void
{
self::$aliases[$alias] = self::alias($value);
}

/**
* @param array $aliases
* @throws InvalidArgumentException
*/
public static function setAliases(array $aliases): void
{
foreach ($aliases as $alias => $realPath) {
// the 1th char must is '@'
if (!$alias || $alias[0] !== '@') {
continue;
}

self::$aliases[$alias] = self::alias($realPath);
}
}

/**
* @return array
*/
public static function getAliases(): array
{
return self::$aliases;
}
}
2 changes: 1 addition & 1 deletion app/Console/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Inhere\Kite\Console\Listener\BeforeCommandRunListener;
use Inhere\Kite\Console\Listener\BeforeRunListener;
use Inhere\Kite\Lib\Jump\QuickJump;
use Inhere\Kite\Common\Traits\InitApplicationTrait;
use Inhere\Kite\Concern\InitApplicationTrait;
use Inhere\Kite\Console\Listener\NotFoundListener;
use Inhere\Kite\Console\Plugin\PluginManager;
use Inhere\Kite\Kite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use function substr;

/**
* class ContentsTryReader
* class ContentsAutoReader
*/
class ContentsTryReader extends AbstractObj
class ContentsAutoReader extends AbstractObj
{
public const TYPE_CLIPBOARD = 'clipboard';

Expand All @@ -25,6 +25,17 @@ class ContentsTryReader extends AbstractObj
*/
protected string $srcType = self::TYPE_STRING;

/**
* @param string $source
* @param array $opts
*
* @return string
*/
public static function readFrom(string $source, array $opts = []): string
{
return (new self())->read($source, $opts);
}

/**
* try read contents
*
Expand Down
19 changes: 19 additions & 0 deletions app/Console/Component/ContentsAutoWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Component;

/**
* class ContentsAutoWriter
*/
class ContentsAutoWriter
{
/**
* @param string $contents
*
* @return bool
*/
public function write(string $contents): bool
{
return true;
}
}
25 changes: 9 additions & 16 deletions app/Console/Controller/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Lib\Convert\JavaProperties;
use Inhere\Kite\Lib\Convert\SQLMarkdown;
use Inhere\Kite\Lib\Parser\DBCreateSQL;
use Inhere\Kite\Lib\Parser\DBTable;
use InvalidArgumentException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
Expand Down Expand Up @@ -78,15 +77,13 @@ protected static function commandAliases(): array
public function md2sqlCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = AppHelper::tryReadContents($source);
$source = ContentsAutoReader::readFrom($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
}

$obj = new SQLMarkdown();
$sql = $obj->toCreateSQL($source);

$sql = DBTable::fromMdTable($source)->toCreateSQL();
$output->writeRaw($sql);
}

Expand All @@ -103,20 +100,16 @@ public function md2sqlCommand(FlagsParser $fs, Output $output): void
public function sql2mdCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = AppHelper::tryReadContents($source);
$source = ContentsAutoReader::readFrom($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
}

// $obj = new SQLMarkdown();
// $sql = $obj->toMdTable($source);

$p = new DBCreateSQL();
$p->parse($source);

$sql = $p->genMDTable();
$output->writeRaw($sql);
$md = DBTable::fromSchemeSQL($source)->toMDTable();
$output->writeRaw($md);
// $cm = new CliMarkdown();
// $output->println($cm->parse($md));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use InvalidArgumentException;
Expand Down Expand Up @@ -97,7 +98,7 @@ private function loadDumpfileJSON(): void
*/
private function autoReadJSON(string $source): void
{
$this->json = AppHelper::tryReadContents($source, [
$this->json = ContentsAutoReader::readFrom($source, [
'loadedFile' => $this->dumpfile,
]);
if (!$this->json) {
Expand Down
50 changes: 42 additions & 8 deletions app/Console/Controller/SqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Lib\Parser\DBCreateSQL;
use Inhere\Kite\Lib\Parser\DBTable;
use InvalidArgumentException;
use Toolkit\PFlag\FlagsParser;

Expand All @@ -25,13 +26,48 @@ class SqlController extends Controller

protected static $description = 'Some useful development tool commands for SQL';

/**
* @return string[][]
*/
protected static function commandAliases(): array
{
return [
'md' => ['2md', 'to-md'],
];
}

/**
* convert create mysql table SQL to markdown table
*
* @options
* -s,--source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function mdCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = ContentsAutoReader::readFrom($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
}

$md = DBTable::fromSchemeSQL($source)->toMDTable();
$output->writeRaw($md);
// $cm = new CliMarkdown();
// $output->println($cm->parse($md));
}

/**
* collect fields from create table SQL.
*
* @options
* -s, --source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -s, --source string;The source create SQL for convert. allow: FILEPATH, @clipboard;true
* -o, --output The output target. default is stdout.
* --case Convert the field case.
* --case Change the field name case. allow: camel, snake
*
* @param FlagsParser $fs
* @param Output $output
Expand All @@ -45,13 +81,11 @@ public function fieldsCommand(FlagsParser $fs, Output $output): void
throw new InvalidArgumentException('empty source code for convert');
}

// $obj = new SQLMarkdown();
// $sql = $obj->toMdTable($source);
$toCemal = $fs->getOpt('case') === 'camel';

$p = new DBCreateSQL();
$p->parse($source);
$dbt = DBTable::fromSchemeSQL($source);
$mp = $dbt->getFieldsComments($toCemal);

$mp = $p->getFieldsComments();
$output->aList($mp);
}
}

0 comments on commit 7a08c1c

Please sign in to comment.