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: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
35 changes: 34 additions & 1 deletion src/Liip/RMT/Action/CommandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
*/
class CommandAction extends BaseAction
{
/**
* Saves the command vars.
* @var array
*/
protected $commandVars = [];
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need that class property? Why not doing everything into the execute() method?
For info, from the execute method, you can do

Context::getParam('current-version')
Context::getParam('new-version')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Running Context::getParam('current-version') in the execute() method would break since it's not yet defined for instance when ran in the prerequisites.

Running Context::get('version-persister')->getCurrentVersion() do the trick but if ran let's say in the prepareCommand(), that will end up being the new released version and not the current one.

The only way found at this point to get the current version is to get it in the constructor but I might be missing something


public function __construct($options)
{
$this->options = array_merge(array(
Expand All @@ -31,11 +37,13 @@ public function __construct($options)
if ($this->options['cmd'] == null) {
throw new \RuntimeException('Missing [cmd] option');
}

$this->commandVars['version'] = Context::get('version-persister')->getCurrentVersion();
}

public function execute()
{
$command = $this->options['cmd'];
$command = $this->prepareCommand($this->options['cmd'], $this->commandVars);
Context::get('output')->write("<comment>$command</comment>\n\n");

// Prepare a callback for live output
Expand Down Expand Up @@ -64,4 +72,29 @@ 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;
}

$this->commandVars['new_version'] = Context::getParam('new-version');
extract($this->commandVars);
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know that extract() function. This is no magique. So ugly. So... :puke:
Can we do something more explicit? Like

foreach(['version', 'new_version'] as $possiblePlaceholder) {
    ...
}


preg_match_all('@%([A-Za-z0-9_]*)%@', $command, $matches);
if (array_key_exists(1, $matches)) {
foreach ($matches[1] as $var) {
$command = str_replace('%' . $var . '%', $$var, $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!

}