From 8c0b5a2bab1149846c211d9b863faa6981c01c4b Mon Sep 17 00:00:00 2001 From: sebastianseidelmann Date: Fri, 14 Jul 2017 14:15:34 +0200 Subject: [PATCH] [TASK] Add the Command changes for #120 --- src/Liip/RMT/Action/CommandAction.php | 42 ++++++++++++++++++- .../Tests/Functional/CommandActionTest.php | 25 +++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Liip/RMT/Action/CommandAction.php b/src/Liip/RMT/Action/CommandAction.php index f05bb045..e13e692a 100644 --- a/src/Liip/RMT/Action/CommandAction.php +++ b/src/Liip/RMT/Action/CommandAction.php @@ -19,6 +19,12 @@ */ class CommandAction extends BaseAction { + /** + * Saves the command vars. + * @var array + */ + protected $commandVars = []; + public function __construct($options) { $this->options = array_merge(array( @@ -30,11 +36,24 @@ public function __construct($options) if ($this->options['cmd'] == null) { throw new \RuntimeException('Missing [cmd] option'); } + + $this->initCommandVars(); + } + + /** + * Initialize the command vars. + */ + private function initCommandVars() + { + $this->commandVars = array( + 'version' => Context::get('version-persister')->getCurrentVersion(), + 'new_version' => Context::getParam('new-version'), + ); } public function execute() { - $command = $this->options['cmd']; + $command = $this->prepareCommand($this->options['cmd'], $this->commandVars); Context::get('output')->write("$command\n\n"); // Prepare a callback for live output @@ -58,4 +77,25 @@ public function execute() throw new \RuntimeException("Command [$command] exit with code " . $process->getExitCode()); } } + + /** + * Prepares the command. + * @param string $command + * @param array $vars + * @return string + */ + private function prepareCommand($command, $vars = array()) + { + if (strpos($command, '{') !== false) { + extract($vars); + preg_match_all('@\{([A-Za-z0-9_]*)\}@i', $command, $matches); + if (! empty($matches[1])) { + foreach ($matches[1] as $var) { + $command = str_replace('{' . $var . '}', $$var, $command); + } + } + } + + return $command; + } } diff --git a/test/Liip/RMT/Tests/Functional/CommandActionTest.php b/test/Liip/RMT/Tests/Functional/CommandActionTest.php index 81b2f452..fe797e41 100644 --- a/test/Liip/RMT/Tests/Functional/CommandActionTest.php +++ b/test/Liip/RMT/Tests/Functional/CommandActionTest.php @@ -26,4 +26,29 @@ public function testCommand() // $this->manualDebug(); $this->assertContains('Command Action : echo "hello world"', $output); } + + public function testCommandWithVariables() + { + $this->createChangelog('simple'); + $this->createConfig('simple', 'changelog', array( + 'pre-release-actions' => array( + 'command' => array('cmd' => 'echo "current version: {version}"'), + ), + )); + + exec('./RMT release -n --no-ansi --comment="test"', $output); + $output = implode("\n", $output); + $this->assertContains('Command Action : echo "current version: 1"', $output); + + $this->createChangelog('simple'); + $this->createConfig('simple', 'changelog', array( + 'pre-release-actions' => array( + 'command' => array('cmd' => 'echo "next version: {new_version}"'), + ), + )); + + exec('./RMT release -n --no-ansi --comment="test"', $output); + $output = implode("\n", $output); + $this->assertContains('Command Action : echo "next version: 2"', $output); + } }