Skip to content

Commit 78ef716

Browse files
authored
feat: Consistently sort files for more deterministic builds (#1534)
1 parent ce28486 commit 78ef716

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/Box.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace KevinGH\Box;
1616

1717
use Amp\Parallel\Worker\TaskFailureThrowable;
18+
use ArrayIterator;
1819
use BadMethodCallException;
1920
use Countable;
2021
use DateTimeImmutable;
@@ -35,6 +36,7 @@
3536
use RuntimeException;
3637
use Seld\PharUtils\Timestamps;
3738
use SplFileInfo;
39+
use Symfony\Component\Finder\Finder;
3840
use Webmozart\Assert\Assert;
3941
use function array_map;
4042
use function array_unshift;
@@ -137,7 +139,11 @@ public function endBuffering(?callable $dumpAutoload): void
137139
}
138140

139141
try {
142+
$files = [];
143+
140144
foreach ($this->bufferedFiles as $file) {
145+
$files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath();
146+
141147
FS::dumpFile(
142148
$file->getPath(),
143149
$file->getContents(),
@@ -154,7 +160,22 @@ public function endBuffering(?callable $dumpAutoload): void
154160

155161
chdir($cwd);
156162

157-
$this->phar->buildFromDirectory($tmp);
163+
$unknownFiles = Finder::create()
164+
->files()
165+
->in($tmp)
166+
->notPath(array_keys($files))
167+
->sortByName();
168+
169+
$files = [...$files, ...$unknownFiles];
170+
171+
uasort($files, static function (SplFileInfo|string $a, SplFileInfo|string $b) {
172+
$a = is_string($a) ? $a : $a->getPath();
173+
$b = is_string($b) ? $b : $b->getPath();
174+
175+
return strcmp($a, $b);
176+
});
177+
178+
$this->phar->buildFromIterator(new ArrayIterator($files), $tmp);
158179
} finally {
159180
FS::remove($tmp);
160181
}

0 commit comments

Comments
 (0)