From c5c4eabd681ddd787c9d7215f10f5e7a5b209542 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Mon, 5 Oct 2020 17:38:24 +0200 Subject: [PATCH] RunScript and Exec Composer tasks --- docs/tasks/Composer.md | 79 +++++++++++++++++++++++++++++++++ src/Task/Composer/Exec.php | 79 +++++++++++++++++++++++++++++++++ src/Task/Composer/RunScript.php | 63 ++++++++++++++++++++++++++ src/Task/Composer/Tasks.php | 23 ++++++++++ 4 files changed, 244 insertions(+) create mode 100644 src/Task/Composer/Exec.php create mode 100644 src/Task/Composer/RunScript.php diff --git a/docs/tasks/Composer.md b/docs/tasks/Composer.md index 2f3ec65ea..a2ed96532 100644 --- a/docs/tasks/Composer.md +++ b/docs/tasks/Composer.md @@ -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 +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 +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. diff --git a/src/Task/Composer/Exec.php b/src/Task/Composer/Exec.php new file mode 100644 index 000000000..0957c1af0 --- /dev/null +++ b/src/Task/Composer/Exec.php @@ -0,0 +1,79 @@ +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); + } +} diff --git a/src/Task/Composer/RunScript.php b/src/Task/Composer/RunScript.php new file mode 100644 index 000000000..165f81f94 --- /dev/null +++ b/src/Task/Composer/RunScript.php @@ -0,0 +1,63 @@ +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); + } +} diff --git a/src/Task/Composer/Tasks.php b/src/Task/Composer/Tasks.php index b022972d4..3cda93e9e 100644 --- a/src/Task/Composer/Tasks.php +++ b/src/Task/Composer/Tasks.php @@ -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); + } }