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

RunScript and Exec Composer tasks #990

Open
wants to merge 1 commit into
base: 3.x
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
79 changes: 79 additions & 0 deletions docs/tasks/Composer.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,82 @@ $this->taskComposerValidate()->run();
* `options(array $options, $separator = null)` Pass multiple options to executable. The associative array contains
* `optionList($option, $value = null, $separator = null)` Pass an option with multiple values to executable. Value can be a string or array.

## RunScript


Composer Run Script

``` php
<?php
// simple execution
$this->taskComposerRunScript()->arg('myscript')->run();

// pass an option to the script
$this->taskComposerRunScript()->arg('myscript')->scriptOption('foo')->run();
?>
```

* `preferDist($preferDist = null)` adds `prefer-dist` option to composer
* `preferSource()` adds `prefer-source` option to composer
* `dev($dev = null)` adds `dev` option to composer
* `noDev()` adds `no-dev` option to composer
* `ansi($ansi = null)` adds `ansi` option to composer
* `noAnsi()` adds `no-ansi` option to composer
* `interaction($interaction = null)` * `param bool` $interaction
* `noInteraction()` adds `no-interaction` option to composer
* `optimizeAutoloader($optimize = null)` adds `optimize-autoloader` option to composer
* `ignorePlatformRequirements($ignore = null)` adds `ignore-platform-reqs` option to composer
* `disablePlugins($disable = null)` disable plugins
* `noScripts($disable = null)` skip scripts
* `workingDir($dir)` adds `--working-dir $dir` option to composer
* `buildCommand()` Copy class fields into command options as directed.
* `dir($dir)` Changes working directory of command
* `arg($arg)` Pass argument to executable. Its value will be automatically escaped.
* `args($args)` Pass methods parameters as arguments to executable. Argument values
* `rawArg($arg)` Pass the provided string in its raw (as provided) form as an argument to executable.
* `option($option, $value = null, $separator = null)` Pass option to executable. Options are prefixed with `--` , value can be provided in second parameter.
* `options(array $options, $separator = null)` Pass multiple options to executable. The associative array contains
* `optionList($option, $value = null, $separator = null)` Pass an option with multiple values to executable. Value can be a string or array.
* `scriptOption($option, $value = null, $separator = null)` Pass option to the script. Options are prefixed with `--` , value can be provided in second parameter.

## Exec


Composer Exec

``` php
<?php
// simple execution
$this->taskComposerExec()->arg('mycommand')->run();

// pass an option to the command
$this->taskComposerExec()->arg('mycommand')->scriptOption('foo')->run();

// execute a command installed globally
$this->taskComposerExec('composer', true)->arg('mycommand')->run();

?>
```

* `preferDist($preferDist = null)` adds `prefer-dist` option to composer
* `preferSource()` adds `prefer-source` option to composer
* `dev($dev = null)` adds `dev` option to composer
* `noDev()` adds `no-dev` option to composer
* `ansi($ansi = null)` adds `ansi` option to composer
* `noAnsi()` adds `no-ansi` option to composer
* `interaction($interaction = null)` * `param bool` $interaction
* `noInteraction()` adds `no-interaction` option to composer
* `optimizeAutoloader($optimize = null)` adds `optimize-autoloader` option to composer
* `ignorePlatformRequirements($ignore = null)` adds `ignore-platform-reqs` option to composer
* `disablePlugins($disable = null)` disable plugins
* `noScripts($disable = null)` skip scripts
* `workingDir($dir)` adds `--working-dir $dir` option to composer
* `buildCommand()` Copy class fields into command options as directed.
* `dir($dir)` Changes working directory of command
* `arg($arg)` Pass argument to executable. Its value will be automatically escaped.
* `args($args)` Pass methods parameters as arguments to executable. Argument values
* `rawArg($arg)` Pass the provided string in its raw (as provided) form as an argument to executable.
* `option($option, $value = null, $separator = null)` Pass option to executable. Options are prefixed with `--` , value can be provided in second parameter.
* `options(array $options, $separator = null)` Pass multiple options to executable. The associative array contains
* `optionList($option, $value = null, $separator = null)` Pass an option with multiple values to executable. Value can be a string or array.
* `scriptOption($option, $value = null, $separator = null)` Pass option to the script. Options are prefixed with `--` , value can be provided in second parameter.
79 changes: 79 additions & 0 deletions src/Task/Composer/Exec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Robo\Task\Composer;

/**
* Class Exec
*
* @package Robo\Task\Composer
*/
class Exec extends Base
{

/**
* @var string
*/
protected $action = 'exec';

/**
* Script options.
*
* @var array
*/
protected $scriptOptions = [];

/**
* Add a script option.
*
* @param string $option Option name
* @param string|null $value Option value
*
* @return self
*/
public function scriptOption(string $option, $value = null)
{
$this->scriptOptions[$option] = $value;

return $this;
}

/**
* Add script options to command.
*/
public function buildCommand()
{
parent::buildCommand();

$this->option('');
$this->options($this->scriptOptions);
}

/**
* Exec constructor.
*
* @param bool $global Run "composer global exec"
* @param null|string $pathToComposer Path to Composer executable
*
* @throws \Robo\Exception\TaskException
*/
public function __construct($pathToComposer = null, $global = false)
{
parent::__construct($pathToComposer);

if ($global) {
$this->action = 'global ' . $this->action;
}
}

/**
* Run the command.
*
* @return \Robo\Result
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo('Executing command: {command}', ['command' => $command]);
return $this->executeCommand($command);
}
}
63 changes: 63 additions & 0 deletions src/Task/Composer/RunScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Robo\Task\Composer;

/**
* Class RunScript
*
* @package Robo\Task\Composer
*/
class RunScript extends Base
{

/**
* @var string
*/
protected $action = 'run-script';


/**
* Script options.
*
* @var array
*/
protected $scriptOptions = [];

/**
* Add a script option.
*
* @param string $option Option name
* @param string|null $value Option value
*
* @return self
*/
public function scriptOption(string $option, $value = null)
{
$this->scriptOptions[$option] = $value;

return $this;
}

/**
* Add script options to command.
*/
public function buildCommand()
{
parent::buildCommand();

$this->option('');
$this->options($this->scriptOptions);
}

/**
* Run the command.
*
* @return \Robo\Result
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo('Running script: {command}', ['command' => $command]);
return $this->executeCommand($command);
}
}
23 changes: 23 additions & 0 deletions src/Task/Composer/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,27 @@ protected function taskCheckPlatformReqs($pathToComposer = null)
{
return $this->task(CheckPlatformReqs::class, $pathToComposer);
}

/**
* @param bool $global Run "composer global exec"
* @param null|string $pathToComposer Path to Composer executable
*
* @return \Robo\Task\Composer\Exec|\Robo\Collection\CollectionBuilder
*/
protected function taskComposerExec($pathToComposer = null, $global = false)
{
return $this->task(Exec::class, $pathToComposer, $global);
}

/**
* Create a Composer script command.
*
* @param null|string $pathToComposer Path to Composer executable
*
* @return \Robo\Task\Composer\RunScript|\Robo\Collection\CollectionBuilder
*/
protected function taskComposerRunScript($pathToComposer = null)
{
return $this->task(RunScript::class, $pathToComposer);
}
}