Skip to content

Commit 8c03348

Browse files
authored
Merge branch 'main' into fix/ensure-included-files-are-included
2 parents 282a9d1 + c962d6d commit 8c03348

File tree

7 files changed

+139
-11
lines changed

7 files changed

+139
-11
lines changed

docs/workflows.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,58 @@ pup workflow my-workflow
3737
pup do my-workflow
3838
```
3939

40+
### Pass additional arguments or options to a workflow
41+
42+
You can pass through additional arguments and options to your workflow script.
43+
44+
Example `test-script.sh`:
45+
```bash
46+
#!/usr/bin/env bash
47+
48+
# Loop through all the arguments
49+
while [[ "$#" -gt 0 ]]; do
50+
case $1 in
51+
--*=*) # Option in --option=value format
52+
option="${1%%=*}" # Extract the option
53+
value="${1#*=}" # Extract the value
54+
echo "Option: $option, Value: $value"
55+
shift
56+
;;
57+
--*) # Option in --option format (expecting a separate value)
58+
option=$1
59+
shift
60+
if [[ "$1" && ! "$1" =~ ^-- ]]; then
61+
value=$1
62+
echo "Option: $option, Value: $value"
63+
shift
64+
else
65+
echo "Option: $option, No value provided"
66+
fi
67+
;;
68+
*) # Regular argument
69+
echo "Argument: $1"
70+
shift
71+
;;
72+
esac
73+
done
74+
```
75+
76+
Example in `.puprc`:
77+
```json
78+
{
79+
"workflows": {
80+
"my-test-workflow": [
81+
"./test-script.sh"
82+
]
83+
}
84+
}
85+
```
86+
87+
Pass through arguments and options to `test-script.sh`:
88+
```bash
89+
pup workflow my-test-workflow -- arg1 arg2 otherArg --option-one=test1 --option-two=test2
90+
```
91+
4092
## Pseudo-workflows
4193

4294
The `build` and `build_dev` properties within your `.puprc` file are also callable via the `workflow` command.

pup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace StellarWP\Pup;
55

6-
const PUP_VERSION = '1.3.6';
6+
const PUP_VERSION = '1.3.7';
77
define( '__PUP_DIR__', __DIR__ );
88

99
if ( ! \Phar::running() ) {

src/Commands/Workflow.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class Workflow extends Command {
1919
*/
2020
protected function configure() {
2121
$this->setName( 'workflow' )
22-
->setAliases( [ 'do' ] )
23-
->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' )
24-
->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' )
25-
->setDescription( 'Run a command workflow.' )
26-
->setHelp( 'Run a command workflow.' );
22+
->setAliases( [ 'do' ] )
23+
->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' )
24+
->addArgument( 'extra_args', InputArgument::IS_ARRAY, 'Additional arguments to pass to the workflow.' )
25+
->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' )
26+
->addOption( 'extra_options', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Additional options to pass to the workflow.' )
27+
->setDescription( 'Run a command workflow.' )
28+
->setHelp( 'Run a command workflow.' );
2729
}
2830

2931
/**
@@ -34,6 +36,8 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
3436
$config = App::getConfig();
3537
$root = $input->getOption( 'root' );
3638
$workflow_slug = $input->getArgument( 'workflow' );
39+
$extra_args = $input->getArgument( 'extra_args' );
40+
$extra_options = $input->getOption( 'extra_options' );
3741
$io = $this->getIO();
3842
$application = $this->getApplication();
3943
if ( ! $application ) {
@@ -73,8 +77,16 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
7377
$bail_on_failure = false;
7478
$step = substr( $step, 1 );
7579
}
76-
$io->section( "> <fg=cyan>{$step}</>" );
77-
system( Env::set( $step ), $result );
80+
81+
// Add extra arguments and options to the command.
82+
$extra_args_string = implode( ' ', array_map( 'escapeshellarg', $extra_args ) );
83+
$extra_options_string = implode( ' ', array_map( static function ( $option ) {
84+
return escapeshellarg( $option );
85+
}, $extra_options ) );
86+
$full_command = trim( "{$step} {$extra_args_string} {$extra_options_string}" );
87+
88+
$io->section( "> <fg=cyan>{$full_command}</>" );
89+
system( Env::set( $full_command ), $result );
7890
$io->newLine();
7991

8092
if ( $result ) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# A test workflow run via the test suite
4+
5+
# Loop through all the arguments
6+
while [[ "$#" -gt 0 ]]; do
7+
case $1 in
8+
--*=*) # Option in --option=value format
9+
option="${1%%=*}" # Extract the option
10+
value="${1#*=}" # Extract the value
11+
echo "Option: $option, Value: $value"
12+
shift
13+
;;
14+
--*) # Option in --option format (expecting a separate value)
15+
option=$1
16+
shift
17+
if [[ "$1" && ! "$1" =~ ^-- ]]; then
18+
value=$1
19+
echo "Option: $option, Value: $value"
20+
shift
21+
else
22+
echo "Option: $option, No value provided"
23+
fi
24+
;;
25+
*) # Regular argument
26+
echo "Argument: $1"
27+
shift
28+
;;
29+
esac
30+
done

tests/cli/Commands/Checks/__snapshots__/TbdCest__it_should_run_fail_tbd_check_when_tbds_exist__0.snapshot.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
Checking for TBDs...
33
--------------------
44

5-
./src/Plugin.php
6-
9: * @since TBD
7-
85
./src/Thing/AnotherFile.php
96
5: _deprecated_file( __FILE__, 'TBD' );
107
9: * @since TBD
@@ -14,5 +11,8 @@ Checking for TBDs...
1411
28: * @deprecated TBD
1512
32: _deprecated_function( __METHOD__, 'TBD' );
1613

14+
./src/Plugin.php
15+
9: * @since TBD
16+
1717

1818
TBDs have been found!

tests/cli/Commands/WorkflowCest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,27 @@ public function it_should_run_build_with_custom_env_vars( CliTester $I ) {
170170
$output = $I->grabShellOutput();
171171
$this->assertMatchesStringSnapshot( $output );
172172
}
173+
174+
/**
175+
* @test
176+
*/
177+
public function it_should_pass_additional_arguments_and_options_to_workflow_script( CliTester $I ) {
178+
$puprc = $this->get_puprc();
179+
$puprc['workflows'] = [];
180+
$puprc['workflows']['test-workflow'] = [];
181+
$puprc['workflows']['test-workflow'][] = codecept_data_dir( 'test-workflow-script.sh' );
182+
$this->write_puprc( $puprc );
183+
184+
chdir( codecept_data_dir( 'fake-project' ) );
185+
186+
$I->runShellCommand( "php {$this->pup} do test-workflow -- arg1 arg2 --option-one=one --option-two=two" );
187+
$I->seeResultCodeIs( 0 );
188+
$I->seeInShellOutput( 'Argument: arg1' );
189+
$I->seeInShellOutput( 'Argument: arg2' );
190+
$I->seeInShellOutput( 'Option: --option-one, Value: one' );
191+
$I->seeInShellOutput( 'Option: --option-two, Value: two' );
192+
193+
$output = $I->grabShellOutput();
194+
$this->assertMatchesStringSnapshot( $output );
195+
}
173196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Running test-workflow workflow steps...
2+
3+
> /var/www/html/wp-content/plugins/pup/tests/_data/test-workflow-script.sh 'arg1' 'arg2' '--option-one=one' '--option-two=two'
4+
------------------------------------------------------------------------------------------------------------------------------
5+
6+
Argument: arg1
7+
Argument: arg2
8+
Option: --option-one, Value: one
9+
Option: --option-two, Value: two
10+
11+
Workflow complete.

0 commit comments

Comments
 (0)