Skip to content

Commit cca1d44

Browse files
committed
Sort versions in output
Otherwise they are ordered by provider source
1 parent 831a02a commit cca1d44

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/JsonRepositoryWriter.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44

55
namespace Phpcq\RepositoryBuilder;
66

7+
use Composer\Semver\Comparator;
78
use Phpcq\RepositoryDefinition\AbstractHash;
89
use Phpcq\RepositoryDefinition\Plugin\PhpFilePluginVersion;
910
use Phpcq\RepositoryDefinition\Plugin\PluginInterface;
1011
use Phpcq\RepositoryDefinition\Plugin\PluginRequirements;
1112
use Phpcq\RepositoryDefinition\Tool\ToolInterface;
1213
use Phpcq\RepositoryDefinition\Tool\ToolRequirements;
14+
use Phpcq\RepositoryDefinition\Tool\ToolVersionInterface;
1315
use Phpcq\RepositoryDefinition\VersionRequirementList;
1416
use stdClass;
1517
use Symfony\Component\Filesystem\Filesystem;
1618

1719
use function array_flip;
1820
use function array_key_exists;
21+
use function array_values;
1922
use function glob;
2023
use function hash_file;
2124
use function is_array;
2225
use function is_dir;
2326
use function is_string;
2427
use function json_encode;
2528
use function sprintf;
29+
use function usort;
2630

2731
use const JSON_UNESCAPED_SLASHES;
2832
use const JSON_UNESCAPED_UNICODE;
@@ -139,7 +143,8 @@ private function processTool(ToolInterface $tool): ?array
139143
$data = [
140144
'tools' => [],
141145
];
142-
foreach ($tool as $version) {
146+
147+
foreach ($this->getToolVersions($tool) as $version) {
143148
if (!isset($data['tools'][$name = $tool->getName()])) {
144149
$data['tools'][$name] = [];
145150
}
@@ -296,4 +301,21 @@ private function dumpFile(string $fileName, array $contents): void
296301
assert(is_string($encoded));
297302
$this->filesystem->dumpFile($fullFileName, $encoded);
298303
}
304+
305+
/** @return list<ToolVersionInterface> */
306+
private function getToolVersions(ToolInterface $tool): array
307+
{
308+
/** @var array<string, ToolVersionInterface> $versions */
309+
$versions = [];
310+
foreach ($tool as $version) {
311+
$versions[] = $version;
312+
}
313+
usort(
314+
$versions,
315+
static fn(ToolVersionInterface $first, ToolVersionInterface $second): int
316+
=> Comparator::greaterThanOrEqualTo($first->getVersion(), $second->getVersion()) ? 1 : -1
317+
);
318+
319+
return $versions;
320+
}
299321
}

0 commit comments

Comments
 (0)