-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCleanCommand.php
More file actions
445 lines (386 loc) · 13.8 KB
/
CleanCommand.php
File metadata and controls
445 lines (386 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
declare(strict_types=1);
namespace OpenForgeProject\MageForge\Console\Command\Theme;
use Laravel\Prompts\MultiSearchPrompt;
use Magento\Framework\Console\Cli;
use OpenForgeProject\MageForge\Console\Command\AbstractCommand;
use OpenForgeProject\MageForge\Model\ThemeList;
use OpenForgeProject\MageForge\Model\ThemePath;
use OpenForgeProject\MageForge\Service\ThemeCleaner;
use OpenForgeProject\MageForge\Service\ThemeSuggester;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command for cleaning static files and preprocessed view files for specific themes
*/
class CleanCommand extends AbstractCommand
{
/**
* @param ThemeCleaner $themeCleaner
* @param ThemeList $themeList
* @param ThemePath $themePath
* @param ThemeSuggester $themeSuggester
*/
public function __construct(
private readonly ThemeCleaner $themeCleaner,
private readonly ThemeList $themeList,
private readonly ThemePath $themePath,
private readonly ThemeSuggester $themeSuggester,
) {
parent::__construct();
}
/**
* Configure command.
*
* @return void
*/
protected function configure(): void
{
$this
->setName($this->getCommandName('theme', 'clean'))
->setDescription('Clean theme static files and cache directories')
->addArgument(
'themeCodes',
InputArgument::IS_ARRAY,
'Theme codes to clean (format: Vendor/theme, Vendor/theme 2, ...)',
)
->addOption('all', 'a', InputOption::VALUE_NONE, 'Clean all themes')
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Show what would be cleaned without actually deleting anything',
)
->setAliases(['frontend:clean']);
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$dryRun = $input->getOption('dry-run');
if ($dryRun) {
$this->io->note('DRY RUN MODE: No files will be deleted');
}
$themeCodes = $this->resolveThemeCodes($input, $output);
if ($themeCodes === null) {
return Cli::RETURN_SUCCESS;
}
[$totalCleaned, $failedThemes] = $this->processThemes($themeCodes, $dryRun, $output);
$this->displaySummary($themeCodes, $totalCleaned, $failedThemes, $dryRun);
return Cli::RETURN_SUCCESS;
}
/**
* Resolve which themes to clean based on input
*
* @param InputInterface $input
* @param OutputInterface $output
* @return array<string>|null Array of theme codes or null to exit
*/
private function resolveThemeCodes(InputInterface $input, OutputInterface $output): ?array
{
$themeCodes = $input->getArgument('themeCodes');
$cleanAll = $input->getOption('all');
if ($cleanAll) {
return $this->getAllThemeCodes();
}
if (empty($themeCodes)) {
return $this->selectThemesInteractively($output);
}
return $themeCodes;
}
/**
* Get all theme codes
*
* @return array<string>|null
*/
private function getAllThemeCodes(): ?array
{
$themes = $this->themeList->getAllThemes();
$themeCodes = array_values(array_map(fn($theme) => $theme->getCode(), $themes));
if (empty($themeCodes)) {
$this->io->info('No themes found.');
return null;
}
$this->io->info(sprintf('Cleaning all %d theme%s...', count($themeCodes), count($themeCodes) === 1 ? '' : 's'));
return $themeCodes;
}
/**
* Select themes interactively
*
* @param OutputInterface $output
* @return array<string>|null
*/
private function selectThemesInteractively(OutputInterface $output): ?array
{
$themes = $this->themeList->getAllThemes();
$options = array_values(array_map(fn($theme) => $theme->getCode(), $themes));
if (!$this->isInteractiveTerminal($output)) {
$this->displayAvailableThemes($themes);
return null;
}
return $this->promptForThemes($options, $themes);
}
/**
* Display available themes for non-interactive environments
*
* @param array<mixed> $themes
* @return void
*/
private function displayAvailableThemes(array $themes): void
{
$this->io->warning('No theme specified. Available themes:');
if (empty($themes)) {
$this->io->info('No themes found.');
return;
}
foreach ($themes as $theme) {
$this->io->writeln(sprintf(' - <fg=cyan>%s</> (%s)', $theme->getCode(), $theme->getThemeTitle()));
}
$this->io->newLine();
$this->io->info('Usage: bin/magento mageforge:theme:clean <theme-code> [<theme-code>...]');
$this->io->info(' bin/magento mageforge:theme:clean --all');
$this->io->info('Example: bin/magento mageforge:theme:clean Magento/luma');
}
/**
* Prompt user to select themes
*
* @param array<string> $options
* @param array<mixed> $themes
* @return array<string>|null
*/
private function promptForThemes(array $options, array $themes): ?array
{
$this->setPromptEnvironment();
$themeCodesPrompt = new MultiSearchPrompt(
label: 'Select themes to clean',
options: fn(string $value) => empty($value)
? $options
: array_values(array_filter($options, fn($option) => stripos((string)$option, $value) !== false)),
placeholder: 'Type to search theme...',
hint: 'Type to search, arrow keys to navigate, Space to toggle, Enter to confirm',
required: false,
);
try {
$themeCodes = $themeCodesPrompt->prompt();
\Laravel\Prompts\Prompt::terminal()->restoreTty();
$this->resetPromptEnvironment();
if (empty($themeCodes)) {
$this->io->info('No themes selected.');
return null;
}
return $themeCodes;
} catch (\Exception $e) {
$this->resetPromptEnvironment();
$this->io->error('Interactive mode failed: ' . $e->getMessage());
$this->displayAvailableThemes($themes);
return null;
}
}
/**
* Process cleaning for all selected themes
*
* @param array<string> $themeCodes
* @param bool $dryRun
* @param OutputInterface $output
* @return array<int, mixed> [totalCleaned, failedThemes]
*/
private function processThemes(array $themeCodes, bool $dryRun, OutputInterface $output): array
{
$totalThemes = count($themeCodes);
$totalCleaned = 0;
$failedThemes = [];
foreach ($themeCodes as $index => $themeName) {
$currentTheme = $index + 1;
// Validate and potentially correct theme name
$validatedTheme = $this->validateTheme($themeName, $failedThemes, $output);
if ($validatedTheme === null) {
continue;
}
// Use validated/corrected theme name
$this->displayThemeHeader($validatedTheme, $currentTheme, $totalThemes);
$cleaned = $this->cleanThemeDirectories($validatedTheme, $dryRun);
$this->displayThemeResult($validatedTheme, $cleaned, $dryRun);
$totalCleaned += $cleaned;
}
return [$totalCleaned, $failedThemes];
}
/**
* Validate theme exists
*
* @param string $themeName
* @param array<string> $failedThemes
* @param OutputInterface $output
* @return string|null Theme code if valid or corrected, null if invalid
*/
private function validateTheme(string $themeName, array &$failedThemes, OutputInterface $output): ?string
{
$themePath = $this->themePath->getPath($themeName);
if ($themePath === null) {
// Try to suggest similar themes
$correctedTheme = $this->handleInvalidThemeWithSuggestions($themeName, $this->themeSuggester, $output);
// If no theme was selected, mark as failed
if ($correctedTheme === null) {
$failedThemes[] = $themeName;
return null;
}
// Use the corrected theme code
$themePath = $this->themePath->getPath($correctedTheme);
// Double-check the corrected theme exists
if ($themePath === null) {
$this->io->error(sprintf("Theme '%s' not found.", $correctedTheme));
$failedThemes[] = $themeName;
return null;
}
$this->io->info("Using theme: $correctedTheme");
return $correctedTheme;
}
return $themeName;
}
/**
* Display header for theme being cleaned
*
* @param string $themeName
* @param int $currentTheme
* @param int $totalThemes
* @return void
*/
private function displayThemeHeader(string $themeName, int $currentTheme, int $totalThemes): void
{
if ($totalThemes > 1) {
$this->io->section(sprintf('Cleaning theme %d of %d: %s', $currentTheme, $totalThemes, $themeName));
} else {
$this->io->section(sprintf('Cleaning static files for theme: %s', $themeName));
}
}
/**
* Clean all directories for a theme
*
* @param string $themeName
* @param bool $dryRun
* @return int Number of directories cleaned
*/
private function cleanThemeDirectories(string $themeName, bool $dryRun): int
{
static $globalCleaned = false;
$cleaned = 0;
$cleaned += $this->themeCleaner->cleanViewPreprocessed($themeName, $this->io, $dryRun, true);
$cleaned += $this->themeCleaner->cleanPubStatic($themeName, $this->io, $dryRun, true);
if (!$globalCleaned) {
$cleaned += $this->themeCleaner->cleanPageCache($this->io, $dryRun, true);
$cleaned += $this->themeCleaner->cleanVarTmp($this->io, $dryRun, true);
$cleaned += $this->themeCleaner->cleanGenerated($this->io, $dryRun, true);
$globalCleaned = true;
}
return $cleaned;
}
/**
* Display result for individual theme
*
* @param string $themeName
* @param int $cleaned
* @param bool $dryRun
* @return void
*/
private function displayThemeResult(string $themeName, int $cleaned, bool $dryRun): void
{
if ($cleaned > 0) {
$action = $dryRun ? 'Would clean' : 'Cleaned';
$this->io->writeln(sprintf(
" <fg=green>✓</> %s %d director%s for theme '%s'",
$action,
$cleaned,
$cleaned === 1 ? 'y' : 'ies',
$themeName,
));
} else {
$this->io->writeln(sprintf(" <fg=yellow>ℹ</> No files to clean for theme '%s'", $themeName));
}
}
/**
* Display summary of cleaning operation
*
* @param array<string> $themeCodes
* @param int $totalCleaned
* @param array<string> $failedThemes
* @param bool $dryRun
* @return void
*/
private function displaySummary(array $themeCodes, int $totalCleaned, array $failedThemes, bool $dryRun): void
{
$this->io->newLine();
$totalThemes = count($themeCodes);
if ($totalThemes === 1) {
$this->displaySingleThemeSummary($themeCodes[0], $totalCleaned, $dryRun);
} else {
$this->displayMultiThemeSummary($totalThemes, $totalCleaned, $failedThemes, $dryRun);
}
}
/**
* Display summary for single theme
*
* @param string $themeCode
* @param int $totalCleaned
* @param bool $dryRun
* @return void
*/
private function displaySingleThemeSummary(string $themeCode, int $totalCleaned, bool $dryRun): void
{
if ($totalCleaned > 0) {
$action = $dryRun ? 'Would clean' : 'Successfully cleaned';
$this->io->success(sprintf(
"%s %d director%s for theme '%s'",
$action,
$totalCleaned,
$totalCleaned === 1 ? 'y' : 'ies',
$themeCode,
));
} else {
$this->io->info(sprintf("No files to clean for theme '%s'", $themeCode));
}
}
/**
* Display summary for multiple themes
*
* @param int $totalThemes
* @param int $totalCleaned
* @param array<string> $failedThemes
* @param bool $dryRun
* @return void
*/
private function displayMultiThemeSummary(
int $totalThemes,
int $totalCleaned,
array $failedThemes,
bool $dryRun,
): void {
$successCount = $totalThemes - count($failedThemes);
if ($successCount > 0 && $totalCleaned > 0) {
$action = $dryRun ? 'Would clean' : 'Successfully cleaned';
$this->io->success(sprintf(
'%s %d director%s across %d theme%s',
$action,
$totalCleaned,
$totalCleaned === 1 ? 'y' : 'ies',
$successCount,
$successCount === 1 ? '' : 's',
));
} else {
$this->io->info('No files were cleaned.');
}
if (!empty($failedThemes)) {
$this->io->warning(sprintf(
'Failed to process %d theme%s: %s',
count($failedThemes),
count($failedThemes) === 1 ? '' : 's',
implode(', ', $failedThemes),
));
}
}
}