Skip to content

Commit

Permalink
MAGECLOUD-3011: Add Baler Support (magento#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatsche authored Jan 31, 2020
1 parent 4727376 commit c88c6e6
Show file tree
Hide file tree
Showing 10 changed files with 564 additions and 3 deletions.
12 changes: 12 additions & 0 deletions config/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ SCD_MATRIX:
deploy:
SCD_MATRIX:
magento/backend: []
SCD_USE_BALER:
description: Run Baler after performing static content deployment in order to generate an optimized JavaScript bundle.
type: boolean
stages:
- global
- build
default:
build: false
examples:
- stage:
build:
SCD_USE_BALER: true
SKIP_SCD:
description: Skips static content deployment during the build/deploy phase.
type: boolean
Expand Down
2 changes: 2 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<service id="Magento\MagentoCloud\Shell\ProcessException" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\BackupData" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\DeployStaticContent" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\RunBaler" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\DeployCompletion" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\DeployStaticContent" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\InstallUpdate" autowire="false" />
Expand Down Expand Up @@ -77,6 +78,7 @@
</argument>
</service>
<service id="Symfony\Component\Serializer\Encoder\XmlEncoder"/>
<service id="Symfony\Component\Process\ExecutableFinder"/>
<!-- Interface mappings -->
<service id="Magento\MagentoCloud\PlatformVariable\DecoderInterface" alias="Magento\MagentoCloud\PlatformVariable\Decoder" />
<service id="Magento\MagentoCloud\Shell\ShellInterface" alias="Magento\MagentoCloud\Shell\Shell" />
Expand Down
2 changes: 1 addition & 1 deletion dist/.magento.env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
# host: "<udp-host>" #
# port: <udp-port> #
# chunk_size: 1024 #
#######################################################################################################################
#######################################################################################################################
9 changes: 9 additions & 0 deletions scenario/build/generate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@
</argument>
</arguments>
</step>
<step name="run-baler" type="Magento\MagentoCloud\Step\Build\RunBaler" priority="1300">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="build-config" xsi:type="object">Magento\MagentoCloud\Config\Stage\BuildInterface</argument>
<argument name="flag-manager" xsi:type="object">Magento\MagentoCloud\Filesystem\Flag\Manager</argument>
<argument name="baler-validator" xsi:type="object">Magento\MagentoCloud\Config\Validator\Build\BalerSupport</argument>
<argument name="shell" xsi:type="object">Magento\MagentoCloud\Shell\ShellInterface</argument>
</arguments>
</step>
</scenario>
7 changes: 6 additions & 1 deletion src/Config/Stage/BuildInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ interface BuildInterface extends StageConfigInterface
/**
* Subdirectory nesting level
*/
const VAR_ERROR_REPORT_DIR_NESTING_LEVEL = 'ERROR_REPORT_DIR_NESTING_LEVEL';
public const VAR_ERROR_REPORT_DIR_NESTING_LEVEL = 'ERROR_REPORT_DIR_NESTING_LEVEL';

/**
* Perfom Baler JS bundling
*/
public const VAR_SCD_USE_BALER = 'SCD_USE_BALER';
}
85 changes: 85 additions & 0 deletions src/Config/Validator/Build/BalerSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Magento\MagentoCloud\Config\Validator\Build;

use Magento\MagentoCloud\Config\Magento\Shared\ReaderInterface;
use Magento\MagentoCloud\Config\Validator;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Symfony\Component\Process\ExecutableFinder;

/**
* Verify that the build configuration is ready for baler.
*/
class BalerSupport implements ValidatorInterface
{
/**
* @var ExecutableFinder
*/
private $finder;

/**
* @var Validator\ResultFactory
*/
private $resultFactory;

/**
* @var ReaderInterface
*/
private $configReader;

/**
* @param Validator\ResultFactory $resultFactory
* @param ExecutableFinder $finder
* @param ReaderInterface $configReader
*/
public function __construct(
Validator\ResultFactory $resultFactory,
ExecutableFinder $finder,
ReaderInterface $configReader
) {
$this->resultFactory = $resultFactory;
$this->finder = $finder;
$this->configReader = $configReader;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function validate(): Validator\ResultInterface
{
$errors = [];

if ($this->finder->find('baler') === null) {
$errors[] = 'Path to baler executable could not be found.'
. ' The Node package may not be installed or may not be linked.';
}

$magentoConfig = $this->configReader->read();

if (!isset($magentoConfig['modules']['Magento_Baler']) || !$magentoConfig['modules']['Magento_Baler']) {
$errors[] = 'The Magento_Baler module is not installed or is disabled.';
}

$jsConfig = $magentoConfig['system']['default']['dev']['js'] ?? [];

if (!isset($jsConfig['enable_baler_js_bundling']) || !$jsConfig['enable_baler_js_bundling']) {
$errors[] = 'The Magento config dev/js/enable_baler_js_bundling must be enabled in app/etc/config.php.';
}

foreach (['minify_files', 'enable_js_bundling', 'merge_files'] as $configKey) {
if (!isset($jsConfig[$configKey]) || $jsConfig[$configKey]) {
$errors[] = sprintf('The Magento config dev/js/%s must be disabled in app/etc/config.php.', $configKey);
}
}

return $errors === []
? $this->resultFactory->success()
: $this->resultFactory->error(
'Baler JS bundling cannot be used because of the following issues:',
implode(PHP_EOL, $errors)
);
}
}
107 changes: 107 additions & 0 deletions src/Step/Build/RunBaler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Magento\MagentoCloud\Step\Build;

use Magento\MagentoCloud\Config\Stage\BuildInterface;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Magento\MagentoCloud\Config\Validator\Result;
use Magento\MagentoCloud\Filesystem\Flag;
use Magento\MagentoCloud\Shell\ShellException;
use Magento\MagentoCloud\Shell\ShellInterface;
use Magento\MagentoCloud\Step\StepException;
use Magento\MagentoCloud\Step\StepInterface;
use Psr\Log\LoggerInterface;

/**
* Attempt to perform JS bundling using Baler.
*/
class RunBaler implements StepInterface
{
/**
* @var BuildInterface
*/
private $buildConfig;

/**
* @var Flag\Manager
*/
private $flagManager;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var ShellInterface
*/
private $shell;

/**
* @param LoggerInterface $logger,
* @param BuildInterface $buildConfig,
* @param Flag\Manager $flagManager,
* @param ValidatorInterface $validator,
* @param ShellInterface $shell
*/
public function __construct(
LoggerInterface $logger,
BuildInterface $buildConfig,
Flag\Manager $flagManager,
ValidatorInterface $validator,
ShellInterface $shell
) {
$this->logger = $logger;
$this->buildConfig = $buildConfig;
$this->flagManager = $flagManager;
$this->validator = $validator;
$this->shell = $shell;
}

/**
* @inheritdoc
*/
public function execute()
{
if (!$this->buildConfig->get(BuildInterface::VAR_SCD_USE_BALER)) {
$this->logger->debug('Baler JS bundling is disabled.');

return;
}

if (!$this->flagManager->exists(Flag\Manager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD)) {
$this->logger->notice('Cannot run baler because static content has not been deployed.');

return;
}

$result = $this->validator->validate();

if ($result instanceof Result\Error) {
$this->logger->warning($result->getError());

foreach (explode(PHP_EOL, $result->getSuggestion()) as $detail) {
$this->logger->warning(' - ' . $detail);
}

return;
}

$this->logger->info('Running Baler JS bundler.');

try {
$this->shell->execute('baler');
} catch (ShellException $exception) {
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
}

$this->logger->info('Baler JS bundling complete.');
}
}
3 changes: 2 additions & 1 deletion src/Test/Unit/Config/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function testGetDefaultsForBuild(): void
BuildInterface::VAR_VERBOSE_COMMANDS => '',
BuildInterface::VAR_SCD_MATRIX => [],
BuildInterface::VAR_SCD_MAX_EXEC_TIME => null,
BuildInterface::VAR_ERROR_REPORT_DIR_NESTING_LEVEL => 1
BuildInterface::VAR_ERROR_REPORT_DIR_NESTING_LEVEL => 1,
BuildInterface::VAR_SCD_USE_BALER => false,
],
$this->schema->getDefaults(StageConfigInterface::STAGE_BUILD)
);
Expand Down
Loading

0 comments on commit c88c6e6

Please sign in to comment.