发布新版本;增加自动替换脚本

This commit is contained in:
2023-12-08 14:09:13 +08:00
parent 89c0961cbc
commit 9f2d0898ec
9 changed files with 232 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
<?php
namespace app\admin\service;
use base\admin\service\AdminUpdateCodeServiceBase;
class AdminUpdateCodeService extends AdminUpdateCodeServiceBase
{
}

View File

@@ -0,0 +1,13 @@
var init = {
tableElem: '#currentTable',
tableRenderId: 'currentTableRenderId',
indexUrl: 'mall.goods/index',
addUrl: 'mall.goods/add',
editUrl: 'mall.goods/edit',
deleteUrl: 'mall.goods/delete',
exportUrl: 'mall.goods/export',
modifyUrl: 'mall.goods/modify',
stock_url: 'mall.goods/stock',
read_url: 'mall.goods/read',
formFullScreen: true,
};

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace app\common\command\admin;
use base\common\command\admin\UpdateCodeBase;
class UpdateCode extends UpdateCodeBase
{
}

View File

@@ -7,6 +7,7 @@ use app\common\command\admin\Clear;
use app\common\command\admin\Version;
use app\common\command\admin\ResetPassword;
use app\common\command\admin\Update;
use app\common\command\admin\UpdateCode;
use app\common\command\curd\Migrate;
use app\common\command\Timer;
@@ -21,6 +22,7 @@ return [
Version::class,
Migrate::class,
Clear::class,
Update::class
Update::class,
UpdateCode::class
],
];

View File

@@ -0,0 +1,38 @@
<?php
namespace base\admin\service;
use think\console\Input;
use think\console\Output;
use UpdateFunction;
class AdminUpdateCodeServiceBase
{
/**
* @var Input
*/
public $input;
/**
* @var Output
*/
public $output;
public function update($version)
{
$class_function_path = app_file_path('admin/service/adminUpdateCodeData/' . $version . '.php');
if (!file_exists($class_function_path)) {
$this->output->error('指定版本无需特定更新');
return;
}
require_once $class_function_path;
$update_class = new UpdateFunction();
$update_class->input = $this->input;
$update_class->output = $this->output;
$update_class->update();
}
}

View File

@@ -0,0 +1,109 @@
<?php
use think\console\Input;
use think\console\Output;
use think\facade\App;
class UpdateFunction
{
/**
* @var Input
*/
public $input;
/**
* @var Output
*/
public $output;
public $replaceMap = [
'table_elem' => 'tableElem',
'table_render_id' => 'tableRenderId',
'index_url' => 'indexUrl',
'add_url' => 'addUrl',
'edit_url' => 'editUrl',
'delete_url' => 'deleteUrl',
'export_url' => 'exportUrl',
'modify_url' => 'modifyUrl',
];
public function update()
{
$this->output->writeln('更新代码');
$this->output->info('当前版本需要将js代码的init的属性从蛇形改为小驼峰');
$this->output->info('您可以通过编辑器的全局搜索替换功能完成');
$this->output->info('也可以使用当前命令替换');
$is_true = $this->output->confirm($this->input, '是否执行替换?');
if (!$is_true) {
$this->output->writeln('取消更新');
return;
}
// 扫描app下的所有js文件
$js_file_list = $this->scanJsFile();
// 将文件的内容替换
foreach ($js_file_list as $file_path) {
$file_content = file_get_contents($file_path);
foreach ($this->replaceMap as $search => $replace) {
$file_content = str_replace($search, $replace, $file_content);
}
file_put_contents($file_path, $file_content);
}
$this->output->writeln('更新代码完成');
$this->output->writeln('请注意查看您的代码变动');
}
/**
* 扫描js文件.
*
* @return array
*/
public function scanJsFile()
{
$js_file_list = [];
$app_path = App::getRootPath() . '/app';
$this->scanDir($app_path, $js_file_list);
return $js_file_list;
}
/**
* 扫描目录.
*
* @param string $dir
* @param array $js_file_list
* @return void
*/
public function scanDir($dir, &$js_file_list)
{
$dir_list = scandir($dir);
foreach ($dir_list as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$file_path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($file_path)) {
$this->scanDir($file_path, $js_file_list);
} else {
$file_ext = pathinfo($file_path, PATHINFO_EXTENSION);
if ($file_ext == 'js') {
$js_file_list[] = $file_path;
}
}
}
}
}

View File

@@ -23,4 +23,11 @@ return [
'本次更新修改了database/migrations/20220419030557_system_auth.php文件修复了安装到sqlite的问题如果你使用sqlite需要有意识的解决这个问题',
],
],
[
'version' => 'v2.0.74',
'desc' => [
'本次更新修改了js中init的各项属性的大小写规范你需要将蛇形命名全局替换为小驼峰命名比如:table_elem改为tableElem',
'可以运行 php think admin:update:code --update-version v2.0.74 命令自动替换',
],
],
];

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace base\common\command\admin;
use app\admin\service\AdminUpdateCodeService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class UpdateCodeBase extends Command
{
protected function configure()
{
// 指令配置
$this->setName('admin:update:code')
->addOption('update-version', null, Option::VALUE_REQUIRED, '按指定版本的规则更新现有代码')
->setDescription('the admin:update:code command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$update_version = $input->getOption('update-version');
if(is_null($update_version)){
$output->error('请指定更新的版本号');
return;
}
$update_service = new AdminUpdateCodeService();
$update_service->input = $input;
$update_service->output = $output;
$update_service->update($update_version);
}
}

View File

@@ -309,7 +309,7 @@ function event_response($name, $params = [])
/**
* 以扩展的架构定位app下的文件位置.
*
* @param string $file_path 文件路径,不要以/开头不需要以app开头会自动定位appextend/base。
* @param string $file_path 文件路径,不要以/开头不需要以app开头会自动定位 appextend/base。
* @return string
*/
function app_file_path($file_path)