|
8 | 8 | use SPC\builder\linux\SystemUtil; |
9 | 9 | use SPC\builder\unix\UnixBuilderBase; |
10 | 10 | use SPC\builder\windows\WindowsBuilder; |
| 11 | +use SPC\exception\ExecutionException; |
11 | 12 | use SPC\exception\FileSystemException; |
12 | 13 | use SPC\exception\PatchException; |
13 | 14 | use SPC\util\SPCTarget; |
@@ -190,46 +191,51 @@ public static function patchMicro(?array $items = null): bool |
190 | 191 | */ |
191 | 192 | public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool |
192 | 193 | { |
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 | + } |
201 | 203 |
|
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 | + } |
206 | 208 |
|
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 | + } |
213 | 215 |
|
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); |
221 | 223 |
|
222 | | - if ($detect_status === 0) { |
223 | | - return true; |
224 | | - } |
| 224 | + if ($detect_status === 0) { |
| 225 | + return true; |
| 226 | + } |
225 | 227 |
|
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' : ''); |
230 | 232 |
|
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 | + } |
233 | 239 | } |
234 | 240 |
|
235 | 241 | public static function patchOpenssl11Darwin(): bool |
|
0 commit comments