add SourcePatcher::patchFile

This commit is contained in:
crazywhalecc 2023-10-14 11:18:02 +08:00 committed by Jerry Ma
parent 56caef78e3
commit 630c0d3a5d

View File

@ -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
*/