-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from tighten/tm/version-sorting
Fix Images Version Sorting
- Loading branch information
Showing
12 changed files
with
123 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Shell; | ||
|
||
use Composer\Semver\Comparator; | ||
|
||
class VersionComparator | ||
{ | ||
public function __invoke(string $a, string $b): int | ||
{ | ||
// Bump "a" if it is "latest"... | ||
if ($a === 'latest') { | ||
return -1; | ||
} | ||
|
||
// Bump "b" if it is "latest"... | ||
if ($b === 'latest') { | ||
return 1; | ||
} | ||
|
||
if ($this->startsAsSemver($a) && ! $this->startsAsSemver($b)) { | ||
return -1; | ||
} | ||
|
||
if ($this->startsAsSemver($b) && ! $this->startsAsSemver($a)) { | ||
return 1; | ||
} | ||
|
||
if ($this->stableSemver($a) && ! $this->stableSemver($b)) { | ||
return -1; | ||
} | ||
|
||
if ($this->stableSemver($b) && ! $this->stableSemver($a)) { | ||
return 1; | ||
} | ||
|
||
return Comparator::greaterThan(preg_replace('/^v/', '', $a), preg_replace('/^v/', '', $b)) ? -1 : 1; | ||
} | ||
|
||
private function stableSemver(string $version): bool | ||
{ | ||
return preg_match('/^v?[\d.]+$/', $version); | ||
} | ||
|
||
private function startsAsSemver(string $version): bool | ||
{ | ||
return preg_match('/^v?[\d.]+/', $version); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Shell\VersionComparator; | ||
use Tests\TestCase; | ||
|
||
class VersionComparatorTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @testWith [["latest", "8.0"], ["latest", "8.0"]] | ||
* [["8.0", "latest"], ["latest", "8.0"]] | ||
* [["5.0", "8.0", "latest"], ["latest", "8.0", "5.0"]] | ||
* [["8.0.43", "8.0", "latest"], ["latest", "8.0.43", "8.0"]] | ||
* [["8.0.4-oraclelinux8", "8.0.4", "latest"], ["latest", "8.0.4", "8.0.4-oraclelinux8"]] | ||
* [["8.0.4-oraclelinux8", "5.0.1", "8.0.4", "latest", "5.0.1-oraclelinux8"], ["latest", "8.0.4", "5.0.1", "8.0.4-oraclelinux8", "5.0.1-oraclelinux8"]] | ||
*/ | ||
public function compares($versions, $expectedOrder) | ||
{ | ||
$this->assertEquals($expectedOrder, $this->sort($versions)); | ||
} | ||
|
||
private function sort(array $versions): array | ||
{ | ||
return collect($versions) | ||
->sort(new VersionComparator) | ||
->values() | ||
->all(); | ||
} | ||
} |