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 a new 'exec' task for dynamic commands #124

Merged
merged 7 commits into from
May 19, 2020
26 changes: 25 additions & 1 deletion src/Tasks/CollectionFactory/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public function getHelp()
protected function taskFactory($task)
{
if (is_string($task)) {
return $this->taskExec($task);
@trigger_error('Defining a task as a plain text is deprecated in openeuropa/task-runner:1.0.0 and is removed from openeuropa/task-runner:2.0.0. Use the "exec" task and pass arguments and options.', E_USER_DEPRECATED);
return $this->taskExec($task)->interactive($this->isTtySupported());
}

$this->secureOption($task, 'force', false);
Expand Down Expand Up @@ -181,6 +182,19 @@ protected function taskFactory($task)

return $this->collectionBuilder()->addTaskList($tasks);

case 'exec':
$taskExec = $this->taskExec($task['command'])->interactive($this->isTtySupported());
if (!empty($task['arguments'])) {
$taskExec->args($task['arguments']);
}
if (!empty($task['options'])) {
$taskExec->options($task['options']);
}
if (!empty($task['dir'])) {
$taskExec->dir($task['dir']);
}
return $taskExec;

default:
throw new TaskException($this, "Task '{$task['task']}' not supported.");
}
Expand All @@ -197,4 +211,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);
}
}
27 changes: 26 additions & 1 deletion tests/Tasks/CollectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use OpenEuropa\TaskRunner\Tasks\CollectionFactory\loadTasks;
use OpenEuropa\TaskRunner\Tests\AbstractTaskTest;
use Robo\Config\Config;

/**
* Class CollectionFactoryTest
Expand Down Expand Up @@ -74,6 +73,32 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source
$this->assertEquals(trim($expected), trim(file_get_contents($destinationFile)));
}

/**
* Tests the exec task and old style exec task deprecation.
*/
public function testExecTask(): void
{
$tasks = [
[
'task' => 'exec',
'command' => 'touch',
'arguments' => [
'file.txt',
],
'options' => [
// 1980-06-06 23:59:59.
'-t' => '198006062359.59'
],
'dir' => $this->getSandboxRoot(),
],
];
$this->taskCollectionFactory($tasks)->run();

$this->assertFileExists($this->getSandboxFilepath('file.txt'));
$mtime = gmdate('Y-m-d H:i:s', filemtime($this->getSandboxFilepath('file.txt')));
$this->assertSame('1980-06-06 23:59:59', $mtime);
}

/**
* @return array
*/
Expand Down