From 5925a4c748e51ed6532301db755857f95b8e1c1b Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 17 Nov 2022 17:03:19 +0800 Subject: [PATCH] up: update some for custom dir, update jenkins lib --- app/Concern/InitApplicationTrait.php | 4 +- app/Helper/KiteUtil.php | 4 +- app/Kite.php | 7 +- app/Lib/Jenkins/JenkinsClient.php | 130 ------------- app/Lib/Jenkins/JenkinsConfig.php | 59 ++++++ app/Lib/Jenkins/JenkinsFactory.php | 184 ++++++++++++++++++ app/Lib/Parser/DBSchemeSQL.php | 4 +- app/Lib/Parser/MySQL/TableField.php | 7 +- app/boot.php | 1 + app/func.php | 11 ++ composer.json | 1 + config/config.cli.php | 2 + custom/console/{commands => Command}/.keep | 0 .../console/{controllers => Controller}/.keep | 0 resource/example/jenkins-build-info.json | 47 +++++ resource/example/md2sql.md | 20 +- script/update-kite-deps.sh | 4 +- 17 files changed, 326 insertions(+), 159 deletions(-) delete mode 100644 app/Lib/Jenkins/JenkinsClient.php create mode 100644 app/Lib/Jenkins/JenkinsConfig.php create mode 100644 app/Lib/Jenkins/JenkinsFactory.php rename custom/console/{commands => Command}/.keep (100%) rename custom/console/{controllers => Controller}/.keep (100%) create mode 100644 resource/example/jenkins-build-info.json diff --git a/app/Concern/InitApplicationTrait.php b/app/Concern/InitApplicationTrait.php index dd4c825..f24df0e 100644 --- a/app/Concern/InitApplicationTrait.php +++ b/app/Concern/InitApplicationTrait.php @@ -5,7 +5,7 @@ use Inhere\Kite\Common\GitAPI\GitHubV3API; use Inhere\Kite\Common\GitAPI\GitLabV4API; use Inhere\Kite\Kite; -use Inhere\Kite\Lib\Jenkins\JenkinsClient; +use Inhere\Kite\Lib\Jenkins\JenkinsFactory; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; use PhpPkg\Config\ConfigBox; @@ -129,7 +129,7 @@ protected function registerComServices(ObjectBox $box): void $box->set('jenkins', function () { $config = $this->config()->getArray('jenkins'); - return new JenkinsClient($config); + return new JenkinsFactory($config); }); } diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 7a46686..a6f81fd 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -20,18 +20,20 @@ */ class KiteUtil { - public const NL_CHAR = 'NL'; + public const TAB_CHAR = 'TAB'; public const SPACE_CHAR = 'SPACE'; public const STDIN_ALIAS = [ '@i', + '@si', '@stdin', 'stdin', ]; public const STDOUT_ALIAS = [ '@o', + '@so', '@stdout', 'stdout', ]; diff --git a/app/Kite.php b/app/Kite.php index 56a8809..d0c9d1a 100644 --- a/app/Kite.php +++ b/app/Kite.php @@ -10,14 +10,13 @@ namespace Inhere\Kite; use BadMethodCallException; -use Composer\Autoload\ClassLoader; use Inhere\Kite\Component\ScriptRunner; use Inhere\Kite\Concern\StaticPathAliasTrait; use Inhere\Kite\Console\CliApplication; use Inhere\Kite\Console\Component\AutoSetProxyEnv; use Inhere\Kite\Console\Plugin\PluginManager; use Inhere\Kite\Http\WebApplication; -use Inhere\Kite\Lib\Jenkins\JenkinsClient; +use Inhere\Kite\Lib\Jenkins\JenkinsFactory; use Inhere\Kite\Lib\Jump\QuickJump; use Inhere\Route\Dispatcher\Dispatcher; use Inhere\Route\Router; @@ -39,7 +38,7 @@ * @method static Router webRouter() * @method static Dispatcher dispatcher() * @method static ScriptRunner scriptRunner() - * @method static JenkinsClient jenkins() + * @method static JenkinsFactory jenkins() * * @see Kite::__callStatic() for quick get object */ @@ -76,7 +75,7 @@ class Kite */ private static WebApplication $webApp; - /** + /* * @var ClassLoader */ // public static ClassLoader $loader; diff --git a/app/Lib/Jenkins/JenkinsClient.php b/app/Lib/Jenkins/JenkinsClient.php deleted file mode 100644 index 61517d4..0000000 --- a/app/Lib/Jenkins/JenkinsClient.php +++ /dev/null @@ -1,130 +0,0 @@ -jobName = $jobName; - return $this; - } - - /** - * @param array $params - * - * @return string - */ - public function buildWithParams(array $params): string - { - $tpl = '{folderPath}/job/{name}/buildWithParameters'; - $url = $this->buildUrl($tpl); - $cli = $this->getHttpClient()->post($url, $params); - - return $cli->getResponseBody(); - } - - public function buildNoParams(): self - { - $tpl = '{folderPath}/job/{name}/build'; - $url = $this->buildUrl($tpl); - - return $this; - } - - /** - * @param string $pathTpl - * - * @return string - */ - public function buildUrl(string $pathTpl): string - { - return $this->hostUrl . strtr($pathTpl, [ - '{folderPath}' => $this->folderPath, - '{name}' => $this->jobName, - ]); - } - - /** - * @param string $jobName - * - * @return string - */ - public function jobPageUrl(string $jobName = ''): string - { - $jobName = $jobName ?: $this->jobName; - - return $this->hostUrl . '/job/' . $jobName; - } - - /** - * @return AbstractClient - */ - public function getHttpClient(): AbstractClient - { - if (!$this->httpClient) { - $this->httpClient = Client::factory([]); - } - - return $this->httpClient; - } - - /** - * @param AbstractClient $httpClient - */ - public function setHttpClient(AbstractClient $httpClient): void - { - $this->httpClient = $httpClient; - } - - -} diff --git a/app/Lib/Jenkins/JenkinsConfig.php b/app/Lib/Jenkins/JenkinsConfig.php new file mode 100644 index 0000000..2cfce0b --- /dev/null +++ b/app/Lib/Jenkins/JenkinsConfig.php @@ -0,0 +1,59 @@ +hostUrl . '/job/' . $jobName; + } + + /** + * @param string $viewName + * + * @return string + */ + public function viewPageUrl(string $viewName): string + { + return $this->hostUrl . '/view/' . $viewName; + } + +} diff --git a/app/Lib/Jenkins/JenkinsFactory.php b/app/Lib/Jenkins/JenkinsFactory.php new file mode 100644 index 0000000..54826a1 --- /dev/null +++ b/app/Lib/Jenkins/JenkinsFactory.php @@ -0,0 +1,184 @@ + ['username' => '', 'apiToken' => '', 'hostUrl' => '']] + */ + public array $envInfo = []; + + /** + * @param string $jobName + * + * @return $this + */ + public function withJobName(string $jobName): self + { + $this->jobName = $jobName; + return $this; + } + + /** + * @param string $env + * + * @return Jenkins + */ + public function getJenkins(string $env = ''): Jenkins + { + return $this->create($env); + } + + /** + * @param string $env + * + * @return Jenkins + */ + public function create(string $env = ''): Jenkins + { + $jc = $this->getEnvConfig($env); + $jk = new Jenkins($jc->hostUrl); + + $jk->setUsername($jc->username); + $jk->setPassword($jc->password); + $jk->setApiToken($jc->apiToken); + + return $jk; + } + + /** + * @param string $pathTpl + * + * @return string + */ + public function buildUrl(string $pathTpl): string + { + return $this->hostUrl . strtr($pathTpl, [ + '{folderPath}' => $this->folderPath, + '{name}' => $this->jobName, + ]); + } + + /** + * @param string $envName + * + * @return $this + */ + public function useEnv(string $envName): self + { + return $this->setEnvName($envName); + } + + /** + * @param string $envName + * + * @return $this + */ + public function setEnvName(string $envName): self + { + if ($envName) { + $this->envName = $envName; + } + return $this; + } + + /** + * @param string $envName + * + * @return JenkinsConfig + */ + public function getEnvConfig(string $envName = ''): JenkinsConfig + { + $defConf = $this->getDefaultConfig(); + $envName = $envName ?: $this->envName; + + if ($envName) { + if (isset($this->envInfo[$envName])) { + return JenkinsConfig::new(array_merge($defConf->toArray(), $this->envInfo[$envName])); + } + + throw new RuntimeException("get unknown env config: $envName"); + } + + return $defConf; + } + + /** + * @return JenkinsConfig + */ + public function getDefaultConfig(): JenkinsConfig + { + if (!$this->defaultConfig) { + $this->defaultConfig = JenkinsConfig::new([ + 'hostUrl' => $this->hostUrl, + 'username' => $this->username, + 'apiToken' => $this->apiToken, + ]); + } + + return $this->defaultConfig; + } + + +} diff --git a/app/Lib/Parser/DBSchemeSQL.php b/app/Lib/Parser/DBSchemeSQL.php index 0bb5ded..596e627 100644 --- a/app/Lib/Parser/DBSchemeSQL.php +++ b/app/Lib/Parser/DBSchemeSQL.php @@ -157,11 +157,11 @@ protected function isIndexLine(string $row): bool return true; } - if (stripos($row, 'UNIQUE KEY') === 0) { + if (stripos($row, 'UNIQUE ') === 0) { return true; } - if (stripos($row, 'INDEX KEY') === 0) { + if (stripos($row, 'INDEX ') === 0) { return true; } diff --git a/app/Lib/Parser/MySQL/TableField.php b/app/Lib/Parser/MySQL/TableField.php index e8ef3f3..3db83d8 100644 --- a/app/Lib/Parser/MySQL/TableField.php +++ b/app/Lib/Parser/MySQL/TableField.php @@ -56,6 +56,10 @@ public function phpType(): string */ public function toJavaType(string $type, string $name): string { + if (Str::hasSuffixIC($this->name, 'id')) { + return JavaType::LONG; + } + if (Str::hasSuffixIC($this->name, 'ids')) { return sprintf('%s<%s>', JavaType::LIST, JavaType::LONG); } @@ -65,8 +69,7 @@ public function toJavaType(string $type, string $name): string return Str::upFirst($name); } - $type = DBType::toPhpType($type); - return Str::upFirst($type); + return Str::upFirst(DBType::toPhpType($type)); } /** diff --git a/app/boot.php b/app/boot.php index dcffd35..a94d38a 100644 --- a/app/boot.php +++ b/app/boot.php @@ -13,6 +13,7 @@ $loader->addPsr4("PhpPkg\\Config\\", $baseDir . '/vendor/phppkg/config/src/'); $loader->addPsr4("PhpPkg\\Ini\\", $baseDir . '/vendor/phppkg/ini/src/'); $loader->addPsr4("Custom\\Lib\\", $baseDir . '/custom/lib/'); +$loader->addPsr4("Custom\\Console\\", $baseDir . '/custom/console/'); // save loader for plugin manager // Kite::$loader = $loader; diff --git a/app/func.php b/app/func.php index fb829cd..69afbd0 100644 --- a/app/func.php +++ b/app/func.php @@ -13,6 +13,17 @@ function env(string $key, string $default = ''): string } } +/** + * @param string $path + * @param bool $rmPharMark + * + * @return string + */ +function kite_path(string $path = '', bool $rmPharMark = true): string +{ + return Inhere\Kite\Kite::getPath($path, $rmPharMark); +} + function load_kite(): void { if ($kiteDir = (string)getenv('KITE_PATH')) { diff --git a/composer.json b/composer.json index 76c3b4f..28635f4 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "gitonomy/gitlib": "^1.3", "psr/log": "^3.0", "psr/container": "^1.1", + "phppkg/jenkins-client": "dev-master", "phppkg/http-client": "dev-master", "phppkg/http-message": "^2.0", "phppkg/easytpl": "dev-main", diff --git a/config/config.cli.php b/config/config.cli.php index b381041..a94aa5e 100644 --- a/config/config.cli.php +++ b/config/config.cli.php @@ -1,5 +1,6 @@ [ // some class + JenkinsController::class, ], // --------- component object config ----------- diff --git a/custom/console/commands/.keep b/custom/console/Command/.keep similarity index 100% rename from custom/console/commands/.keep rename to custom/console/Command/.keep diff --git a/custom/console/controllers/.keep b/custom/console/Controller/.keep similarity index 100% rename from custom/console/controllers/.keep rename to custom/console/Controller/.keep diff --git a/resource/example/jenkins-build-info.json b/resource/example/jenkins-build-info.json new file mode 100644 index 0000000..a961f09 --- /dev/null +++ b/resource/example/jenkins-build-info.json @@ -0,0 +1,47 @@ +{ + "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun", + "actions": [ + { + "_class": "hudson.model.ParametersAction" + }, + { + "_class": "hudson.model.CauseAction" + }, + {}, + { + "_class": "org.jenkinsci.plugins.workflow.cps.EnvActionImpl" + }, + {}, + {}, + {}, + { + "_class": "org.jenkinsci.plugins.pipeline.modeldefinition.actions.RestartDeclarativePipelineAction" + }, + {}, + { + "_class": "org.jenkinsci.plugins.workflow.job.views.FlowGraphAction" + }, + {}, + {} + ], + "artifacts": [], + "building": false, + "description": "发布版本:v2.0.20 ....", + "displayName": "1747 fix", + "duration": 57887, + "estimatedDuration": 56176, + "executor": null, + "fingerprint": [], + "fullDisplayName": "php-pipeline 1747 fix", + "id": "1747", + "keepLog": false, + "number": 1747, + "queueId": 21987, + "result": "SUCCESS", + "timestamp": 1668071294434, + "url": "http://my-jenkins.dev/php-pipeline/1747/", + "changeSets": [], + "culprits": [], + "nextBuild": null, + "previousBuild": {} +} \ No newline at end of file diff --git a/resource/example/md2sql.md b/resource/example/md2sql.md index 86a5169..d29c5a1 100644 --- a/resource/example/md2sql.md +++ b/resource/example/md2sql.md @@ -3,29 +3,15 @@ ## create sql ```sql -CREATE TABLE `goods_v2_info` ( +CREATE TABLE `goods_info` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', - `sid` int(11) unsigned NOT NULL COMMENT '店铺id', - `glid` int(11) unsigned NOT NULL COMMENT '商品列表id', - `name` json NOT NULL COMMENT '二级商品名称', - `photo` json NOT NULL COMMENT '图片', - `show_params` json DEFAULT NULL COMMENT '自定义图片参数', - `broadcast` int(11) unsigned NOT NULL COMMENT '轮播间隔', - `show_en` tinyint(4) NOT NULL COMMENT '是否显示英文名。1,显示;2,不显示', - `show_en_name` varchar(128) NOT NULL COMMENT '英文名称', - `limitation` tinyint(4) NOT NULL COMMENT '限购提醒。1,限购,2,不限购', - `detail_show` tinyint(4) NOT NULL COMMENT '是否显示商品详情,1,显示。2,不显示', - `detail_name` json NOT NULL COMMENT '详情显示名', `detail_content` json NOT NULL COMMENT '详情内容', `atime` int(11) unsigned NOT NULL, - `title` json NOT NULL COMMENT '商品规格名称', - `deleted` tinyint(4) NOT NULL DEFAULT '2' COMMENT '是否删除,1,已删除;2,未删除 3,已废弃(脏数据标记-不可修改此状态进行还原)', + `deleted` tinyint(4) NOT NULL DEFAULT '2' COMMENT '是否删除,1,已删除;2,未删除, `deleted_at` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '删除时间', PRIMARY KEY (`id`), - KEY `idx_sid` (`sid`) USING BTREE, - KEY `idx_glid` (`glid`) USING BTREE, KEY `idx_deleted` (`deleted`) USING BTREE -) ENGINE=InnoDB AUTO_INCREMENT=9931 DEFAULT CHARSET=utf8mb4 COMMENT='菜品二级信息表'; +) ENGINE=InnoDB AUTO_INCREMENT=9931 DEFAULT CHARSET=utf8mb4 COMMENT='菜品信息表'; ``` ## md table diff --git a/script/update-kite-deps.sh b/script/update-kite-deps.sh index 932ae9f..57dd16b 100644 --- a/script/update-kite-deps.sh +++ b/script/update-kite-deps.sh @@ -2,6 +2,8 @@ # # - update composer.lock # - update all depends package +# - skip .git existed dev package +# - clone no .git existed dev package # # run: kite run --proxy update-kite-deps.sh @@ -24,7 +26,7 @@ else fi set -x -#kite env prox +#kite env proxy cd $tmpKiteDir || exit 2 git checkout . git pull