mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-05-21 07:06:38 +02:00
Merge pull request #351 from thePanz/344-allow-flags-filesystem-tasks
344: allow flags filesystem tasks
This commit is contained in:
commit
2c087ab477
|
@ -48,6 +48,33 @@ abstract class AbstractFileTask extends AbstractTask
|
|||
*/
|
||||
abstract protected function getParameters();
|
||||
|
||||
/**
|
||||
* Returns the default "flags".
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
protected function getDefaultFlags()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the flags for the current command.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getFlags()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$flags = $this->getDefaultFlags();
|
||||
|
||||
if (array_key_exists('flags', $options) && !empty($options['flags'])) {
|
||||
$flags = trim($options['flags']);
|
||||
}
|
||||
|
||||
return empty($flags) ? '' : $flags.' ';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a file with the placeholders replaced
|
||||
*
|
||||
|
|
|
@ -28,7 +28,7 @@ class CopyTask extends AbstractFileTask
|
|||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
return sprintf('[FS] Copy %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Copy [missing parameters]';
|
||||
}
|
||||
|
@ -39,7 +39,9 @@ class CopyTask extends AbstractFileTask
|
|||
$copyFrom = $this->getFile('from');
|
||||
$copyTo = $this->getFile('to');
|
||||
|
||||
$cmd = sprintf('cp -p %s %s', $copyFrom, $copyTo);
|
||||
$flags = $this->getFlags();
|
||||
|
||||
$cmd = sprintf('cp %s"%s" "%s"', $flags, $copyFrom, $copyTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
@ -51,4 +53,9 @@ class CopyTask extends AbstractFileTask
|
|||
{
|
||||
return ['from', 'to'];
|
||||
}
|
||||
|
||||
protected function getDefaultFlags()
|
||||
{
|
||||
return '-p';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class LinkTask extends AbstractFileTask
|
|||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
return sprintf('[FS] Link %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Link [missing parameters]';
|
||||
}
|
||||
|
@ -39,7 +39,9 @@ class LinkTask extends AbstractFileTask
|
|||
$linkFrom = $this->getFile('from');
|
||||
$linkTo = $this->getFile('to');
|
||||
|
||||
$cmd = sprintf('ln -snf %s %s', $linkFrom, $linkTo);
|
||||
$flags = $this->getFlags();
|
||||
|
||||
$cmd = sprintf('ln %s"%s" "%s"', $flags, $linkFrom, $linkTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
@ -51,4 +53,9 @@ class LinkTask extends AbstractFileTask
|
|||
{
|
||||
return ['from', 'to'];
|
||||
}
|
||||
|
||||
protected function getDefaultFlags()
|
||||
{
|
||||
return '-snf';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class MoveTask extends AbstractFileTask
|
|||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
return sprintf('[FS] Move %s"%s" to "%s"', $this->getFlags(), $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Move [missing parameters]';
|
||||
}
|
||||
|
@ -38,8 +38,9 @@ class MoveTask extends AbstractFileTask
|
|||
{
|
||||
$moveFrom = $this->getFile('from');
|
||||
$moveTo = $this->getFile('to');
|
||||
$flags = $this->getFlags();
|
||||
|
||||
$cmd = sprintf('mv %s %s', $moveFrom, $moveTo);
|
||||
$cmd = sprintf('mv %s"%s" "%s"', $flags, $moveFrom, $moveTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
|
|
@ -28,7 +28,7 @@ class RemoveTask extends AbstractFileTask
|
|||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Remove "%s"', $this->getFile('file'));
|
||||
return sprintf('[FS] Remove %s"%s"', $this->getFlags(), $this->getFile('file'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Remove [missing parameters]';
|
||||
}
|
||||
|
@ -37,8 +37,9 @@ class RemoveTask extends AbstractFileTask
|
|||
public function execute()
|
||||
{
|
||||
$file = $this->getFile('file');
|
||||
$flags = $this->getFlags();
|
||||
|
||||
$cmd = sprintf('rm %s', $file);
|
||||
$cmd = sprintf('rm %s"%s"', $flags, $file);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
|
|
@ -38,7 +38,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'cp -p a.txt b.txt',
|
||||
0 => 'cp -p "a.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -67,7 +67,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'cp -p test.txt b.txt',
|
||||
0 => 'cp -p "test.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -98,7 +98,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'cp -p localhost.txt 1234.yml',
|
||||
0 => 'cp -p "localhost.txt" "1234.yml"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -147,7 +147,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'mv a.txt b.txt',
|
||||
0 => 'mv "a.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -176,7 +176,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'mv test.txt b.txt',
|
||||
0 => 'mv "test.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -224,7 +224,35 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'rm a.txt',
|
||||
0 => 'rm "a.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
$this->assertEquals(count($testCase), count($ranCommands));
|
||||
|
||||
// Check Generated Commands
|
||||
foreach ($testCase as $index => $command) {
|
||||
$this->assertEquals($command, $ranCommands[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public function testRemoveWithFlagsTask()
|
||||
{
|
||||
$runtime = new RuntimeMockup();
|
||||
$runtime->setConfiguration(['environments' => ['test' => []]]);
|
||||
$runtime->setEnvironment('test');
|
||||
|
||||
$task = new RemoveTask();
|
||||
$task->setOptions(['file' => 'a.txt', 'flags' => '-fr']);
|
||||
$task->setRuntime($runtime);
|
||||
|
||||
$this->assertContains('a.txt', $task->getDescription());
|
||||
$task->execute();
|
||||
|
||||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'rm -fr "a.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -252,7 +280,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'rm test.txt',
|
||||
0 => 'rm "test.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -301,7 +329,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'ln -snf a.txt b.txt',
|
||||
0 => 'ln -snf "a.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
@ -330,7 +358,7 @@ class FileSystemTaskTest extends TestCase
|
|||
$ranCommands = $runtime->getRanCommands();
|
||||
|
||||
$testCase = array(
|
||||
0 => 'ln -snf test.txt b.txt',
|
||||
0 => 'ln -snf "test.txt" "b.txt"',
|
||||
);
|
||||
|
||||
// Check total of Executed Commands
|
||||
|
|
Loading…
Reference in a new issue