feat:模块安装时建立 .runtime 文件

This commit is contained in:
妙码生花 2023-10-12 21:07:31 +08:00
parent 9ff102ac5f
commit 4e73612784
2 changed files with 98 additions and 31 deletions

View File

@ -107,14 +107,13 @@ class Manage
throw new Exception('Order not found');
}
// 下载
$sysVersion = Config::get('buildadmin.version');
$installed = Server::installedList($this->installDir);
$installed = Server::installedList($this->installDir);
foreach ($installed as $item) {
$installedIds[] = $item['uid'];
}
unset($installed);
$zipFile = Server::download($this->uid, $this->installDir, [
'sysVersion' => $sysVersion,
'sysVersion' => Config::get('buildadmin.version'),
'nuxtVersion' => Server::getNuxtVersion(),
'ba-user-token' => $token,
'order_id' => $orderId,
@ -152,6 +151,7 @@ class Manage
{
$file = Filesystem::fsFit(root_path() . 'public' . $file);
if (!is_file($file)) {
// 包未找到
throw new Exception('Zip file not found');
}
@ -163,12 +163,14 @@ class Manage
$copyToDir .= DIRECTORY_SEPARATOR;
// 删除zip
@unlink($file);
@unlink($copyTo);
// 读取ini
$info = Server::getIni($copyToDir);
if (!isset($info['uid']) || !$info['uid']) {
if (empty($info['uid'])) {
Filesystem::delDir($copyToDir);
// 基本配置不完整
throw new Exception('Basic configuration of the Module is incomplete');
}
$this->uid = $info['uid'];
@ -178,18 +180,21 @@ class Manage
$info = $this->getInfo();
if ($info && isset($info['uid'])) {
Filesystem::delDir($copyToDir);
// 模块已经存在
throw new Exception('Module already exists');
}
if (!Filesystem::dirIsEmpty($this->modulesDir)) {
Filesystem::delDir($copyToDir);
// 模块目录被占
throw new Exception('The directory required by the module is occupied');
}
}
// 放置新模块
rename($copyToDir, $this->modulesDir);
// 检查是否完整
// 检查新包是否完整
$this->checkPackage();
// 设置为待安装状态
@ -322,6 +327,9 @@ class Manage
// 安装 WebBootstrap
Server::installWebBootstrap($this->uid, $this->modulesDir);
// 建立 .runtime
Server::createRuntime($this->modulesDir);
// 冲突检查
$this->conflictHandle($trigger);
@ -803,30 +811,6 @@ class Manage
return $depend;
}
public function disableDependConflictCheck(string $backupsDir): array
{
if (!$backupsDir) {
return [];
}
$dependFile = [
Filesystem::fsFit($backupsDir . DIRECTORY_SEPARATOR . 'composer.json') => [
'path' => Filesystem::fsFit(root_path() . 'composer.json'),
'type' => 'composer',
],
Filesystem::fsFit($backupsDir . DIRECTORY_SEPARATOR . 'web/package.json') => [
'path' => Filesystem::fsFit(root_path() . 'web/package.json'),
'type' => 'npm',
],
];
$conflict = [];
foreach ($dependFile as $key => $item) {
if (is_file($key) && (filesize($key) != filesize($item['path']) || md5_file($key) != md5_file($item['path']))) {
$conflict[] = $item['type'];
}
}
return $conflict;
}
/**
* 检查包是否完整
* @throws Throwable

View File

@ -115,14 +115,53 @@ class Server
return $conflict;
}
/**
* 获取模块[冲突]文件列表
* @param string $dir 模块目录
* @param bool $onlyConflict 是否只获取冲突文件
*/
public static function getFileList(string $dir, bool $onlyConflict = false): array
{
if (!is_dir($dir)) {
return [];
}
$fileList = [];
$overwriteDir = self::getOverwriteDir();
$fileList = [];
$overwriteDir = self::getOverwriteDir();
$moduleFileList = self::getRuntime($dir, 'files');
if ($moduleFileList) {
// 有冲突的文件
if ($onlyConflict) {
foreach ($moduleFileList as $file) {
if (!is_file($file['path'])) continue;
// 如果是要安装到项目的文件,从项目根目录开始,如果不是,从模块根目录开始
$path = Filesystem::fsFit(str_replace($dir, '', $file['path']));
$paths = explode(DIRECTORY_SEPARATOR, $path);
$overwriteFile = in_array($paths[0], $overwriteDir) ? root_path() . $path : $dir . $path;
if (is_file($overwriteFile) && (filesize($overwriteFile) != $file['size'] || md5_file($overwriteFile) != $file['md5'])) {
$fileList[] = $path;
}
}
} else {
// 要安装的文件
foreach ($overwriteDir as $item) {
$baseDir = $dir . $item;
if (!is_dir($baseDir)) {
continue;
}
foreach ($moduleFileList as $file) {
if (!is_file($file['path'])) continue;
if (!str_starts_with($file['path'], $baseDir)) continue;
$fileList[] = Filesystem::fsFit(str_replace($dir, '', $file['path']));
}
}
}
return $fileList;
}
foreach ($overwriteDir as $item) {
$baseDir = $dir . $item;
if (!is_dir($baseDir)) {
@ -441,6 +480,50 @@ class Server
return false;
}
/**
* 创建 .runtime
*/
public static function createRuntime(string $dir): void
{
$runtimeFilePath = $dir . '.runtime';
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY
);
$filePaths = [];
foreach ($files as $file) {
if (!$file->isDir()) {
$pathName = $file->getPathName();
if ($pathName == $runtimeFilePath) continue;
$filePaths[] = [
'path' => Filesystem::fsFit($pathName),
'size' => filesize($pathName),
'md5' => md5_file($pathName),
];
}
}
file_put_contents($runtimeFilePath, json_encode([
'files' => $filePaths
]));
}
/**
* 读取 .runtime
*/
public static function getRuntime(string $dir, string $key = ''): array
{
$runtimeFilePath = $dir . '.runtime';
$runtimeContent = @file_get_contents($runtimeFilePath);
$runtimeContentArr = json_decode($runtimeContent, true);
if (!$runtimeContentArr) return [];
if ($key) {
return $runtimeContentArr[$key] ?? [];
} else {
return $runtimeContentArr;
}
}
/**
* 获取请求对象
* @return Client