diff --git a/README.md b/README.md index 6edc2fe6..e73e3e3f 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,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` (will run `./vendor/bin/run [command]`) | +| `run` | `taskExec()` | `command`, `arguments`, `options` (will run `./vendor/bin/run [command] [argument1] [argument2] ... --[option1]=[value1] --[option2]=[value2] ...`) | 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 55df4d6c..48ab94be 100644 --- a/src/Tasks/CollectionFactory/CollectionFactory.php +++ b/src/Tasks/CollectionFactory/CollectionFactory.php @@ -150,7 +150,16 @@ protected function taskFactory($task) ]); case "run": - return $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')->arg($task['command']); + $taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run') + ->arg($task['command']) + ->interactive($this->isTtySupported()); + if (!empty($task['arguments'])) { + $taskExec->args($task['arguments']); + } + if (!empty($task['options'])) { + $taskExec->options($task['options'], '='); + } + return $taskExec; case "process-php": $this->secureOption($task, 'override', false); @@ -197,4 +206,14 @@ protected function secureOption(array &$task, $name, $default) { $task[$name] = isset($task[$name]) ? $task[$name] : $default; } + + /** + * Checks if the TTY mode is supported + * + * @return bool + */ + protected function isTtySupported() + { + return PHP_OS !== 'WINNT' && (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes); + } } diff --git a/tests/AbstractTaskTest.php b/tests/AbstractTaskTest.php index 0370dfda..da1a3abf 100644 --- a/tests/AbstractTaskTest.php +++ b/tests/AbstractTaskTest.php @@ -33,6 +33,7 @@ public function setup() $this->output = new BufferedOutput(); $runner = new TaskRunner(new StringInput(''), $this->output, $this->getClassLoader()); $this->setContainer($runner->getContainer()); + $this->getContainer()->get('config')->set('runner.bin_dir', realpath(__DIR__.'/../bin')); } /** diff --git a/tests/Tasks/CollectionFactoryTest.php b/tests/Tasks/CollectionFactoryTest.php index 1ede1272..ba218bad 100644 --- a/tests/Tasks/CollectionFactoryTest.php +++ b/tests/Tasks/CollectionFactoryTest.php @@ -4,7 +4,6 @@ use OpenEuropa\TaskRunner\Tasks\CollectionFactory\loadTasks; use OpenEuropa\TaskRunner\Tests\AbstractTaskTest; -use Robo\Config\Config; /** * Class CollectionFactoryTest @@ -74,6 +73,28 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source $this->assertEquals(trim($expected), trim(file_get_contents($destinationFile))); } + /** + * Tests the 'run' task. + */ + public function testRunTask() + { + $filePath = $this->getSandboxFilepath('test-file.txt'); + + $tasks = []; + $tasks[] = [ + 'task' => 'run', + 'command' => 'custom:test', + 'arguments' => [ + __METHOD__, + ], + 'options' => [ + 'filepath' => $filePath, + ], + ]; + $this->taskCollectionFactory($tasks)->run(); + $this->assertSame(__METHOD__, file_get_contents($filePath)); + } + /** * @return array */ diff --git a/tests/custom/src/TaskRunner/Commands/TestCommands.php b/tests/custom/src/TaskRunner/Commands/TestCommands.php new file mode 100644 index 00000000..334931bd --- /dev/null +++ b/tests/custom/src/TaskRunner/Commands/TestCommands.php @@ -0,0 +1,29 @@ + InputOption::VALUE_REQUIRED, + ]): void + { + file_put_contents($options['filepath'], $content); + } +} diff --git a/tests/sandbox/.gitkeep b/tests/sandbox/.gitkeep old mode 100644 new mode 100755