Skip to content

Speedup static content deploy for specific theme #30090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/code/Magento/Deploy/Console/DeployStaticOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace Magento\Deploy\Console;

use Magento\Deploy\Process\Queue;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

/**
* Static Content Deployment Options helper
Expand Down Expand Up @@ -127,6 +127,11 @@ class DeployStaticOptions
*/
const NO_LESS = 'no-less';

/**
* Key for not compiling parent themes
*/
const NO_PARENT = 'no-parent';
Copy link
Contributor Author

@ihor-sviziev ihor-sviziev Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably backward incompatible change, but this class insn't marked as @api and that's the most logical place to put this new argument name


const DEFAULT_JOBS_AMOUNT = 0;

/**
Expand Down Expand Up @@ -324,7 +329,13 @@ private function getSkipOptions()
null,
InputOption::VALUE_NONE,
'Do not minify HTML files.'
)
),
new InputOption(
self::NO_PARENT,
null,
InputOption::VALUE_NONE,
'Do not compile parent themes. Supported only in quick and standard strategies.'
),
];
}
}
61 changes: 44 additions & 17 deletions app/code/Magento/Deploy/Console/InputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Deploy\Console;

use Magento\Setup\Console\Command\DeployStaticContentCommand;
use InvalidArgumentException;
use Magento\Deploy\Console\DeployStaticOptions as Options;
use Magento\Framework\Validator\Locale;
use Symfony\Component\Console\Input\InputInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Validator\Locale;
use Magento\Framework\Validator\RegexFactory;
use Symfony\Component\Console\Input\InputInterface;
use function array_key_exists;

/**
* Command input arguments validator class
Expand Down Expand Up @@ -66,7 +68,7 @@ class InputValidator
* InputValidator constructor
*
* @param Locale $localeValidator
* @param RegexFactory $versionValidatorFactory
* @param RegexFactory|null $versionValidatorFactory
*/
public function __construct(
Locale $localeValidator,
Expand Down Expand Up @@ -100,6 +102,10 @@ public function validate(InputInterface $input)
$this->checkVersionInput(
$input->getOption(Options::CONTENT_VERSION) ?: ''
);
$this->checkNoParentInput(
(bool)$input->getOption(Options::NO_PARENT),
(string)$input->getOption(Options::STRATEGY)
);
}

/**
Expand All @@ -108,12 +114,12 @@ public function validate(InputInterface $input)
* @param array $areasInclude
* @param array $areasExclude
* @return void
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function checkAreasInput(array $areasInclude, array $areasExclude)
{
if ($areasInclude[0] != 'all' && $areasExclude[0] != 'none') {
throw new \InvalidArgumentException(
if ($areasInclude[0] !== 'all' && $areasExclude[0] !== 'none') {
throw new InvalidArgumentException(
'--area (-a) and --exclude-area cannot be used at the same time'
);
}
Expand All @@ -125,12 +131,12 @@ private function checkAreasInput(array $areasInclude, array $areasExclude)
* @param array $themesInclude
* @param array $themesExclude
* @return void
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function checkThemesInput(array $themesInclude, array $themesExclude)
{
if ($themesInclude[0] != 'all' && $themesExclude[0] != 'none') {
throw new \InvalidArgumentException(
if ($themesInclude[0] !== 'all' && $themesExclude[0] !== 'none') {
throw new InvalidArgumentException(
'--theme (-t) and --exclude-theme cannot be used at the same time'
);
}
Expand All @@ -142,21 +148,21 @@ private function checkThemesInput(array $themesInclude, array $themesExclude)
* @param array $languagesInclude
* @param array $languagesExclude
* @return void
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function checkLanguagesInput(array $languagesInclude, array $languagesExclude)
{
if ($languagesInclude[0] != 'all') {
if ($languagesInclude[0] !== 'all') {
foreach ($languagesInclude as $lang) {
if (!$this->localeValidator->isValid($lang)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
$lang .
' argument has invalid value, please run info:language:list for list of available locales'
);
}
}
if ($languagesExclude[0] != 'none') {
throw new \InvalidArgumentException(
if ($languagesExclude[0] !== 'none') {
throw new InvalidArgumentException(
'--language (-l) and --exclude-language cannot be used at the same time'
);
}
Expand All @@ -167,7 +173,7 @@ private function checkLanguagesInput(array $languagesInclude, array $languagesEx
* Version input checks
*
* @param string $contentVersion
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function checkVersionInput(string $contentVersion): void
{
Expand All @@ -179,12 +185,33 @@ private function checkVersionInput(string $contentVersion): void
);

if (!$versionValidator->isValid($contentVersion)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'Argument "' .
Options::CONTENT_VERSION
. '" has invalid value, content version should contain only characters, digits and dots'
);
}
}
}

/**
* Validate if --no-parent flag could be used with selected strategy
*
* @param bool $noParent
* @param string $strategy
* @throws InvalidArgumentException
*/
private function checkNoParentInput(bool $noParent, string $strategy): void
{
$supportedStrategies = [
'quick' => true,
'standard' => true,
];

if ($noParent && !array_key_exists($strategy, $supportedStrategies)) {
throw new InvalidArgumentException(
sprintf('Argument "%s" is not supported with "%s" strategy', Options::NO_PARENT, $strategy)
);
}
}
}
46 changes: 40 additions & 6 deletions app/code/Magento/Deploy/Strategy/QuickDeploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*/
namespace Magento\Deploy\Strategy;

use Magento\Deploy\Package\PackagePool;
use Magento\Deploy\Console\DeployStaticOptions as Options;
use Magento\Deploy\Package\Package;
use Magento\Deploy\Package\PackagePool;
use Magento\Deploy\Process\Queue;
use function array_key_exists;

/**
* Quick deployment strategy implementation
Expand Down Expand Up @@ -51,7 +53,6 @@ public function deploy(array $options)
$groupedPackages = $deployPackages = [];
$packages = $this->packagePool->getPackagesForDeployment($options);
foreach ($packages as $package) {
/** @var Package $package */
if ($package->isVirtual()) {
// skip packages which can not be referenced directly
continue;
Expand All @@ -66,10 +67,17 @@ public function deploy(array $options)
$this->preparePackages($level, $levelPackages);
}

$parentCompilationRequested = $options[Options::NO_PARENT] !== true;
$includeThemesMap = array_flip($options[Options::THEME] ?? []);
$excludeThemesMap = array_flip($options[Options::EXCLUDE_THEME] ?? []);

foreach ($groupedPackages as $levelPackages) {
foreach ($levelPackages as $package) {
$this->queue->add($package);
$deployPackages[] = $package;
if ($parentCompilationRequested
|| $this->canDeployTheme($package->getTheme(), $includeThemesMap, $excludeThemesMap)) {
$this->queue->add($package);
$deployPackages[] = $package;
}
}
}

Expand All @@ -79,11 +87,13 @@ public function deploy(array $options)
}

/**
* Prepare packages before deploying
*
* @param int $level
* @param Package[] $levelPackages
* @return void
*/
private function preparePackages($level, array $levelPackages)
private function preparePackages(int $level, array $levelPackages): void
{
foreach ($levelPackages as $package) {
$package->aggregate();
Expand Down Expand Up @@ -120,7 +130,7 @@ private function preparePackages($level, array $levelPackages)
* @param Package $package
* @return int
*/
private function getInheritanceLevel(Package $package)
private function getInheritanceLevel(Package $package): int
{
$level = $package->getInheritanceLevel();
$packageId = $package->getArea() . '/' . $package->getTheme();
Expand All @@ -131,4 +141,28 @@ private function getInheritanceLevel(Package $package)
}
return $level;
}

/**
* Verify if specified theme should be deployed
*
* @param string $theme
* @param array $includedThemesMap
* @param array $excludedEntitiesMap
* @return bool
*/
private function canDeployTheme(string $theme, array $includedThemesMap, array $excludedEntitiesMap): bool
{
$includesAllThemes = array_key_exists('all', $includedThemesMap);
$excludesNoneThemes = array_key_exists('none', $excludedEntitiesMap);

if ($includesAllThemes && $excludesNoneThemes) {
return true;
} elseif (!$excludesNoneThemes) {
return !array_key_exists($theme, $excludedEntitiesMap);
} elseif (!$includesAllThemes) {
return array_key_exists($theme, $includedThemesMap);
}

return true;
}
}
36 changes: 34 additions & 2 deletions app/code/Magento/Deploy/Strategy/StandardDeploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
namespace Magento\Deploy\Strategy;

use Magento\Deploy\Package\PackagePool;
use Magento\Deploy\Console\DeployStaticOptions as Options;
use Magento\Deploy\Package\Package;
use Magento\Deploy\Package\PackagePool;
use Magento\Deploy\Process\Queue;

/**
Expand Down Expand Up @@ -60,12 +61,43 @@ public function deploy(array $options)
$deployedPackages[] = $package;
}

$parentCompilationRequested = $options[Options::NO_PARENT] !== true;
$includeThemesMap = array_flip($options[Options::THEME] ?? []);
$excludeThemesMap = array_flip($options[Options::EXCLUDE_THEME] ?? []);

foreach ($deployedPackages as $package) {
$this->queue->add($package);
if ($parentCompilationRequested
|| $this->canDeployTheme($package->getTheme(), $includeThemesMap, $excludeThemesMap)) {
$this->queue->add($package);
}
}

$this->queue->process();

return $deployedPackages;
}

/**
* Verify if specified theme should be deployed
*
* @param string $theme
* @param array $includedThemesMap
* @param array $excludedEntitiesMap
* @return bool
*/
private function canDeployTheme(string $theme, array $includedThemesMap, array $excludedEntitiesMap): bool
{
$includesAllThemes = array_key_exists('all', $includedThemesMap);
$excludesNoneThemes = array_key_exists('none', $excludedEntitiesMap);

if ($includesAllThemes && $excludesNoneThemes) {
return true;
} elseif (!$excludesNoneThemes) {
return !array_key_exists($theme, $excludedEntitiesMap);
} elseif (!$includesAllThemes) {
return array_key_exists($theme, $includedThemesMap);
}

return true;
}
}
Loading