调整源码对比方式;调整更新逻辑

This commit is contained in:
2023-10-18 15:19:05 +08:00
parent b83e5aa656
commit 8f1a749b8d
20 changed files with 372 additions and 373 deletions

View File

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

View File

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

View File

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

View File

@@ -31,7 +31,6 @@
"overtrue/flysystem-cos": "^5.0",
"iidestiny/flysystem-oss": "^4.0",
"czproject/git-php": "^4.2",
"localheinz/diff": "^1.1",
"psr/simple-cache": ">=1.0"
},
"require-dev": {

62
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9ef28b8bf889c61ae5c86815724ebffd",
"content-hash": "f2aaff931d3892b72c0c06b7e9a023c7",
"packages": [
{
"name": "aliyuncs/oss-sdk-php",
@@ -952,66 +952,6 @@
],
"time": "2023-08-05T12:09:49+00:00"
},
{
"name": "localheinz/diff",
"version": "1.1.1",
"source": {
"type": "git",
"url": "https://github.com/localheinz/diff.git",
"reference": "851bb20ea8358c86f677f5f111c4ab031b1c764c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/localheinz/diff/zipball/851bb20ea8358c86f677f5f111c4ab031b1c764c",
"reference": "851bb20ea8358c86f677f5f111c4ab031b1c764c",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.0",
"symfony/process": "^4.2 || ^5"
},
"type": "library",
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
},
{
"name": "Kore Nordmann",
"email": "mail@kore-nordmann.de"
}
],
"description": "Fork of sebastian/diff for use with ergebnis/composer-normalize",
"homepage": "https://github.com/localheinz/diff",
"keywords": [
"diff",
"udiff",
"unidiff",
"unified diff"
],
"support": {
"source": "https://github.com/localheinz/diff/tree/main"
},
"funding": [
{
"url": "https://github.com/sebastianbergmann",
"type": "github"
}
],
"time": "2020-07-06T04:49:32+00:00"
},
{
"name": "maennchen/zipstream-php",
"version": "2.4.0",

View File

@@ -14,7 +14,7 @@ use app\admin\model\SystemNode;
use app\admin\model\SystemQuick;
use app\common\constants\AdminConstant;
class InitAdminServiceBase
class AdminInitServiceBase
{
protected $output = null;
@@ -142,6 +142,6 @@ class InitAdminServiceBase
protected function requireData($table_name)
{
return include __DIR__ . '/initAdminData/' . $table_name . '.php';
return include __DIR__ . '/adminInitData/' . $table_name . '.php';
}
}

View File

@@ -0,0 +1,319 @@
<?php
namespace base\admin\service;
use app\common\command\admin\Version;
use app\common\tools\PathTools;
use CzProject\GitPhp\Git;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\StorageAttributes;
use think\facade\App;
use think\facade\Env;
class AdminUpdateServiceBase
{
public const REPO = 'https://gitee.com/ulthon/ulthon_admin.git';
public $input;
public $output;
public function __construct()
{
}
public function update()
{
$output = $this->output;
$input = $this->input;
$this->cleanWorkpaceDir();
$current_version = Version::VERSION;
$current_version_dir = App::getRuntimePath() . '/update/' . $current_version;
$last_version_dir = App::getRuntimePath() . '/update/last';
$version_file_regx = "/\bconst VERSION\s*=\s*'[\d\.a-z]+'/";
$output->writeln('获取最新代码');
$last_version_git = new Git();
$last_version_repo = $last_version_git->cloneRepository(self::REPO, $last_version_dir);
$tags = $last_version_repo->getTags();
$update_level = Env::get('adminsystem.update_level', 'production');
if ($update_level == 'production') {
$tags = array_filter($tags, function ($value) {
if (strpos($value, '-')) {
return false;
}
return true;
});
}
usort($tags, function ($a, $b) {
return version_compare($a, $b);
});
$last_version = $tags[count($tags) - 1];
if ($last_version == $current_version) {
$output->writeln('当前版本为最新版本');
$this->cleanWorkpaceDir();
if ($input->hasOption('reinstall')) {
$output->writeln('重装代码');
} else {
return;
}
}
// 将最新代码切换到最新版本,因为最新的提交可能没有发布版本
$output->writeln('切换最新代码的最新版本');
$last_version_repo->checkout($last_version);
$current_version_git = new Git();
$output->writeln('获取当前版本代码');
$current_version_repo = $current_version_git->cloneRepository(self::REPO, $current_version_dir);
$output->writeln('切换版本' . $current_version);
$current_version_repo->checkout($current_version);
// 获取当前版本需要跳过的文件
$current_version_update_config = include $current_version_dir . '/config/update.php';
// 获取当前版本要替换的文件
$current_version_filesystem = new Filesystem(new LocalFilesystemAdapter($current_version_dir));
$current_version_list_files = $current_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($current_version_update_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
$skip_files = $current_version_update_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return false;
}
$skip_dir = $current_version_update_config['skip_dir'] ?? [];
$skip_dir[] = '.git';
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return false;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
// 对比现在的代码,检查是否有定制修改
$output->writeln('对比源码是否被定制');
$now_dir = App::getRootPath();
/**
* @var array<bool|string>
*/
$changed_files = [];
foreach ($current_version_list_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$current_version_file_path = $current_version_dir . '/' . $file_path;
$compare_result = PathTools::compareFiles($now_file_path, $current_version_file_path, true);
if (!$compare_result) {
$changed_files[$file_path] = $compare_result;
}
}
// 有定制修改则退出
if (!empty($changed_files)) {
$output->warning('无法自动更新,以下文件被定制,请还原或手动升级:');
foreach ($changed_files as $file_path => $compare_result) {
$output->warning($file_path);
if (is_string($compare_result)) {
$output->writeln($compare_result);
}
}
$file_count = count($changed_files);
$ask_result = $output->ask($input, "发现{$file_count}个文件被修改,如果您认为以上文件可以被覆盖或为检测错误,可以强制覆盖。\n是否强制继续更新?(y/n)");
if (!$ask_result) {
return;
}
}
// 获取最新版本需要跳过的文件
$last_version_skip_config = include $last_version_dir . '/config/update.php';
// 获取最新版本要替换的文件
$last_version_filesystem = new Filesystem(new LocalFilesystemAdapter($last_version_dir));
$last_version_list_files = $last_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($last_version_skip_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
$skip_files = $last_version_skip_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return false;
}
$skip_dir = $last_version_skip_config['skip_dir'] ?? [];
$skip_dir[] = '.git';
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return false;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
$delete_files = array_diff($current_version_list_files, $last_version_list_files);
foreach ($delete_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
unlink($now_file_path);
}
foreach ($last_version_list_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$last_file_path = $last_version_dir . '/' . $file_path;
$file_content = file_get_contents($last_file_path);
PathTools::intiDir($now_file_path);
file_put_contents($now_file_path, $file_content);
}
// 处理append的文件
$last_version_list_skip_files = $last_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($last_version_skip_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
if (str_starts_with($path, '.git')) {
return false;
}
$skip_files = $last_version_skip_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return true;
}
$skip_dir = $last_version_skip_config['skip_dir'] ?? [];
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return true;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
$last_version_list_append_files = [];
foreach ($last_version_list_skip_files as $file_path) {
if (in_array($file_path, $last_version_skip_config['append_files'])) {
$last_version_list_append_files[] = $file_path;
continue;
}
foreach ($last_version_skip_config['append_dir'] as $dir) {
if (str_starts_with($file_path, $dir)) {
$last_version_list_append_files[] = $file_path;
continue;
}
}
}
foreach ($last_version_list_append_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$last_file_path = $last_version_dir . '/' . $file_path;
if (file_exists($now_file_path)) {
continue;
}
$file_content = file_get_contents($last_file_path);
PathTools::intiDir($now_file_path);
file_put_contents($now_file_path, $file_content);
}
// 检测now的composer依赖和最新的composer依赖
$last_composer_json = file_get_contents($last_version_dir . '/composer.json');
$output->writeln($last_composer_json);
$output->writeln('请参考以上最新composer文件调整您的依赖');
// 分析出最新需要的但now没有的
// 为用户整理出要手动调整的composer命令
$this->cleanWorkpaceDir();
$output->writeln('更新完成');
// 更新完成
$update_tips = include $last_version_dir . '/config/tips.php';
// 按照版本号排序
usort($update_tips, function ($a, $b) {
return version_compare($a['version'], $b['version']);
});
foreach ($update_tips as $tips_item) {
if(version_compare($tips_item['version'], $current_version) <= 0) {
continue;
}
$output->writeln('版本' . $tips_item['version'] . '更新说明:');
$output->writeln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
foreach ($tips_item['desc'] as $desc) {
$output->writeln($desc);
}
}
}
protected function cleanWorkpaceDir()
{
$dir = App::getRuntimePath() . '/update/';
$this->output->writeln('清理目录 ' . $dir);
PathTools::removeDir($dir);
}
}

View File

@@ -0,0 +1,13 @@
<?php
return [
[
'version' => 'v2.0.57',
'desc' => [
'删除了localheinz/diff依赖可以通过以下命令调整',
'composer remove localheinz/diff',
'',
'调整了AdminUpdateServiceBase的代码的文件名和类名可能需要手动修改或删除旧的文件',
],
],
];

View File

@@ -4,23 +4,14 @@ declare(strict_types=1);
namespace base\common\command\admin;
use app\common\command\admin\Version;
use app\common\tools\PathTools;
use CzProject\GitPhp\Git;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\StorageAttributes;
use app\admin\service\AdminUpdateService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\App;
use think\facade\Env;
class UpdateBase extends Command
{
public const REPO = 'https://gitee.com/ulthon/ulthon_admin.git';
protected function configure()
{
// 指令配置
@@ -34,280 +25,9 @@ class UpdateBase extends Command
// 指令输出
$output->writeln('admin:update');
$this->cleanWorkpaceDir();
$current_version = Version::VERSION;
$current_version_dir = App::getRuntimePath() . '/update/' . $current_version;
$last_version_dir = App::getRuntimePath() . '/update/last';
$version_file_regx = "/\bconst VERSION\s*=\s*'[\d\.a-z]+'/";
$output->writeln('获取最新代码');
$last_version_git = new Git();
$last_version_repo = $last_version_git->cloneRepository(self::REPO, $last_version_dir);
$tags = $last_version_repo->getTags();
$update_level = Env::get('adminsystem.update_level', 'production');
if ($update_level == 'production') {
$tags = array_filter($tags, function ($value) {
if (strpos($value, '-')) {
return false;
}
return true;
});
}
usort($tags, function ($a, $b) {
return version_compare($a, $b);
});
$last_version = $tags[count($tags) - 1];
if ($last_version == $current_version) {
$output->writeln('当前版本为最新版本');
$this->cleanWorkpaceDir();
if ($input->hasOption('reinstall')) {
$output->writeln('重装代码');
} else {
return;
}
}
// 将最新代码切换到最新版本,因为最新的提交可能没有发布版本
$output->writeln('切换最新代码的最新版本');
$last_version_repo->checkout($last_version);
$current_version_git = new Git();
$output->writeln('获取当前版本代码');
$current_version_repo = $current_version_git->cloneRepository(self::REPO, $current_version_dir);
$output->writeln('切换版本' . $current_version);
$current_version_repo->checkout($current_version);
// 获取当前版本需要跳过的文件
$current_version_update_config = include $current_version_dir . '/config/update.php';
// 获取当前版本要替换的文件
$current_version_filesystem = new Filesystem(new LocalFilesystemAdapter($current_version_dir));
$current_version_list_files = $current_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($current_version_update_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
$skip_files = $current_version_update_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return false;
}
$skip_dir = $current_version_update_config['skip_dir'] ?? [];
$skip_dir[] = '.git';
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return false;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
// 对比现在的代码,检查是否有定制修改
$output->writeln('对比源码是否被定制');
$now_dir = App::getRootPath();
/**
* @var array<bool|string>
*/
$changed_files = [];
foreach ($current_version_list_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$current_version_file_path = $current_version_dir . '/' . $file_path;
$compare_result = PathTools::compareFiles($now_file_path, $current_version_file_path, true);
if (!$compare_result || is_string($compare_result)) {
$changed_files[$file_path] = $compare_result;
}
}
// 有定制修改则退出
if (!empty($changed_files)) {
$output->warning('无法自动更新,以下文件被定制,请还原或手动升级:');
foreach ($changed_files as $file_path => $compare_result) {
$output->warning($file_path);
if (is_string($compare_result)) {
$output->writeln($compare_result);
}
}
$file_count = count($changed_files);
$ask_result = $output->ask($input, "发现{$file_count}个文件被修改,如果您认为以上文件可以被覆盖或为检测错误,可以强制覆盖。\n是否强制继续更新?(y/n)");
if (!$ask_result) {
return;
}
}
// 获取最新版本需要跳过的文件
$last_version_skip_config = include $last_version_dir . '/config/update.php';
// 获取最新版本要替换的文件
$last_version_filesystem = new Filesystem(new LocalFilesystemAdapter($last_version_dir));
$last_version_list_files = $last_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($last_version_skip_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
$skip_files = $last_version_skip_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return false;
}
$skip_dir = $last_version_skip_config['skip_dir'] ?? [];
$skip_dir[] = '.git';
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return false;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
$delete_files = array_diff($current_version_list_files, $last_version_list_files);
foreach ($delete_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
unlink($now_file_path);
}
foreach ($last_version_list_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$last_file_path = $last_version_dir . '/' . $file_path;
$file_content = file_get_contents($last_file_path);
PathTools::intiDir($now_file_path);
file_put_contents($now_file_path, $file_content);
}
// 处理append的文件
$last_version_list_skip_files = $last_version_filesystem->listContents('/', Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes) use ($last_version_skip_config) {
if ($attributes->isDir()) {
return false;
}
$path = $attributes->path();
if (str_starts_with($path, '.git')) {
return false;
}
$skip_files = $last_version_skip_config['skip_files'] ?? [];
if (in_array($path, $skip_files)) {
return true;
}
$skip_dir = $last_version_skip_config['skip_dir'] ?? [];
foreach ($skip_dir as $dir) {
if (str_starts_with($path, $dir)) {
return true;
}
}
return true;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
$last_version_list_append_files = [];
foreach ($last_version_list_skip_files as $file_path) {
if (in_array($file_path, $last_version_skip_config['append_files'])) {
$last_version_list_append_files[] = $file_path;
continue;
}
foreach ($last_version_skip_config['append_dir'] as $dir) {
if (str_starts_with($file_path, $dir)) {
$last_version_list_append_files[] = $file_path;
continue;
}
}
}
foreach ($last_version_list_append_files as $file_path) {
$now_file_path = $now_dir . '/' . $file_path;
$last_file_path = $last_version_dir . '/' . $file_path;
if (file_exists($now_file_path)) {
continue;
}
$file_content = file_get_contents($last_file_path);
PathTools::intiDir($now_file_path);
file_put_contents($now_file_path, $file_content);
}
// 检测now的composer依赖和最新的composer依赖
$last_composer_json = file_get_contents($last_version_dir . '/composer.json');
$output->writeln($last_composer_json);
$output->writeln('请参考以上最新composer文件调整您的依赖');
// 分析出最新需要的但now没有的
// 为用户整理出要手动调整的composer命令
$this->cleanWorkpaceDir();
$output->writeln('更新完成');
// 更新完成
if (!empty(Version::UPDATE_TIPS)) {
foreach (Version::UPDATE_TIPS as $content) {
$output->writeln($content);
}
$output->writeln('删除对应文件之后,可以通过以下命令重新安装');
$output->writeln('php think admin:update --resinstall');
}
}
protected function cleanWorkpaceDir()
{
$dir = App::getRuntimePath() . '/update/';
$this->output->writeln('清理目录 ' . $dir);
PathTools::removeDir($dir);
$update_service = new AdminUpdateService();
$update_service->input = $input;
$update_service->output = $output;
$update_service->update();
}
}

View File

@@ -12,19 +12,15 @@ use think\console\Output;
class VersionBase extends Command
{
public const VERSION = 'v2.0.56';
public const VERSION = 'v2.0.57';
public const LAYUI_VERSION = '2.8.17';
public const COMMENT = [
'修复默认安装数据库的兼容性问题',
'调整源码对比方式',
'发布新版本',
];
public const UPDATE_TIPS = [
'',
];
protected function configure()
{
// 指令配置

View File

@@ -2,7 +2,6 @@
namespace base\common\tools;
use Localheinz\Diff\Differ;
use think\facade\App;
class PathToolsBase
@@ -105,9 +104,9 @@ class PathToolsBase
return str_replace('/', '\\', $content);
}
public static function compareFiles($a, $b, $return_diff = false):bool|string
public static function compareFiles($a, $b):bool
{
if(file_exists($a) !== file_exists($b)) {
if (file_exists($a) !== file_exists($b)) {
return false;
}
@@ -149,14 +148,18 @@ class PathToolsBase
$a_content = file_get_contents($a);
$b_content = file_get_contents($b);
$diff = new Differ();
$a_content_length = strlen($a_content);
$b_content_length = strlen($b_content);
$diff_content = $diff->diff($a_content, $b_content);
if (!empty($diff_content)) {
$result = $return_diff ? $diff_content : false;
if ($a_content_length !== $b_content_length) {
$result = false;
} else {
$result = true;
for ($i = 0; $i < $a_content_length; $i++) {
if ($a_content[$i] !== $b_content[$i]) {
$result = false;
break;
}
}
}
}
}