Skip to content

Commit

Permalink
Add Exec command (#2444)
Browse files Browse the repository at this point in the history
* Add Exec command

* Implement Process component

* Handle the output of exec

* Use shellprocess

* Update parameters for exec

* Improve the comments

* Fix duplication
  • Loading branch information
Saphyel authored and jmolivas committed Jun 26, 2016
1 parent caebeb7 commit 85e3070
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 27 deletions.
5 changes: 5 additions & 0 deletions config/services/exec.yml
@@ -0,0 +1,5 @@
services:
exec:
class: Drupal\Console\Command\Exec\ExecCommand
tags:
- { name: console.command }
8 changes: 8 additions & 0 deletions config/translations/en/exec.yml
@@ -0,0 +1,8 @@
description: 'Execute an external command.'
arguments:
bin: 'Executable name.'
messages:
success: 'Executed the command.'
invalid-bin: 'Error executing the command.'
missing-bin: 'Provide a executable.'

75 changes: 75 additions & 0 deletions src/Command/Exec/ExecCommand.php
@@ -0,0 +1,75 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Exec\ExecCommand.
*/

namespace Drupal\Console\Command\Exec;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;

/**
* Class ExecCommand
* @package Drupal\Console\Command\Exec
*/
class ExecCommand extends Command
{
use ContainerAwareCommandTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('exec')
->setDescription($this->trans('commands.exec.description'))
->addArgument(
'bin',
InputArgument::REQUIRED,
$this->trans('commands.exec.arguments.bin')
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$bin = $input->getArgument('bin');

if (!$bin) {
$io->error(
$this->trans('commands.exec.messages.missing-bin')
);
return 1;
}

$shellProcess = $this->get('shell_process');
if ($shellProcess->exec($bin, TRUE)) {
$io->success(
sprintf(
$this->trans('commands.exec.messages.success'),
$bin
)
);
}
else {
$io->error(
sprintf(
$this->trans('commands.exec.messages.invalid-bin')
)
);
return 1;
}

}

}
53 changes: 26 additions & 27 deletions src/Utils/ShellProcess.php
Expand Up @@ -29,14 +29,13 @@ class ShellProcess
/* @var Progress */
protected $progress;

/**
* @var ShellProcess
*/
/* @var ShellProcess */
protected $process;

/**
* Process constructor.
* @param Site $site
* @param Config $config
*/
public function __construct(Site $site, Config $config)
{
Expand All @@ -52,13 +51,14 @@ public function __construct(Site $site, Config $config)
}

/**
* @param $command
* @param $command string
* @param $show_output boolean
*
* @throws ProcessFailedException
*
* @return Process
*/
public function exec($command)
public function exec($command, $show_output = FALSE)
{
$rootPath = $this->site->getRoot();

Expand All @@ -67,25 +67,24 @@ public function exec($command)
$this->process->enableOutput();
$this->process->setTimeout(null);

if ($this->shellexec_output) {
$this->process->run(function ($type, $buffer) {
$this->output->writeln(
sprintf(
"<info>%s</info>",
$buffer
)
);
});
}else{
$this->progress->start();
$this->process->start();

while ($this->process->isRunning()) {
$this->advance();
}
$this->progress->finish();
if ($this->shellexec_output || $show_output) {
$this->process->run(function ($type, $buffer) {
$this->output->writeln(
sprintf('<info>%s</info>', $buffer)
);
});
}
else {
$this->progress->start();
$this->process->start();

while ($this->process->isRunning()) {
$this->advance();
}

$this->progress->finish();
$this->output->writeln("");
}

if (!$this->process->isSuccessful()) {
throw new ProcessFailedException($this->process);
Expand All @@ -94,16 +93,16 @@ public function exec($command)
return $this->process->isSuccessful();
}

private function advance() {
usleep(300000);
$this->progress->advance();
}

/**
* @return string
*/
public function getOutput()
{
return $this->process->getOutput();
}

private function advance() {
usleep(300000);
$this->progress->advance();
}
}

0 comments on commit 85e3070

Please sign in to comment.