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
29 changes: 28 additions & 1 deletion docs/commands_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ Complete reference of all CLI commands provided by the MageForge module.

---

## Machine-readable output

These commands accept `--format=json` (or the shortcut `--json`) for scripts, CI and AI agents: `theme:list`, `theme:info`, `theme:build`, `template:obsolete`, `dependencies:update`, `hyva:compatibility:check`, `system:version` and `system:check`.

- stdout contains only the JSON document. Progress, warnings and errors go to stderr.
- Prompts are never shown in JSON mode, so pass all arguments explicitly.
- On failure the exit code is non-zero and stdout contains `{"error": "<message>"}`.
- An unknown `--format` value fails with an error.

```bash
bin/magento mageforge:theme:list --format=json
bin/magento mageforge:theme:build Magento/luma --json
```

Top-level structure per command:

- `theme:list` — array of `{code, title, area, path}`.
- `theme:info` — object with the theme details (same keys as the table view).
- `theme:build` — `{success, duration_seconds, built: [{theme, builder, hyva_version}], failed: [codes]}`.
- `template:obsolete` — array of `{theme, template, file, reason, details, safe_to_delete}`.
- `dependencies:update` — `{dry_run, duration_seconds, updated, skipped, failed}`.
- `hyva:compatibility:check` — `{summary, modules: [{name, version, status, hyva_aware, files}], tailwind4}`; `tailwind4` is `null` when the readiness check did not run.
- `system:version` — `{module_version, latest_version, update_available}`; unknown values are `null`.
- `system:check` — `{php, composer, node, npm, git, database, xdebug, redis, search_engine, os, disk_space, magento, php_extensions}`.

---

## Theme Commands

### `mageforge:theme:list`
Expand All @@ -44,7 +71,7 @@ Shows details about a theme: area, path, Composer package and version, parent ch

```bash
bin/magento mageforge:theme:info [<theme-code>]
bin/magento mageforge:theme:info Magento/luma --json
bin/magento mageforge:theme:info Magento/luma --format=json
```

### `mageforge:theme:build`
Expand Down
161 changes: 160 additions & 1 deletion src/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

Expand All @@ -28,11 +30,29 @@
*/
protected const COMMAND_PREFIX = 'mageforge';

protected const FORMAT_TABLE = 'table';
protected const FORMAT_JSON = 'json';

/**
* @var SymfonyStyle
*/
protected SymfonyStyle $io;

/**
* @var bool Whether the current run produces JSON on stdout
*/
private bool $jsonOutput = false;

/**
* @var bool Whether a JSON document was already written during the current run
*/
private bool $jsonWritten = false;

/**
* @var OutputInterface Real standard output, kept separate from $io which logs to stderr in JSON mode
*/
private OutputInterface $stdout;

/**
* @var array<string, string|null>
*/
Expand Down Expand Up @@ -69,14 +89,148 @@
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->stdout = $output;
$this->jsonWritten = false;

$format = $this->resolveFormat($input);
if ($format === null) {
$this->io = new SymfonyStyle($input, $output);
$this->io->error(sprintf(
"Invalid format '%s'. Use '%s' or '%s'.",
$this->getFormatOptionValue($input),
self::FORMAT_TABLE,
self::FORMAT_JSON,
));
return Cli::RETURN_FAILURE;
}

$this->jsonOutput = $format === self::FORMAT_JSON;
if ($this->jsonOutput) {
// Keep stdout reserved for the JSON document; progress and messages go to stderr
$output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : new NullOutput();
}

$this->io = new SymfonyStyle($input, $output);

try {
return $this->executeCommand($input, $output);
$result = $this->executeCommand($input, $output);
} catch (\Exception $e) {
$this->io->error($e->getMessage());
$this->writeJsonError($e->getMessage());
return Cli::RETURN_FAILURE;
}

if ($result !== Cli::RETURN_SUCCESS) {
$this->writeJsonError('The command failed. See stderr for details.');
}

return $result;
}

/**
* Add the --format and --json options for machine-readable output
*
* @return void
*/
protected function addFormatOption(): void
{
$this->addOption(
'format',
null,
InputOption::VALUE_REQUIRED,
sprintf('Output format: %s or %s', self::FORMAT_TABLE, self::FORMAT_JSON),
self::FORMAT_TABLE,
)->addOption('json', null, InputOption::VALUE_NONE, 'Shortcut for --format=json');
}

/**
* Check whether the current run outputs JSON
*
* @return bool
*/
protected function isJsonOutput(): bool
{
return $this->jsonOutput;
}

/**
* Write a JSON document to stdout
*
* @param mixed $data
* @return void
*/
protected function writeJson(mixed $data): void
{
$this->stdout->writeln(
json_encode(
$data,
JSON_PRETTY_PRINT
| JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
| JSON_THROW_ON_ERROR
| JSON_INVALID_UTF8_SUBSTITUTE,
),
OutputInterface::OUTPUT_RAW,
);
$this->jsonWritten = true;
}

/**
* Round a duration for JSON output
*
* @param float $seconds
* @return float
*/
protected function roundSeconds(float $seconds): float
{
return round($seconds, 2);
}

/**
* Write a JSON error document to stdout when running in JSON mode
*
* @param string $message
* @return void
*/
protected function writeJsonError(string $message): void
{
if ($this->jsonOutput && !$this->jsonWritten) {
$this->writeJson(['error' => $message]);
}
}

/**
* Resolve the requested output format
*
* @param InputInterface $input
* @return string|null The format, or null if it is invalid
*/
private function resolveFormat(InputInterface $input): ?string
{
$definition = $this->getDefinition();
if (!$definition->hasOption('format')) {
return self::FORMAT_TABLE;
}

$format = $this->getFormatOptionValue($input);
if (!in_array($format, [self::FORMAT_TABLE, self::FORMAT_JSON], true)) {
return null;
}

return $definition->hasOption('json') && $input->getOption('json') ? self::FORMAT_JSON : $format;
}

/**
* Get the raw value of the --format option
*
* @param InputInterface $input
* @return string
*/
private function getFormatOptionValue(InputInterface $input): string
{
$format = $input->getOption('format');

return is_string($format) ? $format : self::FORMAT_TABLE;
}

/**
Expand Down Expand Up @@ -234,7 +388,7 @@
array $parents,
array $chains = [],
): bool {
if ($parents === [] || $input->getOption('no-parents')) {

Check warning on line 391 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;
}

Expand Down Expand Up @@ -302,6 +456,11 @@
*/
protected function isInteractiveTerminal(OutputInterface $output): bool
{
// Prompts would corrupt the JSON document and cannot be answered by agents
if ($this->jsonOutput) {
return false;
}

// Check if output supports ANSI
if (!$output->isDecorated()) {
return false;
Expand Down
46 changes: 45 additions & 1 deletion src/Console/Command/Dependencies/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function configure(): void
)
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show outdated packages without changing anything')
->setAliases(['dependencies:update']);
$this->addFormatOption();
}

/**
Expand All @@ -81,14 +82,29 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
$themeCodes = $this->resolveThemeCodes($input, $output);

if ($themeCodes === null) {
if ($this->isJsonOutput()) {
return $this->writeEmptyJsonResult($input, $dryRun);
}

return Cli::RETURN_SUCCESS;
}

$startTime = microtime(true);

[$updatedThemes, $skippedThemes, $failedThemes] = $this->processThemes($themeCodes, $latest, $dryRun, $output);

$this->displaySummary($updatedThemes, $skippedThemes, $failedThemes, $dryRun, microtime(true) - $startTime);
$duration = microtime(true) - $startTime;
if ($this->isJsonOutput()) {
$this->writeJson([
'dry_run' => $dryRun,
'duration_seconds' => $this->roundSeconds($duration),
'updated' => $updatedThemes,
'skipped' => $skippedThemes,
'failed' => $failedThemes,
]);
} else {
$this->displaySummary($updatedThemes, $skippedThemes, $failedThemes, $dryRun, $duration);
}

if (!empty($failedThemes)) {
return Cli::RETURN_FAILURE;
Expand All @@ -97,6 +113,34 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
return Cli::RETURN_SUCCESS;
}

/**
* Write the JSON result for runs that selected no themes
*
* Wildcards that matched nothing are a successful no-op; omitting the theme codes is an error.
*
* @param InputInterface $input
* @param bool $dryRun
* @return int
*/
private function writeEmptyJsonResult(InputInterface $input, bool $dryRun): int
{
if ($input->getArgument('themeCodes') === []) {
$this->writeJsonError('No themes specified. Pass one or more theme codes, e.g. Magento/luma.');

return Cli::RETURN_FAILURE;
}

$this->writeJson([
'dry_run' => $dryRun,
'duration_seconds' => 0.0,
'updated' => [],
'skipped' => [],
'failed' => [],
]);

return Cli::RETURN_SUCCESS;
}

/**
* Resolve which themes to update based on input
*
Expand Down
Loading
Loading