Skip to content

Commit 5a9891d

Browse files
committed
feat: refactor Grunt task execution and integrate into theme builder
1 parent 50eaabf commit 5a9891d

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/Service/GruntTaskRunner.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,26 @@ public function runTasks(
2323
bool $isVerbose
2424
): bool {
2525
try {
26-
foreach (['clean', 'less'] as $task) {
27-
$shellOutput = $this->shell->execute(self::GRUNT_PATH . ' ' . $task . ' --quiet');
28-
if ($isVerbose) {
29-
$output->writeln($shellOutput);
30-
$io->success("'grunt $task' has been successfully executed.");
31-
}
26+
if ($isVerbose) {
27+
$io->text('Running grunt clean...');
28+
$this->shell->execute(self::GRUNT_PATH . ' clean');
29+
} else {
30+
$this->shell->execute(self::GRUNT_PATH . ' clean --quiet');
31+
}
32+
33+
if ($isVerbose) {
34+
$io->text('Running grunt less...');
35+
$this->shell->execute(self::GRUNT_PATH . ' less');
36+
} else {
37+
$this->shell->execute(self::GRUNT_PATH . ' less --quiet');
38+
}
39+
40+
if ($isVerbose) {
41+
$io->success('Grunt tasks completed successfully.');
3242
}
3343
return true;
3444
} catch (\Exception $e) {
35-
$io->error($e->getMessage());
45+
$io->error('Failed to run grunt tasks: ' . $e->getMessage());
3646
return false;
3747
}
3848
}

src/Service/ThemeBuilder/MagentoStandard/Builder.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Magento\Framework\Filesystem\Driver\File;
88
use Magento\Framework\Shell;
99
use OpenForgeProject\MageForge\Service\CacheCleaner;
10+
use OpenForgeProject\MageForge\Service\GruntTaskRunner;
1011
use OpenForgeProject\MageForge\Service\NodePackageManager;
1112
use OpenForgeProject\MageForge\Service\StaticContentCleaner;
1213
use OpenForgeProject\MageForge\Service\StaticContentDeployer;
@@ -26,7 +27,8 @@ public function __construct(
2627
private readonly StaticContentCleaner $staticContentCleaner,
2728
private readonly CacheCleaner $cacheCleaner,
2829
private readonly SymlinkCleaner $symlinkCleaner,
29-
private readonly NodePackageManager $nodePackageManager
30+
private readonly NodePackageManager $nodePackageManager,
31+
private readonly GruntTaskRunner $gruntTaskRunner
3032
) {
3133
}
3234

@@ -56,37 +58,7 @@ public function build(string $themeCode, string $themePath, SymfonyStyle $io, Ou
5658
if ($this->isVendorTheme($themePath)) {
5759
$io->warning('Vendor theme detected. Skipping Grunt steps.');
5860
} elseif ($this->hasNodeSetup()) {
59-
// Check if Node/Grunt setup exists
60-
if (!$this->autoRepair($themePath, $io, $output, $isVerbose)) {
61-
return false;
62-
}
63-
64-
// Clean symlinks in web/css/ directory before build
65-
if (!$this->symlinkCleaner->cleanSymlinks($themePath, $io, $isVerbose)) {
66-
return false;
67-
}
68-
69-
// Run grunt tasks
70-
try {
71-
if ($isVerbose) {
72-
$io->text('Running grunt clean...');
73-
$this->shell->execute('node_modules/.bin/grunt clean');
74-
} else {
75-
$this->shell->execute('node_modules/.bin/grunt clean --quiet');
76-
}
77-
78-
if ($isVerbose) {
79-
$io->text('Running grunt less...');
80-
$this->shell->execute('node_modules/.bin/grunt less');
81-
} else {
82-
$this->shell->execute('node_modules/.bin/grunt less --quiet');
83-
}
84-
85-
if ($isVerbose) {
86-
$io->success('Grunt tasks completed successfully.');
87-
}
88-
} catch (\Exception $e) {
89-
$io->error('Failed to run grunt tasks: ' . $e->getMessage());
61+
if (!$this->processNodeSetup($themePath, $io, $output, $isVerbose)) {
9062
return false;
9163
}
9264
} else {
@@ -108,6 +80,29 @@ public function build(string $themeCode, string $themePath, SymfonyStyle $io, Ou
10880
return true;
10981
}
11082

83+
/**
84+
* Process Node.js and Grunt setup
85+
*/
86+
private function processNodeSetup(
87+
string $themePath,
88+
SymfonyStyle $io,
89+
OutputInterface $output,
90+
bool $isVerbose
91+
): bool {
92+
// Check if Node/Grunt setup exists
93+
if (!$this->autoRepair($themePath, $io, $output, $isVerbose)) {
94+
return false;
95+
}
96+
97+
// Clean symlinks in web/css/ directory before build
98+
if (!$this->symlinkCleaner->cleanSymlinks($themePath, $io, $isVerbose)) {
99+
return false;
100+
}
101+
102+
// Run grunt tasks
103+
return $this->gruntTaskRunner->runTasks($io, $output, $isVerbose);
104+
}
105+
111106
public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool
112107
{
113108
$rootPath = '.';

0 commit comments

Comments
 (0)