Use PatchException instead of ExecutionException for patchFile

This commit is contained in:
crazywhalecc
2025-08-15 12:04:06 +08:00
parent a3ae1eb2f0
commit d0412a88df

View File

@@ -8,6 +8,7 @@ use SPC\builder\BuilderBase;
use SPC\builder\linux\SystemUtil; use SPC\builder\linux\SystemUtil;
use SPC\builder\unix\UnixBuilderBase; use SPC\builder\unix\UnixBuilderBase;
use SPC\builder\windows\WindowsBuilder; use SPC\builder\windows\WindowsBuilder;
use SPC\exception\ExecutionException;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\PatchException; use SPC\exception\PatchException;
use SPC\util\SPCTarget; use SPC\util\SPCTarget;
@@ -190,46 +191,51 @@ class SourcePatcher
*/ */
public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool
{ {
if (FileSystem::isRelativePath($patch_name)) { try {
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}"; if (FileSystem::isRelativePath($patch_name)) {
} else { $patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}";
$patch_file = $patch_name; } else {
} $patch_file = $patch_name;
if (!file_exists($patch_file)) { }
return false; if (!file_exists($patch_file)) {
} return false;
}
$patch_str = FileSystem::convertPath($patch_file); $patch_str = FileSystem::convertPath($patch_file);
if (!file_exists($patch_str)) { if (!file_exists($patch_str)) {
throw new PatchException($patch_name, "Patch file [{$patch_str}] does not exist"); throw new PatchException($patch_name, "Patch file [{$patch_str}] does not exist");
} }
// Copy patch from phar // Copy patch from phar
if (str_starts_with($patch_str, 'phar://')) { if (str_starts_with($patch_str, 'phar://')) {
$filename = pathinfo($patch_file, PATHINFO_BASENAME); $filename = pathinfo($patch_file, PATHINFO_BASENAME);
file_put_contents(SOURCE_PATH . "/{$filename}", file_get_contents($patch_file)); file_put_contents(SOURCE_PATH . "/{$filename}", file_get_contents($patch_file));
$patch_str = FileSystem::convertPath(SOURCE_PATH . "/{$filename}"); $patch_str = FileSystem::convertPath(SOURCE_PATH . "/{$filename}");
} }
// detect // detect
$detect_reverse = !$reverse; $detect_reverse = !$reverse;
$detect_cmd = 'cd ' . escapeshellarg($cwd) . ' && ' $detect_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str) . (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
. ' | patch --dry-run -p1 -s -f ' . ($detect_reverse ? '-R' : '') . ' | patch --dry-run -p1 -s -f ' . ($detect_reverse ? '-R' : '')
. ' > ' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1'; . ' > ' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1';
exec($detect_cmd, $output, $detect_status); exec($detect_cmd, $output, $detect_status);
if ($detect_status === 0) { if ($detect_status === 0) {
return true;
}
// apply patch
$apply_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
. ' | patch -p1 ' . ($reverse ? '-R' : '');
f_passthru($apply_cmd);
return true; return true;
} catch (ExecutionException $e) {
// If patch failed, throw exception
throw new PatchException($patch_name, "Patch file [{$patch_name}] failed to apply", previous: $e);
} }
// apply patch
$apply_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
. ' | patch -p1 ' . ($reverse ? '-R' : '');
f_passthru($apply_cmd);
return true;
} }
public static function patchOpenssl11Darwin(): bool public static function patchOpenssl11Darwin(): bool