diff --git a/README.md b/README.md index 35092b5e..d40dd094 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ Prerequisite actions are executed before the interactive part. * Option `ignore-require` and `ignore-require-dev`: don't check dependencies in `require` or `require-dev` section * Option `whitelist`: allow specific dependencies to use development version * `command`: Execute a system command - * Option `cmd` The command to execute + * Option `cmd` The command to execute (variables `%version%` and `%new_version%` can be used) * Option `live_output` boolean, do we display the command output? (default: *true*) * Option `timeout` integer, limits the time for the command. (default: *600*) * Option `stop_on_error` boolean, do we break the release process on error? (default: *true*) @@ -213,7 +213,7 @@ Actions can be used for pre or post release parts. * Option `default-stub-cli`: the default stub for CLI usage of the package. * Option `default-stub-web`: the default stub for web application usage of the package. * `command`: Execute a system command - * Option `cmd` The command to execute + * Option `cmd` The command to execute (variables `%version%` and `%new_version%` can be used) * Option `live_output` boolean, do we display the command output? (default: *true*) * Option `timeout` integer, limits the time for the command. (default: *600*) * Option `stop_on_error` boolean, do we break the release process on error? (default: *true*) diff --git a/src/Liip/RMT/Action/CommandAction.php b/src/Liip/RMT/Action/CommandAction.php index b2ea5b0e..15d268e9 100644 --- a/src/Liip/RMT/Action/CommandAction.php +++ b/src/Liip/RMT/Action/CommandAction.php @@ -19,8 +19,12 @@ */ class CommandAction extends BaseAction { + protected $currentVersion; + public function __construct($options) { + parent::__construct($options); + $this->options = array_merge(array( 'cmd' => null, 'live_output' => true, @@ -31,11 +35,13 @@ public function __construct($options) if ($this->options['cmd'] == null) { throw new \RuntimeException('Missing [cmd] option'); } + + $this->currentVersion = Context::get('version-persister')->getCurrentVersion(); } public function execute() { - $command = $this->options['cmd']; + $command = $this->prepareCommand($this->options['cmd']); Context::get('output')->write("$command\n\n"); // Prepare a callback for live output @@ -64,4 +70,36 @@ public function execute() throw new \RuntimeException("Command [$command] exit with code " . $process->getExitCode()); } } + + /** + * Prepares the command + * + * @param string $command + * @return string + */ + protected function prepareCommand($command) + { + if (substr_count($command, '%') < 2) { + return $command; + } + + preg_match_all('@%([A-Za-z0-9_]*)%@', $command, $matches); + + if (! array_key_exists(1, $matches)) { + return $command; + } + + $placeHolderValue = [ + 'version' => $this->currentVersion, + 'new_version' => Context::getParam('new-version'), + ]; + + foreach ($matches[1] as $placeHolder) { + if (array_key_exists($placeHolder, $placeHolderValue)) { + $command = str_replace("%$placeHolder%", $placeHolderValue[$placeHolder], $command); + } + } + + return $command; + } } diff --git a/test/Liip/RMT/Tests/Functional/CommandActionTest.php b/test/Liip/RMT/Tests/Functional/CommandActionTest.php index 81b2f452..b4abcae5 100644 --- a/test/Liip/RMT/Tests/Functional/CommandActionTest.php +++ b/test/Liip/RMT/Tests/Functional/CommandActionTest.php @@ -26,4 +26,18 @@ public function testCommand() // $this->manualDebug(); $this->assertContains('Command Action : echo "hello world"', $output); } + + public function testVersionsOutputOnPostRelease() + { + $this->createChangelog('simple'); + $this->createConfig('simple', 'changelog', array( + 'post-release-actions' => array( + 'command' => array('cmd' => 'echo %version% %new_version%'), + ), + )); + + exec('./RMT release -n --no-ansi --comment="test"', $output); + $output = implode("\n", $output); + $this->assertContains('Command Action : echo 1 2', $output); + } }