From e6ec0442307f0d19d2b06de793ae967733fba2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 10 Apr 2022 01:55:49 -0300 Subject: [PATCH] [Galactica] V5 - improve exec task --- CHANGELOG.md | 1 + src/Task/BuiltIn/ExecTask.php | 16 +++++++++++++- tests/Task/BuiltIn/ExecTaskTest.php | 33 +++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d3fcfb..60f0231 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ CHANGELOG for 5.X * Refactored for Symfony 6 and PHP 8 * Added strong types * Removed task `composer/self-update` + * Allow `exec` task to interpolate `%environment%` and `%release%` diff --git a/src/Task/BuiltIn/ExecTask.php b/src/Task/BuiltIn/ExecTask.php index 589e3f4..a5a3705 100644 --- a/src/Task/BuiltIn/ExecTask.php +++ b/src/Task/BuiltIn/ExecTask.php @@ -57,8 +57,22 @@ class ExecTask extends AbstractTask throw new ErrorException('Parameter "cmd" is not defined'); } + $mapping = [ + '%environment%' => $this->runtime->getEnvironment(), + ]; + + if ($this->runtime->getReleaseId() !== null) { + $mapping['%release%'] = $this->runtime->getReleaseId(); + } + + $cmd = str_replace( + array_keys($mapping), + array_values($mapping), + strval($options['cmd']) + ); + /** @var Process $process */ - $process = $this->runtime->runCommand(strval($options['cmd']), intval($options['timeout'])); + $process = $this->runtime->runCommand($cmd, intval($options['timeout'])); return $process->isSuccessful(); } diff --git a/tests/Task/BuiltIn/ExecTaskTest.php b/tests/Task/BuiltIn/ExecTaskTest.php index 048670c..a17e78a 100755 --- a/tests/Task/BuiltIn/ExecTaskTest.php +++ b/tests/Task/BuiltIn/ExecTaskTest.php @@ -25,10 +25,10 @@ class ExecTest extends TestCase $runtime->setEnvironment('test'); $task = new ExecTask(); - $task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']); + $task->setOptions(['cmd' => 'ls -l', 'desc' => 'Command description']); $task->setRuntime($runtime); - $this->assertStringContainsString('[Exec] Loading docker', $task->getDescription()); + $this->assertStringContainsString('[Exec] Command description', $task->getDescription()); $task->execute(); $ranCommands = $runtime->getRanCommands(); @@ -46,6 +46,35 @@ class ExecTest extends TestCase } } + public function testSimpleCommandWithInterpolation() + { + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(['environments' => ['test' => []]]); + $runtime->setEnvironment('test'); + $runtime->setReleaseId('1234'); + + $task = new ExecTask(); + $task->setOptions(['cmd' => 'cp %environment%.env /app/%release%/.env', 'desc' => 'Copy config']); + $task->setRuntime($runtime); + + $this->assertStringContainsString('[Exec] Copy config', $task->getDescription()); + $task->execute(); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'cp test.env /app/1234/.env', + ); + + // 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 testCommandWithoutDescription() { $runtime = new RuntimeMockup();