|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenForgeProject\MageForge\Console\Command\Theme; |
| 6 | + |
| 7 | +use Laravel\Prompts\SelectPrompt; |
| 8 | +use Magento\Framework\Console\Cli; |
| 9 | +use Magento\Framework\Filesystem\Driver\File; |
| 10 | +use Magento\Framework\Shell; |
| 11 | +use OpenForgeProject\MageForge\Console\Command\AbstractCommand; |
| 12 | +use OpenForgeProject\MageForge\Model\ThemeList; |
| 13 | +use OpenForgeProject\MageForge\Model\ThemePath; |
| 14 | +use OpenForgeProject\MageForge\Service\ThemeBuilder\BuilderPool; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Command for generating Hyvä design tokens |
| 21 | + */ |
| 22 | +class TokensCommand extends AbstractCommand |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @param ThemeList $themeList |
| 26 | + * @param ThemePath $themePath |
| 27 | + * @param BuilderPool $builderPool |
| 28 | + * @param File $fileDriver |
| 29 | + * @param Shell $shell |
| 30 | + */ |
| 31 | + public function __construct( |
| 32 | + private readonly ThemeList $themeList, |
| 33 | + private readonly ThemePath $themePath, |
| 34 | + private readonly BuilderPool $builderPool, |
| 35 | + private readonly File $fileDriver, |
| 36 | + private readonly Shell $shell, |
| 37 | + ) { |
| 38 | + parent::__construct(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this->setName($this->getCommandName('hyva', 'tokens')) |
| 47 | + ->setAliases(['m:h:t']) |
| 48 | + ->setDescription('Generate Hyvä design tokens from design.tokens.json or hyva.config.json') |
| 49 | + ->addArgument( |
| 50 | + 'themeCode', |
| 51 | + InputArgument::OPTIONAL, |
| 52 | + 'Theme code to generate tokens for (format: Vendor/theme)' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + protected function executeCommand(InputInterface $input, OutputInterface $output): int |
| 60 | + { |
| 61 | + $themeCode = $input->getArgument('themeCode'); |
| 62 | + $isVerbose = $this->isVerbose($output); |
| 63 | + |
| 64 | + if (empty($themeCode)) { |
| 65 | + $themes = $this->themeList->getAllThemes(); |
| 66 | + $options = array_map(fn($theme) => $theme->getCode(), $themes); |
| 67 | + |
| 68 | + $themeCodePrompt = new SelectPrompt( |
| 69 | + label: 'Select theme to generate tokens for', |
| 70 | + options: $options, |
| 71 | + scroll: 10, |
| 72 | + hint: 'Arrow keys to navigate, Enter to confirm', |
| 73 | + ); |
| 74 | + |
| 75 | + try { |
| 76 | + $themeCode = $themeCodePrompt->prompt(); |
| 77 | + \Laravel\Prompts\Prompt::terminal()->restoreTty(); |
| 78 | + } catch (\Exception $e) { |
| 79 | + $this->io->error('Interactive mode failed: ' . $e->getMessage()); |
| 80 | + return Cli::RETURN_FAILURE; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $themePath = $this->themePath->getPath($themeCode); |
| 85 | + if ($themePath === null) { |
| 86 | + $this->io->error("Theme $themeCode is not installed."); |
| 87 | + return Cli::RETURN_FAILURE; |
| 88 | + } |
| 89 | + |
| 90 | + // Check if this is a Hyvä theme |
| 91 | + $builder = $this->builderPool->getBuilder($themePath); |
| 92 | + if ($builder === null || $builder->getName() !== 'HyvaThemes') { |
| 93 | + $this->io->error("Theme $themeCode is not a Hyvä theme. This command only works with Hyvä themes."); |
| 94 | + return Cli::RETURN_FAILURE; |
| 95 | + } |
| 96 | + |
| 97 | + $tailwindPath = rtrim($themePath, '/') . '/web/tailwind'; |
| 98 | + if (!$this->fileDriver->isDirectory($tailwindPath)) { |
| 99 | + $this->io->error("Tailwind directory not found in: $tailwindPath"); |
| 100 | + return Cli::RETURN_FAILURE; |
| 101 | + } |
| 102 | + |
| 103 | + // Check if node_modules exists |
| 104 | + if (!$this->fileDriver->isDirectory($tailwindPath . '/node_modules')) { |
| 105 | + $this->io->warning('Node modules not found. Please run: bin/magento mageforge:theme:build ' . $themeCode); |
| 106 | + return Cli::RETURN_FAILURE; |
| 107 | + } |
| 108 | + |
| 109 | + if ($isVerbose) { |
| 110 | + $this->io->section("Generating Hyvä design tokens for theme: $themeCode"); |
| 111 | + $this->io->text("Working directory: $tailwindPath"); |
| 112 | + } |
| 113 | + |
| 114 | + // Change to tailwind directory and run npx hyva-tokens |
| 115 | + $currentDir = getcwd(); |
| 116 | + chdir($tailwindPath); |
| 117 | + |
| 118 | + try { |
| 119 | + if ($isVerbose) { |
| 120 | + $this->io->text('Running npx hyva-tokens...'); |
| 121 | + } |
| 122 | + |
| 123 | + $this->shell->execute('npx hyva-tokens'); |
| 124 | + |
| 125 | + chdir($currentDir); |
| 126 | + |
| 127 | + // Determine output path based on theme location |
| 128 | + $isVendorTheme = str_contains($themePath, '/vendor/'); |
| 129 | + $sourceFilePath = $tailwindPath . '/generated/hyva-tokens.css'; |
| 130 | + |
| 131 | + if ($isVendorTheme) { |
| 132 | + // Store in var/generated/hyva-token/{ThemeCode}/ for vendor themes |
| 133 | + $varGeneratedPath = $currentDir . '/var/generated/hyva-token/' . str_replace('/', '/', $themeCode); |
| 134 | + |
| 135 | + if (!$this->fileDriver->isDirectory($varGeneratedPath)) { |
| 136 | + $this->fileDriver->createDirectory($varGeneratedPath, 0755); |
| 137 | + } |
| 138 | + |
| 139 | + $generatedFilePath = $varGeneratedPath . '/hyva-tokens.css'; |
| 140 | + |
| 141 | + // Copy file to var/generated location |
| 142 | + if ($this->fileDriver->isExists($sourceFilePath)) { |
| 143 | + $this->fileDriver->copy($sourceFilePath, $generatedFilePath); |
| 144 | + } |
| 145 | + |
| 146 | + $this->io->success('Hyvä design tokens generated successfully.'); |
| 147 | + $this->io->note('This is a vendor theme. Tokens have been saved to var/generated/hyva-token/ instead.'); |
| 148 | + $this->io->text('Generated file: ' . $generatedFilePath); |
| 149 | + } else { |
| 150 | + $generatedFilePath = $sourceFilePath; |
| 151 | + $this->io->success('Hyvä design tokens generated successfully.'); |
| 152 | + $this->io->text('Generated file: ' . $generatedFilePath); |
| 153 | + } |
| 154 | + |
| 155 | + $this->io->newLine(); |
| 156 | + |
| 157 | + return Cli::RETURN_SUCCESS; |
| 158 | + } catch (\Exception $e) { |
| 159 | + chdir($currentDir); |
| 160 | + $this->io->error('Failed to generate Hyvä design tokens: ' . $e->getMessage()); |
| 161 | + return Cli::RETURN_FAILURE; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments