From 630c0d3a5da9733e030d83a87e2bccca791d0285 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 14 Oct 2023 11:18:02 +0800 Subject: [PATCH] add SourcePatcher::patchFile --- src/SPC/store/SourcePatcher.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/SPC/store/SourcePatcher.php b/src/SPC/store/SourcePatcher.php index 1bbe0476..b0edc303 100644 --- a/src/SPC/store/SourcePatcher.php +++ b/src/SPC/store/SourcePatcher.php @@ -117,6 +117,30 @@ class SourcePatcher return true; } + /** + * Use existing patch file for patching + * + * @param string $patch_name Patch file name in src/globals/patch/ + * @param string $cwd Working directory for patch command + * @param bool $reverse Reverse patches (default: False) + * @throws RuntimeException + */ + public static function patchFile(string $patch_name, string $cwd, bool $reverse = false): bool + { + if (!file_exists(ROOT_DIR . "/src/globals/patch/{$patch_name}")) { + return false; + } + + $patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}"; + $patch_str = str_replace('/', DIRECTORY_SEPARATOR, $patch_file); + + f_passthru( + 'cd ' . $cwd . ' && ' . + (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . $patch_str . ' | patch -p1 ' . ($reverse ? '-R' : '') + ); + return true; + } + /** * @throws FileSystemException */