Skip to content

Commit

Permalink
Merge pull request #13 from sebastianfeldmann
Browse files Browse the repository at this point in the history
Add method to handle command variables
  • Loading branch information
sebastianfeldmann authored Oct 20, 2021
2 parents c4a677f + f44c6fe commit 72f2066
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
run: tools/phpcs --standard=psr12 src tests

- name: Static code analysis
run: tools/phpstan analyse
run: tools/phpstan analyse -c phpstan-${{ matrix.php-versions }}.neon
6 changes: 3 additions & 3 deletions phive.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^9.0.0" installed="9.5.0" location="./tools/phpunit" copy="true"/>
<phar name="phpcs" version="^3.5.2" installed="3.5.8" location="./tools/phpcs" copy="true"/>
<phar name="phpstan" version="^0.12.2" installed="0.12.59" location="./tools/phpstan" copy="true"/>
<phar name="phpunit" version="^9.0.0" installed="9.5.10" location="./tools/phpunit" copy="true"/>
<phar name="phpcs" version="^3.5.2" installed="3.6.1" location="./tools/phpcs" copy="true"/>
<phar name="phpstan" version="^0.12.2" installed="0.12.99" location="./tools/phpstan" copy="true"/>
</phive>
4 changes: 4 additions & 0 deletions phpstan-7.3.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 5
paths:
- %currentWorkingDirectory%/src/
4 changes: 4 additions & 0 deletions phpstan-7.4.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 5
paths:
- %currentWorkingDirectory%/src/
6 changes: 6 additions & 0 deletions phpstan-8.0.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 5
paths:
- %currentWorkingDirectory%/src/
ignoreErrors:
- '#Left side of && is always true#'
43 changes: 40 additions & 3 deletions src/Command/Executable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class Executable implements Command
*/
private $options = [];

/**
* List of variables to define
*
* @var string[]
*/
private $vars = [];

/**
* List of acceptable exit codes.
*
Expand All @@ -71,9 +78,9 @@ public function __construct(string $cmd, array $exitCodes = [0])
*/
public function getCommand(): string
{
$cmd = sprintf('"%s"', $this->cmd)
. (count($this->options) ? ' ' . implode(' ', $this->options) : '')
. ($this->isSilent ? ' 2> /dev/null' : '');
$cmd = $this->getVars() . sprintf('"%s"', $this->cmd)
. (count($this->options) ? ' ' . implode(' ', $this->options) : '')
. ($this->isSilent ? ' 2> /dev/null' : '');

return Util::escapeSpacesIfOnWindows($cmd);
}
Expand Down Expand Up @@ -125,6 +132,36 @@ public function addOption(string $option, $value = null, string $glue = '='): Ex
return $this;
}

/**
* Add a var definition to a command
*
* @param string $name
* @param string $value
* @return $this
*/
public function addVar(string $name, string $value): Executable
{
$this->vars[$name] = $value;

return $this;
}

/**
* Return variable definition string e.g. "MYFOO='sometext' MYBAR='nothing' "
*
* @return string
*/
protected function getVars(): string
{
$varStrings = [];

foreach ($this->vars as $name => $value) {
$varStrings[] = $name . '=' . escapeshellarg($value);
}

return count($varStrings) ? implode(' ', $varStrings) . ' ' : '';
}

/**
* Adds an option to a command if it is not empty.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/cli/Command/ExecutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,17 @@ public function testAddOptionIfEmptyAsValue()

$this->assertEquals('"foo" -bar=\'fiz\'', (string) $cmd, 'option should be added');
}

/**
* Tests Executable::addVar
*/
public function testAddVar()
{
$cmd = new Executable('tool');
$cmd->addVar('FOO', 'fiz');
$cmd->addVar('BAR', 'baz');
$cmd->addOptionIfNotEmpty('--extra', 'bonus');

$this->assertEquals('FOO=\'fiz\' BAR=\'baz\' "tool" --extra=\'bonus\'', (string) $cmd, 'vars prefix');
}
}

0 comments on commit 72f2066

Please sign in to comment.