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

[TASK] Add the Command changes for #120 #127

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 41 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 = [];

public function __construct($options)
{
$this->options = array_merge(array(
Expand All @@ -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("<comment>$command</comment>\n\n");

// Prepare a callback for live output
Expand All @@ -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;
}
}
25 changes: 25 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,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);
}
}