From 4665b7376be87719fb5b3e1ab9211e1c88dab880 Mon Sep 17 00:00:00 2001 From: Luca Bognolo <11542801+BoGnY@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:43:57 +0200 Subject: [PATCH 1/2] Fix directory separation problems on Windows machine --- src/Differ/PatchDiffer.php | 2 +- src/FileSystem/PathResolver.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Differ/PatchDiffer.php b/src/Differ/PatchDiffer.php index 204295e9..33ecb213 100644 --- a/src/Differ/PatchDiffer.php +++ b/src/Differ/PatchDiffer.php @@ -50,7 +50,7 @@ public function diff(OldAndNewFile $oldAndNewFile): string private function resolveRelativeFilePath(string $beforeFilePath): string { - $match = Strings::match($beforeFilePath, self::LOCAL_PATH_REGEX); + $match = Strings::match(str_replace('\\', '/', $beforeFilePath), self::LOCAL_PATH_REGEX); if (! isset($match['local_path'])) { throw new ShouldNotHappenException(); diff --git a/src/FileSystem/PathResolver.php b/src/FileSystem/PathResolver.php index 4a202f19..46fa5e20 100644 --- a/src/FileSystem/PathResolver.php +++ b/src/FileSystem/PathResolver.php @@ -18,7 +18,7 @@ final class PathResolver public static function resolveVendorDirectory(string $filePath): string { - $match = Strings::match($filePath, self::VENDOR_PACKAGE_DIRECTORY_REGEX); + $match = Strings::match(str_replace('\\', '/', $filePath), self::VENDOR_PACKAGE_DIRECTORY_REGEX); if (! isset($match['vendor_package_directory'])) { throw new ShouldNotHappenException('Could not resolve vendor package directory'); } From b8d04520c45b5c572bb56b9c04be9ae9923496e6 Mon Sep 17 00:00:00 2001 From: Luca Bognolo <11542801+BoGnY@users.noreply.github.com> Date: Thu, 28 Sep 2023 18:18:59 +0200 Subject: [PATCH 2/2] Extracted method for backslashes replacement --- src/Differ/PatchDiffer.php | 3 ++- src/FileSystem/PathResolver.php | 3 ++- src/Utils/FileSystemHelper.php | 16 ++++++++++++++++ tests/PatchFileFactory/PatchFileFactoryTest.php | 3 ++- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/Utils/FileSystemHelper.php diff --git a/src/Differ/PatchDiffer.php b/src/Differ/PatchDiffer.php index 33ecb213..21d5ab6b 100644 --- a/src/Differ/PatchDiffer.php +++ b/src/Differ/PatchDiffer.php @@ -7,6 +7,7 @@ use Nette\Utils\Strings; use SebastianBergmann\Diff\Differ; use Symplify\VendorPatches\Exception\ShouldNotHappenException; +use Symplify\VendorPatches\Utils\FileSystemHelper; use Symplify\VendorPatches\ValueObject\OldAndNewFile; /** @@ -50,7 +51,7 @@ public function diff(OldAndNewFile $oldAndNewFile): string private function resolveRelativeFilePath(string $beforeFilePath): string { - $match = Strings::match(str_replace('\\', '/', $beforeFilePath), self::LOCAL_PATH_REGEX); + $match = Strings::match(FileSystemHelper::normalizePath($beforeFilePath), self::LOCAL_PATH_REGEX); if (! isset($match['local_path'])) { throw new ShouldNotHappenException(); diff --git a/src/FileSystem/PathResolver.php b/src/FileSystem/PathResolver.php index 46fa5e20..f4de6682 100644 --- a/src/FileSystem/PathResolver.php +++ b/src/FileSystem/PathResolver.php @@ -6,6 +6,7 @@ use Nette\Utils\Strings; use Symplify\VendorPatches\Exception\ShouldNotHappenException; +use Symplify\VendorPatches\Utils\FileSystemHelper; use Webmozart\Assert\Assert; final class PathResolver @@ -18,7 +19,7 @@ final class PathResolver public static function resolveVendorDirectory(string $filePath): string { - $match = Strings::match(str_replace('\\', '/', $filePath), self::VENDOR_PACKAGE_DIRECTORY_REGEX); + $match = Strings::match(FileSystemHelper::normalizePath($filePath), self::VENDOR_PACKAGE_DIRECTORY_REGEX); if (! isset($match['vendor_package_directory'])) { throw new ShouldNotHappenException('Could not resolve vendor package directory'); } diff --git a/src/Utils/FileSystemHelper.php b/src/Utils/FileSystemHelper.php new file mode 100644 index 00000000..9db329c1 --- /dev/null +++ b/src/Utils/FileSystemHelper.php @@ -0,0 +1,16 @@ +createPatchFilePath($oldAndNewFile, __DIR__ . '/Fixture'); - $this->assertSame('patches/some-new-file-php.patch', $pathFilePath); + $this->assertSame('patches/some-new-file-php.patch', FileSystemHelper::normalizePath($pathFilePath)); } }