Skip to content

Commit 55eda16

Browse files
authored
Merge pull request #194 from ByteInternet/add-support-for-high-performance-static-content-deployment
Add high-performance static content deployment task
2 parents bc6a737 + 155561b commit 55eda16

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

ci/build/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ RUN curl -sS https://getcomposer.org/installer | php -- --2.2 --filename=compose
117117
# Use version 1 for main composer binary
118118
RUN rm -f /usr/local/bin/composer; ln -s /usr/local/bin/composer2 /usr/local/bin/composer
119119

120+
# Install elgentos magento2-static-deploy binary for high-performance static content deployment
121+
RUN curl -sL -o /opt/magento2-static-deploy \
122+
https://github.com/elgentos/magento2-static-deploy/releases/latest/download/magento2-static-deploy-linux-amd64 \
123+
&& chmod +x /opt/magento2-static-deploy
124+
120125
# Set python3 as default python executable
121126
RUN ln -s /usr/bin/python3 /usr/local/bin/python
122127

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy\Deployer\Task\Build;
6+
7+
use Hypernode\Deploy\Deployer\Task\TaskBase;
8+
use Hypernode\DeployConfiguration\Configuration;
9+
10+
use function Deployer\get;
11+
use function Deployer\run;
12+
use function Deployer\task;
13+
use function Deployer\within;
14+
15+
/**
16+
* High-performance static content deployment using elgentos/magento2-static-deploy.
17+
*
18+
* @see https://github.com/elgentos/magento2-static-deploy
19+
*/
20+
class HighPerformanceStaticDeployTask extends TaskBase
21+
{
22+
private const BINARY_PATH = '/opt/magento2-static-deploy';
23+
24+
public function configure(Configuration $config): void
25+
{
26+
if (!$this->isEnabled($config)) {
27+
return;
28+
}
29+
30+
task('magento:deploy:assets', function () {
31+
$themes = get('magento_themes', []);
32+
$themeArgs = $this->buildThemeArgs($themes);
33+
$locales = get('static_content_locales', 'en_US');
34+
$contentVersion = get('content_version', time());
35+
36+
within('{{release_or_current_path}}', function () use ($themeArgs, $locales, $contentVersion) {
37+
run(self::BINARY_PATH . " --force --area=frontend --area=adminhtml $themeArgs --content-version=$contentVersion --verbose $locales");
38+
});
39+
})->select('stage=build');
40+
}
41+
42+
public function isEnabled(Configuration $config): bool
43+
{
44+
$variables = $config->getVariables();
45+
$buildVariables = $config->getVariables('build');
46+
47+
return $variables['high_performance_static_deploy']
48+
?? $buildVariables['high_performance_static_deploy']
49+
?? false;
50+
}
51+
52+
/**
53+
* @param array<string, string> $themes
54+
*/
55+
public function buildThemeArgs(array $themes): string
56+
{
57+
return implode(' ', array_map(fn($t) => "--theme=$t", array_keys($themes)));
58+
}
59+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy\Tests\Unit\Deployer\Task\Build;
6+
7+
use Hypernode\Deploy\Deployer\Task\Build\HighPerformanceStaticDeployTask;
8+
use Hypernode\DeployConfiguration\Configuration;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class HighPerformanceStaticDeployTaskTest extends TestCase
12+
{
13+
private HighPerformanceStaticDeployTask $task;
14+
15+
protected function setUp(): void
16+
{
17+
$this->task = new HighPerformanceStaticDeployTask();
18+
}
19+
20+
public function testIsEnabledReturnsFalseWhenNotConfigured(): void
21+
{
22+
$config = $this->createMock(Configuration::class);
23+
$config->method('getVariables')->willReturn([]);
24+
25+
$this->assertFalse($this->task->isEnabled($config));
26+
}
27+
28+
public function testIsEnabledReturnsTrueWhenEnabledInVariables(): void
29+
{
30+
$config = $this->createMock(Configuration::class);
31+
$config->method('getVariables')
32+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
33+
'all' => ['high_performance_static_deploy' => true],
34+
default => [],
35+
});
36+
37+
$this->assertTrue($this->task->isEnabled($config));
38+
}
39+
40+
public function testIsEnabledReturnsTrueWhenEnabledInBuildVariables(): void
41+
{
42+
$config = $this->createMock(Configuration::class);
43+
$config->method('getVariables')
44+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
45+
'build' => ['high_performance_static_deploy' => true],
46+
default => [],
47+
});
48+
49+
$this->assertTrue($this->task->isEnabled($config));
50+
}
51+
52+
public function testIsEnabledReturnsFalseWhenExplicitlyDisabled(): void
53+
{
54+
$config = $this->createMock(Configuration::class);
55+
$config->method('getVariables')
56+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
57+
'all' => ['high_performance_static_deploy' => false],
58+
default => [],
59+
});
60+
61+
$this->assertFalse($this->task->isEnabled($config));
62+
}
63+
64+
public function testBuildThemeArgsWithSingleTheme(): void
65+
{
66+
$themes = ['Vendor/theme' => 'nl_NL en_US'];
67+
68+
$result = $this->task->buildThemeArgs($themes);
69+
70+
$this->assertSame('--theme=Vendor/theme', $result);
71+
}
72+
73+
public function testBuildThemeArgsWithMultipleThemes(): void
74+
{
75+
$themes = [
76+
'Vendor/theme1' => 'nl_NL',
77+
'Vendor/theme2' => 'en_US',
78+
'Vendor/theme3' => 'de_DE',
79+
];
80+
81+
$result = $this->task->buildThemeArgs($themes);
82+
83+
$this->assertSame('--theme=Vendor/theme1 --theme=Vendor/theme2 --theme=Vendor/theme3', $result);
84+
}
85+
86+
public function testBuildThemeArgsWithEmptyArray(): void
87+
{
88+
$result = $this->task->buildThemeArgs([]);
89+
90+
$this->assertSame('', $result);
91+
}
92+
}

0 commit comments

Comments
 (0)