Skip to content
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

Add RunInCurrentProcess task and enable in CollectionFactory, fixes #142, obsoletes #125 #159

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@ commands:
- { task: "symlink", from: "../../custom/themes", to: "${drupal.root}/themes/custom" }
- { task: "run", command: "drupal:drush-setup" }
- { task: "run", command: "drupal:settings-setup" }
- { task: "run", command: "setup:behat" }
- { task: "run", command: "setup:behat", "in-current-process": true }
- "./vendor/bin/drush --root=$(pwd)/${drupal.root} cr"
setup:behat:
- { task: "process", source: "behat.yml.dist", destination: "behat.yml" }
```

Commands can reference each-other, allowing for complex scenarios to be implemented with relative ease.

In this example, the `setup:behat` task will be executed in the current process, while the other tasks run in a new runner instance launched via command line.

At the moment the following tasks are supported (optional argument default values in parenthesis):

| Task | Task | Arguments |
Expand All @@ -211,7 +213,7 @@ At the moment the following tasks are supported (optional argument default value
| `process-php` | `taskAppendConfiguration()` | `type: append`, `config`, `source`, `destination`, `override` (false) |
| `process-php` | `taskPrependConfiguration()` | `type: prepend`, `config`, `source`, `destination`, `override` (false) |
| `process-php` | `taskWriteConfiguration()` | `type: write`, `config`, `source`, `destination`, `override` (false) |
| `run` | `taskExec()` | `command`, `arguments`, `options` (will run `./vendor/bin/run [command] [argument1] [argument2] ... --[option1]=[value1] --[option2]=[value2] ...`) |
| `run` | `taskExec()` | `command`, `arguments`, `options` (will run `./vendor/bin/run [command] [argument1] [argument2] ... --[option1]=[value1] --[option2]=[value2] ...`), `in-current-process` (false) |

Tasks provided as plain-text strings will be executed as is in the current working directory.

Expand Down
13 changes: 10 additions & 3 deletions src/Tasks/CollectionFactory/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CollectionFactory extends BaseTask implements BuilderAwareInterface, Simul
{
use LoadAllTasks;
use TaskRunner\Tasks\ProcessConfigFile\loadTasks;
use TaskRunner\Tasks\RunInCurrentProcess\loadTasks;
use \NuvoleWeb\Robo\Task\Config\Php\loadTasks;

/**
Expand Down Expand Up @@ -166,9 +167,15 @@ protected function taskFactory($task)
]);

case "run":
$taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir') . '/run')
->arg($task['command'])
->interactive($this->isTtySupported());
if (!empty($task['in-current-process'])) {
$taskExec = $this->taskRunInCurrentProcess($task['command'])
->setInheritConfig($task['inherit-config'] ?? false);
}
else {
$taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir') . '/run')
->arg($task['command'])
->interactive($this->isTtySupported());
}
if (!empty($task['arguments'])) {
$taskExec->args($task['arguments']);
}
Expand Down
108 changes: 108 additions & 0 deletions src/Tasks/RunInCurrentProcess/RunInCurrentProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace OpenEuropa\TaskRunner\Tasks\RunInCurrentProcess;

use OpenEuropa\TaskRunner\TaskRunner;
use Robo\Common\CommandArguments;
use Robo\Result;
use Robo\Robo;
use Robo\Task\BaseTask;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

/**
* Run a configurable command in current process.
*/
class RunInCurrentProcess extends BaseTask
{

use CommandArguments;

/**
* @var string
*/
protected $command;

/**
* @var bool
*/
protected $capture = false;

/**
* @var bool
*/
protected $inheritConfig = false;

/**
* @param string $command
*/
public function __construct(string $command)
{
$this->command = $command;
}

/**
* @return bool
*/
public function getCapture()
{
return $this->capture;
}

/**
* Capture and return output.
*
* @param bool $capture
*/
public function setCapture(bool $capture)
{
$this->capture = $capture;
}

/**
* @return bool
*/
public function getInheritConfig()
{
return $this->inheritConfig;
}

/**
* @param bool $inheritConfig
*/
public function setInheritConfig(bool $inheritConfig)
{
$this->inheritConfig = $inheritConfig;
}

public function run()
{
// Assemble command and input.
$line = trim($this->command . $this->arguments);
$input = new StringInput($line);

// Backup the container, robo has a global for it :-/.
$backupOutput = Robo::output();
$backupConfig = Robo::config();
$backupContainer = Robo::getContainer();
$classLoader = $backupContainer->get('classLoader');
Robo::unsetContainer();

if ($this->inheritConfig) {
Robo::config()->replace($backupConfig->export());
}

// Run command.
$output = $this->capture ? new BufferedOutput() : $backupOutput;
$taskRunner = new TaskRunner($input, $output, $classLoader);
$exitCode = $taskRunner->run();

// Restore the container.
Robo::setContainer($backupContainer);

// Get captured output if requested.
$message = $output instanceof BufferedOutput ? $output->fetch() : '';
return new Result($this, $exitCode, $message);
}

}
20 changes: 20 additions & 0 deletions src/Tasks/RunInCurrentProcess/loadTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace OpenEuropa\TaskRunner\Tasks\RunInCurrentProcess;

/**
* Load run-in-current-process tasks.
*/
trait loadTasks
{

/**
* @param string|\Robo\Contract\CommandInterface $command
*
* @return \Robo\Task\Base\Exec|\Robo\Collection\CollectionBuilder
*/
public function taskRunInCurrentProcess($command) {
return $this->task(RunInCurrentProcess::class, $command);
}

}