Skip to content

Commit

Permalink
Merge pull request #967 from M0rgan01/compare-release-fix
Browse files Browse the repository at this point in the history
Rework UpdateFiles warmup
  • Loading branch information
M0rgan01 authored Nov 7, 2024
2 parents f10b307 + 7958c90 commit 2213114
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 126 deletions.
4 changes: 2 additions & 2 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public function getProperties($type): array
break;
case self::WITH_UPDATE_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->state->getOriginVersion(),
'to_ps_version' => $this->state->getInstallVersion(),
'from_ps_version' => $this->state->getCurrentVersion(),
'to_ps_version' => $this->state->getDestinationVersion(),
'upgrade_channel' => $this->upgradeConfiguration->getChannel(),
'disable_non_native_modules' => $this->upgradeConfiguration->shouldDeactivateCustomModules(),
'switch_to_default_theme' => $this->upgradeConfiguration->shouldSwitchToDefaultTheme(),
Expand Down
10 changes: 3 additions & 7 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\ErrorHandler;
use PrestaShop\Module\AutoUpgrade\Log\CliLogger;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -94,17 +94,13 @@ protected function loadConfiguration(?string $configPath, UpgradeContainer $upgr
$this->logger->debug('Loading configuration from ' . $configPath);
$configFile = file_get_contents($configPath);
if (!$configFile) {
$this->logger->error('Configuration file not found a location ' . $configPath);

return ExitCode::FAIL;
throw new InvalidArgumentException('Configuration file not found a location ' . $configPath);
}

$inputData = json_decode($configFile, true);

if (!$inputData) {
$this->logger->error('An error occurred during the json decode process, please check the content and syntax of the file content');

return ExitCode::FAIL;
throw new InvalidArgumentException('An error occurred during the json decode process, please check the content and syntax of the file content');
}

$this->logger->debug('Configuration file content: ' . json_encode($inputData));
Expand Down
4 changes: 2 additions & 2 deletions classes/Commands/CheckRequirementsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$phpVersionResolverService = new PhpVersionResolverService(
$distributionApiService,
$this->upgradeContainer->getFileLoader(),
$this->upgradeContainer->getState()->getOriginVersion()
$this->upgradeContainer->getState()->getCurrentVersion()
);

$this->upgradeSelfCheck = new UpgradeSelfCheck(
Expand All @@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
_PS_ROOT_DIR_,
_PS_ADMIN_DIR_,
$moduleConfigPath,
$this->upgradeContainer->getState()->getOriginVersion()
$this->upgradeContainer->getState()->getCurrentVersion()
);

$output->writeln('Result of prerequisite checks:');
Expand Down
10 changes: 0 additions & 10 deletions classes/Parameters/UpgradeFileNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ class UpgradeFileNames
*/
const MODULES_TO_UPGRADE_LIST = 'modulesToUpgrade.list';

/**
* during upgradeFiles process,
* this files contains the list of files left to upgrade in a serialized array.
* (this file is deleted in init() method if you reload the page).
*
* @var string
*/
const FILES_DIFF_LIST = 'filesDiff.list';

/**
* during backupFiles process,
* this files contains the list of files left to save in a serialized array.
Expand Down Expand Up @@ -166,7 +157,6 @@ class UpgradeFileNames
public static $tmp_files = [
'QUERIES_TO_UPGRADE_LIST', // used ?
'FILES_TO_UPGRADE_LIST',
'FILES_DIFF_LIST',
'FILES_TO_BACKUP_LIST',
'DB_TABLES_TO_BACKUP_LIST',
'QUERIES_TO_RESTORE_LIST',
Expand Down
59 changes: 23 additions & 36 deletions classes/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class State
/**
* @var string
*/
private $originVersion; // Origin version of PrestaShop
private $currentVersion; // Origin version of PrestaShop
/**
* @var ?string
*/
private $install_version; // Destination version of PrestaShop
private $destinationVersion; // Destination version of PrestaShop
/**
* @var string
*/
Expand Down Expand Up @@ -96,18 +96,6 @@ class State
* @var string[]
*/
private $installedLanguagesIso = [];
/**
* modules_addons is an array of array(id_addons => name_module).
*
* @var array<string, string>
*/
private $modules_addons = [];
/**
* modules_versions is an array of array(id_addons => version of the module).
*
* @var array<string, string>
*/
private $modules_versions = [];

/**
* @var bool Determining if all steps went totally successfully
Expand All @@ -120,6 +108,9 @@ class State
/** @var ?string */
private $processTimestamp;

/** @var bool Allows you to know if the state has been initialized using the initDefault method */
private $initialized = false;

/**
* @param array<string, mixed> $savedState from another request
*/
Expand Down Expand Up @@ -152,7 +143,7 @@ public function export(): array
return get_object_vars($this);
}

public function initDefault(string $version): void
public function initDefault(string $currentVersion, ?string $destinationVersion): void
{
// installedLanguagesIso is used to merge translations files
$installedLanguagesIso = array_map(
Expand All @@ -163,21 +154,24 @@ function ($v) { return $v['iso_code']; },

$rand = dechex(mt_rand(0, min(0xffffffff, mt_getrandmax())));
$date = date('Ymd-His');
$backupName = 'V' . $version . '_' . $date . '-' . $rand;
$backupName = 'V' . $currentVersion . '_' . $date . '-' . $rand;
// Todo: To be moved in state class? We could only require the backup name here
// I.e = $this->upgradeContainer->getState()->setBackupName($backupName);, which triggers 2 other setters internally
$this->setBackupName($backupName);
$this->setCurrentVersion($currentVersion);
$this->setDestinationVersion($destinationVersion);
$this->initialized = true;
}

// GETTERS
public function getOriginVersion(): string
public function getCurrentVersion(): string
{
return $this->originVersion;
return $this->currentVersion;
}

public function getInstallVersion(): ?string
public function getDestinationVersion(): ?string
{
return $this->install_version;
return $this->destinationVersion;
}

public function getBackupName(): string
Expand Down Expand Up @@ -258,16 +252,16 @@ public function getProcessTimestamp(): ?string
}

// SETTERS
public function setOriginVersion(string $originVersion): State
public function setCurrentVersion(string $currentVersion): State
{
$this->originVersion = $originVersion;
$this->currentVersion = $currentVersion;

return $this;
}

public function setInstallVersion(?string $install_version): State
public function setDestinationVersion(?string $destinationVersion): State
{
$this->install_version = $install_version;
$this->destinationVersion = $destinationVersion;

return $this;
}
Expand Down Expand Up @@ -375,18 +369,6 @@ public function setInstalledLanguagesIso(array $installedLanguagesIso): State
return $this;
}

/**
* @param array<string, string> $modules_versions
*
* @return self
*/
public function setModulesVersions(array $modules_versions): State
{
$this->modules_versions = $modules_versions;

return $this;
}

public function setWarningExists(bool $warning_exists): State
{
$this->warning_exists = $warning_exists;
Expand All @@ -409,4 +391,9 @@ public function setProcessTimestamp(string $processTimestamp): void
{
$this->processTimestamp = $processTimestamp;
}

public function isInitialized(): bool
{
return $this->initialized;
}
}
18 changes: 18 additions & 0 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\Upgrader;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

abstract class AbstractTask
Expand Down Expand Up @@ -184,6 +185,23 @@ public function setErrorFlag(): void
public function init(): void
{
$this->container->initPrestaShopCore();
$this->setupEnvironment();
}

/**
* @throws Exception
*/
protected function setupEnvironment(): void
{
if ($this::TASK_TYPE === TaskType::TASK_TYPE_UPDATE && $this->container->getUpgrader()->getChannel() === Upgrader::CHANNEL_LOCAL) {
$archiveXml = $this->container->getUpgradeConfiguration()->getLocalChannelXml();
$this->container->getFileLoader()->addXmlMd5File($this->container->getUpgrader()->getDestinationVersion(), $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH) . DIRECTORY_SEPARATOR . $archiveXml);
}

if ($this::TASK_TYPE !== TaskType::TASK_TYPE_RESTORE && !$this->container->getState()->isInitialized()) {
$this->container->getState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION), $this->container->getUpgrader()->getDestinationVersion());
$this->logger->debug($this->translator->trans('Successfully initialized state.'));
}
}

abstract public function run(): int;
Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Miscellaneous/CompareReleases.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
namespace PrestaShop\Module\AutoUpgrade\Task\Miscellaneous;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;

/**
* This class gets the list of all modified and deleted files between current version
* and target version (according to channel configuration).
*
* TODO Task to remove after removing the old UI
*/
class CompareReleases extends AbstractTask
{
Expand All @@ -57,7 +58,6 @@ public function run(): int
$this->nextParams['status'] = 'error';
$this->nextParams['msg'] = sprintf('Unable to generate diff file list between %1$s and %2$s.', _PS_VERSION_, $version);
} else {
$this->container->getFileConfigurationStorage()->save($diffFileList, UpgradeFileNames::FILES_DIFF_LIST);
$this->nextParams['msg'] = $this->translator->trans(
'%modifiedfiles% files will be modified, %deletedfiles% files will be deleted (if they are found).',
[
Expand Down
8 changes: 5 additions & 3 deletions classes/Task/Miscellaneous/UpdateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public function run(): int
return ExitCode::FAIL;
}

$this->container->getState()->setInstallVersion($this->container->getUpgrader()->getDestinationVersion());
$this->container->getState()->setOriginVersion($this->container->getProperty(UpgradeContainer::PS_VERSION));

return ExitCode::SUCCESS;
}

Expand Down Expand Up @@ -175,4 +172,9 @@ private function writeConfig(array $config): bool

return (new UpgradeConfigurationStorage($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR))->save($classConfig, UpgradeFileNames::CONFIG_FILENAME);
}

public function init(): void
{
$this->container->initPrestaShopCore();
}
}
2 changes: 0 additions & 2 deletions classes/Task/Runner/AllBackupTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

use Exception;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

/**
* Execute the whole upgrade process in a single request.
Expand All @@ -52,7 +51,6 @@ public function init(): void
{
if ($this->step === self::initialTask) {
parent::init();
$this->container->getState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION));
}
}

Expand Down
2 changes: 0 additions & 2 deletions classes/Task/Runner/AllUpdateTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Exception;
use PrestaShop\Module\AutoUpgrade\AjaxResponse;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -123,7 +122,6 @@ public function init(): void
{
if ($this->step === self::initialTask) {
parent::init();
$this->container->getState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION));
}
}
}
4 changes: 4 additions & 0 deletions classes/Task/Runner/SingleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function setOptions(array $options): void
if (!empty($options['action'])) {
$this->step = $options['action'];
}

if (!empty($options['data'])) {
$this->container->getState()->importFromEncodedData($options['data']);
}
}

protected function canContinue(): bool
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Update/UpdateComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function run(): int
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$destinationVersion = $this->container->getState()->getInstallVersion();
$destinationVersion = $this->container->getState()->getDestinationVersion();

$this->logger->info($this->container->getState()->getWarningExists() ?
$this->translator->trans('Shop updated to %s, but some warnings have been found.', [$destinationVersion]) :
Expand Down
Loading

0 comments on commit 2213114

Please sign in to comment.