diff --git a/README.md b/README.md index 6de9987..6be76ff 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ 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" } @@ -193,6 +193,8 @@ commands: 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 | @@ -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. diff --git a/src/Tasks/CollectionFactory/CollectionFactory.php b/src/Tasks/CollectionFactory/CollectionFactory.php index b26f753..cd8fe97 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -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; /** @@ -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']); } diff --git a/src/Tasks/RunInCurrentProcess/RunInCurrentProcess.php b/src/Tasks/RunInCurrentProcess/RunInCurrentProcess.php new file mode 100644 index 0000000..1928a1f --- /dev/null +++ b/src/Tasks/RunInCurrentProcess/RunInCurrentProcess.php @@ -0,0 +1,108 @@ +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); + } + +} diff --git a/src/Tasks/RunInCurrentProcess/loadTasks.php b/src/Tasks/RunInCurrentProcess/loadTasks.php new file mode 100644 index 0000000..ead0770 --- /dev/null +++ b/src/Tasks/RunInCurrentProcess/loadTasks.php @@ -0,0 +1,20 @@ +task(RunInCurrentProcess::class, $command); + } + +}