Skip to content
Merged
9 changes: 9 additions & 0 deletions docs/commands_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ bin/magento frontend:build Magento/luma Magento/blank

- `themeCodes` — One or more theme codes in format `Vendor/theme`. Accepts wildcards like `Magento/*`.

**Options:**

- `--with-parents` — Build all parent themes first, without asking.
- `--no-parents` — Never build parent themes (default in non-interactive environments).

**Behavior:**

- If no theme codes are provided, an interactive prompt lets you select themes.
- For each theme, the appropriate builder is determined automatically (Hyvä, TailwindCSS, Magento Standard, etc.).
- If a selected theme has parent themes (e.g. a child of `Vendor/base`), you are asked whether to build them first, topmost ancestor first. Only parents with a builder are offered; vendor (Composer) themes are skipped.
- Displays a summary of built themes and execution time.

---
Expand All @@ -74,9 +80,12 @@ bin/magento frontend:watch Magento/luma
**Options:**

- `-t, --theme=VALUE` — Alternative way to specify the theme code.
- `--with-parents` — Build all parent themes once before watching, without asking.
- `--no-parents` — Never build parent themes (default in non-interactive environments).

**Behavior:**

- For child themes, you are asked whether to build the parent themes once before the watcher starts. Only the selected theme is watched.
- Runs indefinitely until interrupted (Ctrl+C).
- Monitors source files (SCSS, JS, etc.) and triggers rebuilds on change.
- Useful for active theme development.
Expand Down
98 changes: 98 additions & 0 deletions src/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace OpenForgeProject\MageForge\Console\Command;

use Laravel\Prompts\ConfirmPrompt;
use Laravel\Prompts\SelectPrompt;
use Magento\Framework\Console\Cli;
use OpenForgeProject\MageForge\Model\ThemeAncestor;
use OpenForgeProject\MageForge\Model\ThemeList;
use OpenForgeProject\MageForge\Service\ThemeSuggester;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

Expand Down Expand Up @@ -196,6 +199,101 @@
}
}

/**
* Register the --with-parents / --no-parents options
*
* @return void
*/
protected function addParentThemeOptions(): void
{
$this->addOption(
'with-parents',
null,
InputOption::VALUE_NONE,
'Also build all parent themes first',
)->addOption('no-parents', null, InputOption::VALUE_NONE, 'Never build parent themes (skips the prompt)');
}

/**
* Decide whether the given parent themes should be built as well.
*
* Explicit options win; otherwise the user is asked in interactive terminals
* and parents are skipped in non-interactive environments.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param string $label
* @param array<string> $parents
* @param array<array<ThemeAncestor>> $chains
* @return bool
*/
protected function confirmParentThemes(
InputInterface $input,
OutputInterface $output,
string $label,
array $parents,
array $chains = [],
): bool {
if ($parents === [] || $input->getOption('no-parents')) {

Check warning on line 237 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "LogicalOr": @@ @@ array $parents, array $chains = [], ): bool { - if ($parents === [] || $input->getOption('no-parents')) { + if ($parents === [] && $input->getOption('no-parents')) { return false; }
return false;

Check warning on line 238 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ array $chains = [], ): bool { if ($parents === [] || $input->getOption('no-parents')) { - return false; + } if ($input->getOption('with-parents')) {
}

if ($input->getOption('with-parents')) {
return true;
}

if (!$input->isInteractive() || !$this->isInteractiveTerminal($output)) {

Check warning on line 245 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": @@ @@ return true; } - if (!$input->isInteractive() || !$this->isInteractiveTerminal($output)) { + if ($input->isInteractive() || $this->isInteractiveTerminal($output)) { return false; }

Check warning on line 245 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "LogicalNot": @@ @@ return true; } - if (!$input->isInteractive() || !$this->isInteractiveTerminal($output)) { + if ($input->isInteractive() || !$this->isInteractiveTerminal($output)) { return false; }
return false;
}

foreach ($chains as $themeCode => $chain) {
$this->renderInheritanceTree((string) $themeCode, $chain);
}

$this->setPromptEnvironment();
try {
$confirmed = (new ConfirmPrompt(
label: sprintf('Build parent theme(s) of %s first? (%s)', $label, implode(' → ', $parents)),
default: true,
))->prompt();
Comment thread
dermatz marked this conversation as resolved.
Resolved
\Laravel\Prompts\Prompt::terminal()->restoreTty();
} catch (\Exception) {
$confirmed = false;
} finally {
$this->resetPromptEnvironment();
}

return $confirmed === true;
}

/**
* Render a theme and its ancestors as an indented tree, child on top.
*
* @param string $themeCode
* @param array<ThemeAncestor> $chain
* @return void
*/
protected function renderInheritanceTree(string $themeCode, array $chain): void
{
$this->io->newLine();
$this->io->writeln(sprintf(' <fg=cyan;options=bold>%s</> <fg=gray>(selected)</>', $themeCode));

foreach (array_values($chain) as $level => $ancestor) {
$indent = str_repeat(' ', $level);
$status = $ancestor->skipReason === null
? '<fg=green>will be built</>'
: sprintf('<fg=gray>skipped: %s</>', $ancestor->skipReason);
$this->io->writeln(sprintf(
' %s└─ <fg=yellow>%s</> <fg=gray>(parent)</> · %s',
$indent,
$ancestor->code,
$status,
));
}

$this->io->newLine();
}

/**
* Check if terminal is interactive (supports Laravel Prompts)
*
Expand All @@ -206,7 +304,7 @@
{
// Check if output supports ANSI
if (!$output->isDecorated()) {
return false;

Check warning on line 307 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ { // Check if output supports ANSI if (!$output->isDecorated()) { - return false; + } // Check if STDIN is available (undefined outside the CLI SAPI)
}

// Check if STDIN is available (undefined outside the CLI SAPI)
Expand All @@ -215,7 +313,7 @@
}

// Check for CI environments
$nonInteractiveEnvs = [

Check warning on line 316 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ // Check for CI environments $nonInteractiveEnvs = [ - 'CI', 'GITHUB_ACTIONS', 'GITLAB_CI', 'JENKINS_URL',
'CI',
'GITHUB_ACTIONS',
'GITLAB_CI',
Expand All @@ -223,8 +321,8 @@
'TEAMCITY_VERSION',
];

foreach ($nonInteractiveEnvs as $env) {

Check warning on line 324 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "Foreach_": @@ @@ 'TEAMCITY_VERSION', ]; - foreach ($nonInteractiveEnvs as $env) { + foreach ([] as $env) { if ($this->getEnvVar($env) || $this->getServerVar($env)) { return false; }
if ($this->getEnvVar($env) || $this->getServerVar($env)) {

Check warning on line 325 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "LogicalOr": @@ @@ ]; foreach ($nonInteractiveEnvs as $env) { - if ($this->getEnvVar($env) || $this->getServerVar($env)) { + if ($this->getEnvVar($env) && $this->getServerVar($env)) { return false; } }
return false;
}
}
Expand Down Expand Up @@ -340,7 +438,7 @@
if (isset($this->secureEnvStorage[$var])) {
$this->cachedEnv[$var] = $this->secureEnvStorage[$var];
} else {
$globalEnv = filter_input_array(INPUT_ENV) ?: [];

Check warning on line 441 in src/Console/Command/AbstractCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (Infection)

Escaped Mutant for Mutator "Ternary": @@ @@ if (isset($this->secureEnvStorage[$var])) { $this->cachedEnv[$var] = $this->secureEnvStorage[$var]; } else { - $globalEnv = filter_input_array(INPUT_ENV) ?: []; + $globalEnv = filter_input_array(INPUT_ENV) ? [] : filter_input_array(INPUT_ENV); if (array_key_exists($var, $globalEnv)) { $this->cachedEnv[$var] = (string) $globalEnv[$var]; }
if (array_key_exists($var, $globalEnv)) {
$this->cachedEnv[$var] = (string) $globalEnv[$var];
}
Expand Down
72 changes: 71 additions & 1 deletion src/Console/Command/Theme/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OpenForgeProject\MageForge\Model\ThemeList;
use OpenForgeProject\MageForge\Model\ThemePath;
use OpenForgeProject\MageForge\Service\ThemeBuilder\BuilderPool;
use OpenForgeProject\MageForge\Service\ThemeParentResolver;
use OpenForgeProject\MageForge\Service\ThemeSuggester;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
Expand All @@ -28,12 +29,14 @@ class BuildCommand extends AbstractCommand
* @param ThemeList $themeList
* @param BuilderPool $builderPool
* @param ThemeSuggester $themeSuggester
* @param ThemeParentResolver $parentResolver
*/
public function __construct(
private readonly ThemePath $themePath,
private readonly ThemeList $themeList,
private readonly BuilderPool $builderPool,
private readonly ThemeSuggester $themeSuggester,
private readonly ThemeParentResolver $parentResolver,
) {
parent::__construct();
}
Expand All @@ -54,6 +57,7 @@ protected function configure(): void
'Theme codes to build (format: Vendor/theme, Vendor, ...)',
)
->setAliases(['frontend:build']);
$this->addParentThemeOptions();
}

/**
Expand Down Expand Up @@ -128,7 +132,73 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
}
}

return $this->processBuildThemes($themeCodes, $this->io, $output, $isVerbose);
// Correct theme names first so parents are resolved for the final theme codes
$validatedCodes = [];
$invalidCount = 0;
foreach ($themeCodes as $themeCode) {
$validated = $this->validateAndCorrectTheme($themeCode, $this->io, $output);
if ($validated === null) {
$invalidCount++;
continue;
}
$validatedCodes[] = $validated;
}

if ($validatedCodes === []) {
return Command::FAILURE;
}

$validatedCodes = $this->expandWithParents($input, $output, $validatedCodes);
$result = $this->processBuildThemes($validatedCodes, $this->io, $output, $isVerbose);

return $invalidCount > 0 ? Command::FAILURE : $result;
}

/**
* Prepend the parent themes of the selected themes, topmost ancestor first, without duplicates.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param array<string> $themeCodes
* @return array<string>
*/
private function expandWithParents(InputInterface $input, OutputInterface $output, array $themeCodes): array
{
$parentsByTheme = [];
$chains = [];
$missing = [];
foreach ($themeCodes as $themeCode) {
$parents = $this->parentResolver->getBuildableParents($themeCode);
$parentsByTheme[$themeCode] = $parents;
if ($parents !== []) {
$chains[$themeCode] = $this->parentResolver->getInheritanceChain($themeCode);
}
foreach ($parents as $parent) {
if (!in_array($parent, $themeCodes, true)) {
$missing[$parent] = true;
}
}
}

$includeMissing = $this->confirmParentThemes(
$input,
$output,
count($themeCodes) === 1 ? $themeCodes[array_key_first($themeCodes)] : 'Selected themes',
array_keys($missing),
$chains,
);

$result = [];
foreach ($themeCodes as $themeCode) {
foreach ($parentsByTheme[$themeCode] as $parent) {
if (in_array($parent, $themeCodes, true) || $includeMissing) {
$result[$parent] = true;
}
}
$result[$themeCode] = true;
}

return array_map('strval', array_keys($result));
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/Console/Command/Theme/WatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenForgeProject\MageForge\Model\ThemeList;
use OpenForgeProject\MageForge\Model\ThemePath;
use OpenForgeProject\MageForge\Service\ThemeBuilder\BuilderPool;
use OpenForgeProject\MageForge\Service\ThemeParentResolver;
use OpenForgeProject\MageForge\Service\ThemeSuggester;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,12 +26,14 @@ class WatchCommand extends AbstractCommand
* @param ThemeList $themeList
* @param ThemePath $themePath
* @param ThemeSuggester $themeSuggester
* @param ThemeParentResolver $parentResolver
*/
public function __construct(
private readonly BuilderPool $builderPool,
private readonly ThemeList $themeList,
private readonly ThemePath $themePath,
private readonly ThemeSuggester $themeSuggester,
private readonly ThemeParentResolver $parentResolver,
) {
parent::__construct();
}
Expand All @@ -48,6 +51,7 @@ protected function configure(): void
->addArgument('themeCode', InputArgument::OPTIONAL, 'Theme to watch (format: Vendor/theme)')
->addOption('theme', 't', InputOption::VALUE_OPTIONAL, 'Theme to watch (format: Vendor/theme)')
->setAliases(['frontend:watch']);
$this->addParentThemeOptions();
}

/**
Expand Down Expand Up @@ -121,6 +125,47 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
return self::FAILURE;
}

$parents = $this->parentResolver->getBuildableParents($themeCode);
if ($this->confirmParentThemes($input, $output, $themeCode, $parents, [
$themeCode => $this->parentResolver->getInheritanceChain($themeCode),
])) {
foreach ($parents as $parent) {
if (!$this->buildParent($parent, $output, $isVerbose)) {
return self::FAILURE;
}
}
}

return $builder->watch($themeCode, $themePath, $this->io, $output, $isVerbose) ? self::SUCCESS : self::FAILURE;
}

/**
* Build a parent theme once before watching its child.
*
* @param string $parentCode
* @param OutputInterface $output
* @param bool $isVerbose
* @return bool
*/
private function buildParent(string $parentCode, OutputInterface $output, bool $isVerbose): bool
{
$parentPath = $this->themePath->getPath($parentCode);
$parentBuilder = $parentPath === null ? null : $this->builderPool->getBuilder($parentPath);
if ($parentPath === null || $parentBuilder === null) {
$this->io->error("Cannot build parent theme $parentCode.");
return false;
}

$this->io->section(sprintf(
'Building parent theme %s using %s builder',
$parentCode,
$parentBuilder->getName(),
));
if (!$parentBuilder->build($parentCode, $parentPath, $this->io, $output, $isVerbose)) {
$this->io->error("Failed to build parent theme $parentCode.");
return false;
}

return true;
}
}
21 changes: 21 additions & 0 deletions src/Model/ThemeAncestor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace OpenForgeProject\MageForge\Model;

/**
* An ancestor in a theme's inheritance chain and why it is not built (if applicable)
*/
class ThemeAncestor
{
/**
* @param string $code
* @param string|null $skipReason
*/
public function __construct(
public readonly string $code,
public readonly ?string $skipReason = null,
) {
}
}
Loading
Loading