Commit a975d571 authored by Jon Pugh's avatar Jon Pugh

Much improved ServerContext::shell_exec() output: Adding our own SymfonyStyle class ProvisionStyle.

parent 7f669dea
<?php
namespace Aegir\Provision\Console;
use Drupal\Console\Core\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
class ProvisionStyle extends DrupalStyle {
/**
* @var BufferedOutput
*/
protected $bufferedOutput;
protected $input;
protected $lineLength;
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
$this->lineLength = min($width - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
parent::__construct($input, $output);
}
public function commandBlock($message) {
$this->autoPrependBlock();
$this->customLite($message, '<fg=yellow>$</>', '');
}
public function outputBlock($message) {
$this->block(
$message,
NULL,
'fg=yellow;bg=black',
' ╎ ',
TRUE
);
}
/**
* Replacement for parent::autoPrependBlock(), allowing access and setting newLine to 1 - instead of 2 -.
*/
private function autoPrependBlock()
{
$chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
if (!isset($chars[0])) {
return $this->newLine(); //empty history, so we should start with a new line.
}
//Prepend new line for each non LF chars (This means no blank line was output before)
$this->newLine(1 - substr_count($chars, "\n"));
}
}
\ No newline at end of file
......@@ -112,16 +112,21 @@ class ServerContext extends ContextProvider implements ConfigurationInterface
$effective_wd = $dir? $dir:
$this->getProperty('server_config_path');
$this->getProvision()->getLogger()->info('Running command [{command}] in directory [{dir}]', [
'command' => $cmd,
'dir' => $effective_wd,
]);
if ($this->getProvision()->getOutput()->isVerbose()) {
$this->getProvision()->io()->commandBlock($cmd);
}
chdir($effective_wd);
exec($cmd, $output, $exit);
chdir($cwd);
$response = implode("\n", $output);
if (!empty($output)){
if ($this->getProvision()->getOutput()->isVerbose()) {
$this->getProvision()->io()->outputBlock($output);
}
}
if ($exit != ResultData::EXITCODE_OK) {
throw new \Exception("Command failed: $cmd");
......
......@@ -7,6 +7,7 @@ use Aegir\Provision\Console\Config;
use Aegir\Provision\Commands\ExampleCommands;
use Aegir\Provision\Console\ConsoleOutput;
use Aegir\Provision\Console\ProvisionStyle;
use Aegir\Provision\Robo\ProvisionCollectionBuilder;
use Aegir\Provision\Robo\ProvisionExecutor;
use Aegir\Provision\Robo\ProvisionTasks;
......@@ -264,12 +265,12 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
/**
* Provide access to DrupalStyle object.
*
* @return \Drupal\Console\Core\Style\DrupalStyle
* @return ProvisionStyle
*/
public function io()
{
if (!$this->io) {
$this->io = new DrupalStyle($this->input(), $this->output());
$this->io = new ProvisionStyle($this->input(), $this->output());
}
return $this->io;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment