Skip to content

Commit d0412a8

Browse files
committed
Use PatchException instead of ExecutionException for patchFile
1 parent a3ae1eb commit d0412a8

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

src/SPC/store/SourcePatcher.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SPC\builder\linux\SystemUtil;
99
use SPC\builder\unix\UnixBuilderBase;
1010
use SPC\builder\windows\WindowsBuilder;
11+
use SPC\exception\ExecutionException;
1112
use SPC\exception\FileSystemException;
1213
use SPC\exception\PatchException;
1314
use SPC\util\SPCTarget;
@@ -190,46 +191,51 @@ public static function patchMicro(?array $items = null): bool
190191
*/
191192
public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool
192193
{
193-
if (FileSystem::isRelativePath($patch_name)) {
194-
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}";
195-
} else {
196-
$patch_file = $patch_name;
197-
}
198-
if (!file_exists($patch_file)) {
199-
return false;
200-
}
194+
try {
195+
if (FileSystem::isRelativePath($patch_name)) {
196+
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}";
197+
} else {
198+
$patch_file = $patch_name;
199+
}
200+
if (!file_exists($patch_file)) {
201+
return false;
202+
}
201203

202-
$patch_str = FileSystem::convertPath($patch_file);
203-
if (!file_exists($patch_str)) {
204-
throw new PatchException($patch_name, "Patch file [{$patch_str}] does not exist");
205-
}
204+
$patch_str = FileSystem::convertPath($patch_file);
205+
if (!file_exists($patch_str)) {
206+
throw new PatchException($patch_name, "Patch file [{$patch_str}] does not exist");
207+
}
206208

207-
// Copy patch from phar
208-
if (str_starts_with($patch_str, 'phar://')) {
209-
$filename = pathinfo($patch_file, PATHINFO_BASENAME);
210-
file_put_contents(SOURCE_PATH . "/{$filename}", file_get_contents($patch_file));
211-
$patch_str = FileSystem::convertPath(SOURCE_PATH . "/{$filename}");
212-
}
209+
// Copy patch from phar
210+
if (str_starts_with($patch_str, 'phar://')) {
211+
$filename = pathinfo($patch_file, PATHINFO_BASENAME);
212+
file_put_contents(SOURCE_PATH . "/{$filename}", file_get_contents($patch_file));
213+
$patch_str = FileSystem::convertPath(SOURCE_PATH . "/{$filename}");
214+
}
213215

214-
// detect
215-
$detect_reverse = !$reverse;
216-
$detect_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
217-
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
218-
. ' | patch --dry-run -p1 -s -f ' . ($detect_reverse ? '-R' : '')
219-
. ' > ' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1';
220-
exec($detect_cmd, $output, $detect_status);
216+
// detect
217+
$detect_reverse = !$reverse;
218+
$detect_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
219+
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
220+
. ' | patch --dry-run -p1 -s -f ' . ($detect_reverse ? '-R' : '')
221+
. ' > ' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1';
222+
exec($detect_cmd, $output, $detect_status);
221223

222-
if ($detect_status === 0) {
223-
return true;
224-
}
224+
if ($detect_status === 0) {
225+
return true;
226+
}
225227

226-
// apply patch
227-
$apply_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
228-
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
229-
. ' | patch -p1 ' . ($reverse ? '-R' : '');
228+
// apply patch
229+
$apply_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
230+
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
231+
. ' | patch -p1 ' . ($reverse ? '-R' : '');
230232

231-
f_passthru($apply_cmd);
232-
return true;
233+
f_passthru($apply_cmd);
234+
return true;
235+
} catch (ExecutionException $e) {
236+
// If patch failed, throw exception
237+
throw new PatchException($patch_name, "Patch file [{$patch_name}] failed to apply", previous: $e);
238+
}
233239
}
234240

235241
public static function patchOpenssl11Darwin(): bool

0 commit comments

Comments
 (0)