Skip to content

use symfony process to run shell command #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is taken from the Process exception. I didn't want to expose a Symfony exception here as it's an implementation detail.

$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.