Skip to content

Commit

Permalink
use symfony process to run shell command
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkwiecinski committed Jul 9, 2024
1 parent 6d9b270 commit 4317099
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
55 changes: 32 additions & 23 deletions Css/PreProcessor/Adapter/Less/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\View\Asset\File as AssetFile;
use Magento\Framework\View\Asset\Source;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

class Processor implements ContentProcessorInterface
{
Expand Down Expand Up @@ -93,29 +94,37 @@ public function processContent(AssetFile $asset)
*/
protected function compileFile($filePath)
{
$nodeCmdArgs = $this->getNodeArgsAsArray();
$lessCmdArgs = $this->getCompilerArgsAsArray();

$cmd = '%s ' . str_repeat(' %s', count($nodeCmdArgs)) . ' %s' . str_repeat(' %s', count($lessCmdArgs)) . ' %s';
$arguments = [];
$arguments[] = $this->getPathToNodeBinary();
$arguments = array_merge($arguments, $nodeCmdArgs);
$arguments[] = $this->getPathToLessCompiler();
$arguments = array_merge($arguments, $lessCmdArgs);
$arguments[] = $filePath;

// to log or not to log, that's the question
// also, it would be better to use the logger in the Shell class,
// since that one will contain the exact correct command, and not this sprintf version
// $logArguments = array_map('escapeshellarg', $arguments);
// $this->logger->debug('Less compilation command: `'
// . sprintf($cmd, ...$logArguments)
// . '`');

return $this->shell->execute(
$cmd,
$arguments
);
$process = new Process([
$this->getPathToNodeBinary(),
...$this->getNodeArgsAsArray(),
$this->getPathToLessCompiler(),
...$this->getCompilerArgsAsArray(),
$filePath,
]);

$process->run();

if (!$process->isSuccessful()) {
$error = sprintf(
'The command "%s" failed.'."\n\nExit Code: %s(%s)\n\nWorking directory: %s",
$process->getCommandLine(),
$process->getExitCode(),
$process->getExitCodeText(),
$process->getWorkingDirectory()
);

if (!$process->isOutputDisabled()) {
$error .= sprintf(
"\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s",
$process->getOutput(),
$process->getErrorOutput()
);
}

throw new LocalizedException(__($error));
}

return $process->getOutput();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "~5.5.0 || ~5.6.0 || ~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"magento/framework": "^100.0.9 || ^101.0.0 || ^102.0.0 || ^103.0.0",
"magento/module-developer": "^100.0.5"
"magento/module-developer": "^100.0.5",
"symfony/process": "^4.4|^5.0|^6.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4317099

Please sign in to comment.