Refactor lock component to a single class (#773)

This commit is contained in:
Jerry Ma
2025-06-18 14:05:43 +08:00
committed by GitHub
parent 1c439a01a1
commit ba0ea5b40a
10 changed files with 284 additions and 147 deletions

View File

@@ -17,11 +17,6 @@ class SourceManager
*/
public static function initSource(?array $sources = null, ?array $libs = null, ?array $exts = null, bool $source_only = false): void
{
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
throw new WrongUsageException('Download lock file "downloads/.lock.json" not found, maybe you need to download sources first ?');
}
$lock = json_decode(FileSystem::readFile(DOWNLOAD_PATH . '/.lock.json'), true);
$sources_extracted = [];
// source check exist
if (is_array($sources)) {
@@ -56,8 +51,8 @@ class SourceManager
}
// check source downloaded
$pre_built_name = Downloader::getPreBuiltLockName($source);
if ($source_only || !isset($lock[$pre_built_name])) {
if (!isset($lock[$source])) {
if ($source_only || LockFile::get($pre_built_name) === null) {
if (LockFile::get($source) === null) {
throw new WrongUsageException("Source [{$source}] not downloaded or not locked, you should download it first !");
}
$lock_name = $source;
@@ -65,20 +60,23 @@ class SourceManager
$lock_name = $pre_built_name;
}
$lock_content = LockFile::get($lock_name);
// check source dir exist
$check = $lock[$lock_name]['move_path'] === null ? (SOURCE_PATH . '/' . $source) : (SOURCE_PATH . '/' . $lock[$lock_name]['move_path']);
$check = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $source);
// $check = $lock[$lock_name]['move_path'] === null ? (SOURCE_PATH . '/' . $source) : (SOURCE_PATH . '/' . $lock[$lock_name]['move_path']);
if (!is_dir($check)) {
logger()->debug('Extracting source [' . $source . '] to ' . $check . ' ...');
$filename = self::getSourceFullPath($lock[$lock_name]);
FileSystem::extractSource($source, $lock[$lock_name]['source_type'], $filename, $lock[$lock_name]['move_path']);
Downloader::putLockSourceHash($lock[$lock_name], $check);
$filename = LockFile::getLockFullPath($lock_content);
FileSystem::extractSource($source, $lock_content['source_type'], $filename, $check);
LockFile::putLockSourceHash($lock_content, $check);
continue;
}
// if a lock file does not have hash, calculate with the current source (backward compatibility)
if (!isset($lock[$lock_name]['hash'])) {
$hash = Downloader::getLockSourceHash($lock[$lock_name]);
if (!isset($lock_content['hash'])) {
$hash = LockFile::getLockSourceHash($lock_content);
} else {
$hash = $lock[$lock_name]['hash'];
$hash = $lock_content['hash'];
}
// when source already extracted, detect if the extracted source hash is the same as the lock file one
@@ -90,18 +88,10 @@ class SourceManager
// if not, remove the source dir and extract again
logger()->notice("Source [{$source}] hash mismatch, removing old source dir and extracting again ...");
FileSystem::removeDir($check);
$filename = self::getSourceFullPath($lock[$lock_name]);
FileSystem::extractSource($source, $lock[$lock_name]['source_type'], $filename, $lock[$lock_name]['move_path']);
Downloader::putLockSourceHash($lock[$lock_name], $check);
$filename = LockFile::getLockFullPath($lock_content);
$move_path = LockFile::getExtractPath($lock_name, SOURCE_PATH . '/' . $source);
FileSystem::extractSource($source, $lock_content['source_type'], $filename, $move_path);
LockFile::putLockSourceHash($lock_content, $check);
}
}
private static function getSourceFullPath(array $lock_options): string
{
return match ($lock_options['source_type']) {
SPC_SOURCE_ARCHIVE => FileSystem::isRelativePath($lock_options['filename']) ? (DOWNLOAD_PATH . '/' . $lock_options['filename']) : $lock_options['filename'],
SPC_SOURCE_GIT, SPC_SOURCE_LOCAL => FileSystem::isRelativePath($lock_options['dirname']) ? (DOWNLOAD_PATH . '/' . $lock_options['dirname']) : $lock_options['dirname'],
default => throw new WrongUsageException("Unknown source type: {$lock_options['source_type']}"),
};
}
}