Skip to content

Commit

Permalink
Automatically add next versions to list
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlavtox committed Feb 5, 2024
1 parent 5f55bc3 commit 36d7818
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/Command/GenerateJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use App\ModuleCollection;
use App\Util\ModuleUtils;
use App\Util\PrestaShopUtils;
use App\Util\VersionUtils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateJsonCommand extends Command
{
private const MIN_PRESTASHOP_VERSION = '8.0.0';

protected static $defaultName = 'generateJson';

private ModuleUtils $moduleUtils;
Expand Down Expand Up @@ -156,29 +159,48 @@ private function generatePrestaShopModulesJson(
*
* @return PrestaShop[]
*/
private function addVersionsUnderDelopment(array $prestashopVersions): array
public function addVersionsUnderDelopment(array $prestashopVersions): array
{
// Add current development version so that its module's list is available, but only if it is not part of the released ones.
$developmentVersion = '9.0.0';
$nextPatchVersion = '8.1.4';
if (empty($prestashopVersions)) {
return $prestashopVersions;
}

// Check if versions are already in the list
$addDevelopmentVersion = true;
$addPatchVersion = true;
foreach ($prestashopVersions as $prestashopVersion) {
if ($prestashopVersion->getVersion() === $developmentVersion) {
$addDevelopmentVersion = false;
}
if ($prestashopVersion->getVersion() === $nextPatchVersion) {
$addPatchVersion = false;
// Let's initialize utilities to get us the highest versions
$versionUtils = new VersionUtils();
$developmentVersions = [];

// Add next major, minor and patches for the current highest version
$highestVersion = $versionUtils->getHighestStableVersionFromList($prestashopVersions);
if (!empty($highestVersion)) {
$developmentVersions[] = $highestVersion->getNextMajorVersion();
$developmentVersions[] = $highestVersion->getNextMinorVersion();
$developmentVersions[] = $highestVersion->getNextPatchVersion();
}

// Let's also add possible patches for previous major
$highestPreviousVersion = $versionUtils->getHighestStablePreviousVersionFromList($prestashopVersions);
if (!empty($highestPreviousVersion)) {
$developmentVersions[] = $highestPreviousVersion->getNextMinorVersion();
$developmentVersions[] = $highestPreviousVersion->getNextPatchVersion();
}

// Remove all development versions that are older than the min version
foreach ($developmentVersions as $k => $v) {
if (version_compare(self::MIN_PRESTASHOP_VERSION, $v, '>')) {
unset($developmentVersions[$k]);
}
}

// Now add versions if needed
if ($addPatchVersion) {
$prestashopVersions[] = new PrestaShop($nextPatchVersion);
// Remove all development versions that are already in the list, for some reason
foreach ($prestashopVersions as $prestashopVersion) {
if (in_array($prestashopVersion->getVersion(), $developmentVersions)) {
$key = array_search($prestashopVersion->getVersion(), $developmentVersions);
unset($developmentVersions[$key]);
}
}
if ($addDevelopmentVersion) {

// Add all of them to the list
foreach ($developmentVersions as $developmentVersion) {
$prestashopVersions[] = new PrestaShop($developmentVersion);
}

Expand Down
61 changes: 61 additions & 0 deletions src/Model/PrestaShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,67 @@ public function getVersion(): string
return $this->version;
}

public function getNextMajorVersion(): string
{
return implode('.', [
$this->getMajorVersionNumber() + 1,
0,
0,
]);
}

public function getNextMinorVersion(): string
{
return implode('.', [
$this->getMajorVersionNumber(),
$this->getMinorVersionNumber() + 1,
0,
]);
}

public function getNextPatchVersion(): string
{
return implode('.', [
$this->getMajorVersionNumber(),
$this->getMinorVersionNumber(),
$this->getPatchVersionNumber() + 1,
]);
}

public function getMajorVersionNumber(): int
{
$version = $this->stripExtraDataFromVersion($this->version);
$version = explode('.', $version);

return (int) $version[0];
}

public function getMinorVersionNumber(): int
{
$version = $this->stripExtraDataFromVersion($this->version);
$version = explode('.', $version);

return (int) $version[1];
}

public function getPatchVersionNumber(): int
{
$version = $this->stripExtraDataFromVersion($this->version);
$version = explode('.', $version);

return (int) $version[2];
}

private function stripExtraDataFromVersion(string $version): string
{
if (str_starts_with($version, '1.')) {
$version = substr($version, 2);
}
$version = explode('-', $version);

return $version[0];
}

public function getMinPhpVersion(): ?string
{
return $this->minPhpVersion;
Expand Down
69 changes: 69 additions & 0 deletions src/Util/VersionUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace App\Util;

use App\Model\PrestaShop;

class VersionUtils
{
/**
* Returns highest available stable version from a list of Prestashop versions.
* Ignores beta and release candidates.
*
* @param PrestaShop[] $list
*
* @return PrestaShop|null
*/
public function getHighestStableVersionFromList(array $list = []): ?Prestashop
{
if (empty($list)) {
return null;
}

// Get highest version available
$highestVersion = null;
foreach ($list as $version) {
if (($highestVersion === null || version_compare($version->getVersion(), $highestVersion->getVersion(), '>')) &&
$version->isStable()) {
$highestVersion = $version;
}
}

return $highestVersion;
}

/**
* Returns highest available stable version of a previous major from a list of Prestashop versions.
* Ignores beta and release candidates.
*
* @param PrestaShop[] $list
*
* @return PrestaShop|null
*/
public function getHighestStablePreviousVersionFromList(array $list = []): ?Prestashop
{
if (empty($list)) {
return null;
}

$highestVersion = $this->getHighestStableVersionFromList($list);

if (empty($highestVersion)) {
return null;
}

$possiblePreviousMajor = $highestVersion->getMajorVersionNumber() - 1;
$highestPreviousVersion = null;
foreach ($list as $version) {
if (($highestPreviousVersion === null || version_compare($version->getVersion(), $highestPreviousVersion->getVersion(), '>')) &&
$version->getMajorVersionNumber() == $possiblePreviousMajor &&
$version->isStable()) {
$highestPreviousVersion = $version;
}
}

return $highestPreviousVersion;
}
}
60 changes: 60 additions & 0 deletions tests/Command/GenerateJsonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Command;

use App\Command\GenerateJsonCommand;
use App\Model\PrestaShop;
use App\Util\ModuleUtils;
use App\Util\PrestaShopUtils;
use App\Util\PublicDownloadUrlProvider;
Expand Down Expand Up @@ -107,4 +108,63 @@ public function testGenerateJson()
$baseOutput . '/prestashop/beta.json'
);
}

/**
* @dataProvider versionListProvider
*/
public function testAddVersionsUnderDelopment(array $before, array $afterExpected)
{
$this->assertEquals($afterExpected, $this->command->addVersionsUnderDelopment($before));
}

public function versionListProvider(): iterable
{
// Pretty normal scenario
yield [[
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('9.0.0'),
new Prestashop('9.0.3'),
new Prestashop('1.7.8.10'),
], [
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('9.0.0'),
new Prestashop('9.0.3'),
new Prestashop('1.7.8.10'),
new Prestashop('10.0.0'),
new Prestashop('9.1.0'),
new Prestashop('9.0.4'),
new Prestashop('8.2.0'),
new Prestashop('8.1.5'),
]];
// Scenario to avoid adding 1.7 versions as a previous major
yield [[
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('1.7.8.10'),
], [
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('1.7.8.10'),
new Prestashop('9.0.0'),
new Prestashop('8.2.0'),
new Prestashop('8.1.5'),
]];
// Scenario to avoid considering beta as a stable channel
yield [[
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('9.0.0-beta'),
new Prestashop('1.7.8.10'),
], [
new Prestashop('8.1.4'),
new Prestashop('8.1.3'),
new Prestashop('9.0.0-beta'),
new Prestashop('1.7.8.10'),
new Prestashop('9.0.0'),
new Prestashop('8.2.0'),
new Prestashop('8.1.5'),
]];
}
}
41 changes: 41 additions & 0 deletions tests/Model/PrestaShopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ public function testIsStable(string $version, bool $expected)
$this->assertSame($expected, $prestaShop->isStable());
}

/**
* @dataProvider versionNumberProvider
*/
public function testVersionNumber(string $version, int $expectedMajor, int $expectedMinor, int $expectedPatch)
{
$prestaShop = new PrestaShop($version);
$this->assertSame($expectedMajor, $prestaShop->getMajorVersionNumber());
$this->assertSame($expectedMinor, $prestaShop->getMinorVersionNumber());
$this->assertSame($expectedPatch, $prestaShop->getPatchVersionNumber());
}

/**
* @dataProvider nextVersionsProvider
*/
public function testNextVersion(string $version, string $nextMajor, string $nextMinor, string $nextPatch)
{
$prestaShop = new PrestaShop($version);
$this->assertSame($nextMajor, $prestaShop->getNextMajorVersion());
$this->assertSame($nextMinor, $prestaShop->getNextMinorVersion());
$this->assertSame($nextPatch, $prestaShop->getNextPatchVersion());
}

/**
* @dataProvider rcProvider
*/
Expand Down Expand Up @@ -77,4 +99,23 @@ public function betaProvider(): iterable
yield ['8.0.0-beta.1', true];
yield ['8.0.0-beta.2', true];
}

public function versionNumberProvider(): iterable
{
yield ['1.7.8.0', 7, 8, 0];
yield ['8.0.0', 8, 0, 0];
yield ['8.0.0-rc.1', 8, 0, 0];
yield ['8.0.0-beta.1', 8, 0, 0];
yield ['8.1.4', 8, 1, 4];
}

public function nextVersionsProvider(): iterable
{
yield ['1.7.8.0', '8.0.0', '7.9.0', '7.8.1'];
yield ['8.0.0', '9.0.0', '8.1.0', '8.0.1'];
yield ['8.0.0-rc.1', '9.0.0', '8.1.0', '8.0.1'];
yield ['8.0.0-beta.1', '9.0.0', '8.1.0', '8.0.1'];
yield ['8.1.4', '9.0.0', '8.2.0', '8.1.5'];
yield ['9.0.0', '10.0.0', '9.1.0', '9.0.1'];
}
}

0 comments on commit 36d7818

Please sign in to comment.