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 the Command changes for #120 #156

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*)
Expand Down Expand Up @@ -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*)
Expand Down
40 changes: 39 additions & 1 deletion src/Liip/RMT/Action/CommandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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("<comment>$command</comment>\n\n");

// Prepare a callback for live output
Expand Down Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions test/Liip/RMT/Tests/Functional/CommandActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, for the tests, thanks!

}