Skip to content
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
5 changes: 5 additions & 0 deletions ci/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ RUN curl -sS https://getcomposer.org/installer | php -- --2.2 --filename=compose
# Use version 1 for main composer binary
RUN rm -f /usr/local/bin/composer; ln -s /usr/local/bin/composer2 /usr/local/bin/composer

# Install elgentos magento2-static-deploy binary for high-performance static content deployment
RUN curl -sL -o /opt/magento2-static-deploy \
https://github.com/elgentos/magento2-static-deploy/releases/latest/download/magento2-static-deploy-linux-amd64 \
&& chmod +x /opt/magento2-static-deploy

# Set python3 as default python executable
RUN ln -s /usr/bin/python3 /usr/local/bin/python

Expand Down
59 changes: 59 additions & 0 deletions src/Deployer/Task/Build/HighPerformanceStaticDeployTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Hypernode\Deploy\Deployer\Task\Build;

use Hypernode\Deploy\Deployer\Task\TaskBase;
use Hypernode\DeployConfiguration\Configuration;

use function Deployer\get;
use function Deployer\run;
use function Deployer\task;
use function Deployer\within;

/**
* High-performance static content deployment using elgentos/magento2-static-deploy.
*
* @see https://github.com/elgentos/magento2-static-deploy
*/
class HighPerformanceStaticDeployTask extends TaskBase
{
private const BINARY_PATH = '/opt/magento2-static-deploy';

public function configure(Configuration $config): void
{
if (!$this->isEnabled($config)) {
return;
}

task('magento:deploy:assets', function () {
$themes = get('magento_themes', []);
$themeArgs = $this->buildThemeArgs($themes);
$locales = get('static_content_locales', 'en_US');
$contentVersion = get('content_version', time());

within('{{release_or_current_path}}', function () use ($themeArgs, $locales, $contentVersion) {
run(self::BINARY_PATH . " --force --area=frontend --area=adminhtml $themeArgs --content-version=$contentVersion --verbose $locales");
});
})->select('stage=build');
}

public function isEnabled(Configuration $config): bool
{
$variables = $config->getVariables();
$buildVariables = $config->getVariables('build');

return $variables['high_performance_static_deploy']
?? $buildVariables['high_performance_static_deploy']
?? false;
}

/**
* @param array<string, string> $themes
*/
public function buildThemeArgs(array $themes): string
{
return implode(' ', array_map(fn($t) => "--theme=$t", array_keys($themes)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Hypernode\Deploy\Tests\Unit\Deployer\Task\Build;

use Hypernode\Deploy\Deployer\Task\Build\HighPerformanceStaticDeployTask;
use Hypernode\DeployConfiguration\Configuration;
use PHPUnit\Framework\TestCase;

class HighPerformanceStaticDeployTaskTest extends TestCase
{
private HighPerformanceStaticDeployTask $task;

protected function setUp(): void
{
$this->task = new HighPerformanceStaticDeployTask();
}

public function testIsEnabledReturnsFalseWhenNotConfigured(): void
{
$config = $this->createMock(Configuration::class);
$config->method('getVariables')->willReturn([]);

$this->assertFalse($this->task->isEnabled($config));
}

public function testIsEnabledReturnsTrueWhenEnabledInVariables(): void
{
$config = $this->createMock(Configuration::class);
$config->method('getVariables')
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
'all' => ['high_performance_static_deploy' => true],
default => [],
});

$this->assertTrue($this->task->isEnabled($config));
}

public function testIsEnabledReturnsTrueWhenEnabledInBuildVariables(): void
{
$config = $this->createMock(Configuration::class);
$config->method('getVariables')
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
'build' => ['high_performance_static_deploy' => true],
default => [],
});

$this->assertTrue($this->task->isEnabled($config));
}

public function testIsEnabledReturnsFalseWhenExplicitlyDisabled(): void
{
$config = $this->createMock(Configuration::class);
$config->method('getVariables')
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
'all' => ['high_performance_static_deploy' => false],
default => [],
});

$this->assertFalse($this->task->isEnabled($config));
}

public function testBuildThemeArgsWithSingleTheme(): void
{
$themes = ['Vendor/theme' => 'nl_NL en_US'];

$result = $this->task->buildThemeArgs($themes);

$this->assertSame('--theme=Vendor/theme', $result);
}

public function testBuildThemeArgsWithMultipleThemes(): void
{
$themes = [
'Vendor/theme1' => 'nl_NL',
'Vendor/theme2' => 'en_US',
'Vendor/theme3' => 'de_DE',
];

$result = $this->task->buildThemeArgs($themes);

$this->assertSame('--theme=Vendor/theme1 --theme=Vendor/theme2 --theme=Vendor/theme3', $result);
}

public function testBuildThemeArgsWithEmptyArray(): void
{
$result = $this->task->buildThemeArgs([]);

$this->assertSame('', $result);
}
}