diff --git a/ci/build/Dockerfile b/ci/build/Dockerfile index 2a5f7c0..d870d20 100644 --- a/ci/build/Dockerfile +++ b/ci/build/Dockerfile @@ -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 diff --git a/src/Deployer/Task/Build/HighPerformanceStaticDeployTask.php b/src/Deployer/Task/Build/HighPerformanceStaticDeployTask.php new file mode 100644 index 0000000..bd2a7ff --- /dev/null +++ b/src/Deployer/Task/Build/HighPerformanceStaticDeployTask.php @@ -0,0 +1,59 @@ +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 $themes + */ + public function buildThemeArgs(array $themes): string + { + return implode(' ', array_map(fn($t) => "--theme=$t", array_keys($themes))); + } +} diff --git a/tests/Unit/Deployer/Task/Build/HighPerformanceStaticDeployTaskTest.php b/tests/Unit/Deployer/Task/Build/HighPerformanceStaticDeployTaskTest.php new file mode 100644 index 0000000..5769c76 --- /dev/null +++ b/tests/Unit/Deployer/Task/Build/HighPerformanceStaticDeployTaskTest.php @@ -0,0 +1,92 @@ +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); + } +}