Closed DRSDavidSoft closed 1 month ago
TIL slash comes before letters in ascii and backslash comes after... Ok let me think of how to fix this
So weird, TIL this also! 😄
The Finder
component docs apparently doesn't have a built-in sorter to deal with this, they sadly suggest using something like this:
$filesToArchive->sort(function (\SplFileInfo $a, \SplFileInfo $b): int {
return strcmp($a->getRealPath(), $b->getRealPath());
});
instead of:
$finder->sortByName();
Alternatively, you can use DIRECTORY_SEPARATOR
instead of using hard-coded /
:
->in([__DIR__.DIRECTORY_SEPARATOR.'src', __DIR__.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'compiled'])
->append([
__DIR__.DIRECTORY_SEPARATOR.'init_phar.php',
__DIR__.DIRECTORY_SEPARATOR.'init.php',
__DIR__.DIRECTORY_SEPARATOR.'init_helpers.php',
])
instead of:
->in([__DIR__.'/src', __DIR__.'/resources/compiled'])
->append([
__DIR__.'/init_phar.php',
__DIR__.'/init.php',
__DIR__.'/init_helpers.php',
])
But this will look ugly and hard to read. So maybe you can wrap the __DIR__.'/some/path
statements in a realpath()
call.
I personally feel like apologizing for the /
vs \
inconsistency in Windows systems. 😔
Alternatively, you can use DIRECTORY_SEPARATOR
That's not the problem. We're telling the finder to grab all the files in src and the finder is giving them backslashes. Actually the finder is weird to begin with since those paths you echoed have a mixture of slashes and backslashes in a single file.
I don't think getRealPath
would work because even if it's all backslashes on windows you'll still end up with a different ordering from linux where it produces slashes. I think we're going to have to replace the backslashes in the paths for a consistent sort. Let me whip up a prototype and you can try it out
@DRSDavidSoft Can you try this?
diff --git a/build.php b/build.php
index b4c941f8..0436bdca 100644
--- a/build.php
+++ b/build.php
@@ -60,6 +60,14 @@ $filesToArchive = Finder::create()
])
->sortByName();
+if (KINT_WIN) {
+ $filesToArchive->sort(static function (SplFileInfo $a, SplFileInfo $b) {
+ $a = strtr($a->getRealPath() ?: $a->getPathname(), '\\', '/');
+ $b = strtr($b->getRealPath() ?: $b->getPathname(), '\\', '/');
+ return strcmp($a, $b);
+ });
+}
+
foreach ($filesToArchive as $file) {
$local = \substr((string) $file, $pathlen);
$phar->addFile((string) $file, $local);
This definitely fixes it
C:\Users\David\Desktop\kint>sha1sum build\kint.phar
\d1a8d851ad71f2ba207809af7e666061c91cf210 *build\\kint.phar
C:\Users\David\Desktop\kint>composer clean
C:\Users\David\Desktop\kint>composer build
....
C:\Users\David\Desktop\kint>sha1sum build\kint.phar
\d1a8d851ad71f2ba207809af7e666061c91cf210 *build\\kint.phar
Notice the first file is the one already built correctly.
Great!
Here's the output: