From ffea02997b0e155959b8e8347ab8fd904ee69ce9 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 9 Sep 2024 10:53:55 -0600 Subject: [PATCH 1/3] Allow arguments and options to be passed through to workflow scripts --- docs/workflows.md | 52 +++++++++++++++++++++++++++++++++++++++ src/Commands/Workflow.php | 26 ++++++++++++++------ 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/docs/workflows.md b/docs/workflows.md index acd18fe..5b5439b 100644 --- a/docs/workflows.md +++ b/docs/workflows.md @@ -37,6 +37,58 @@ pup workflow my-workflow pup do my-workflow ``` +### Pass additional arguments or options to a workflow + +You can pass through additional arguments and options to your workflow script. + +Example `test-script.sh`: +```bash +#!/usr/bin/env bash + +# Loop through all the arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --*=*) # Option in --option=value format + option="${1%%=*}" # Extract the option + value="${1#*=}" # Extract the value + echo "Option: $option, Value: $value" + shift + ;; + --*) # Option in --option format (expecting a separate value) + option=$1 + shift + if [[ "$1" && ! "$1" =~ ^-- ]]; then + value=$1 + echo "Option: $option, Value: $value" + shift + else + echo "Option: $option, No value provided" + fi + ;; + *) # Regular argument + echo "Argument: $1" + shift + ;; + esac +done +``` + +Example in `.puprc`: +```json +{ + "workflows": { + "my-test-workflow": [ + "./test-script.sh" + ] + } +} +``` + +Pass through arguments and options to `test-script.sh`: +```bash +pup workflow my-test-workflow -- arg1 arg2 otherArg --option-one=test1 --option-two=test2 +``` + ## Pseudo-workflows The `build` and `build_dev` properties within your `.puprc` file are also callable via the `workflow` command. diff --git a/src/Commands/Workflow.php b/src/Commands/Workflow.php index a0f7201..d4d33c3 100644 --- a/src/Commands/Workflow.php +++ b/src/Commands/Workflow.php @@ -19,11 +19,13 @@ class Workflow extends Command { */ protected function configure() { $this->setName( 'workflow' ) - ->setAliases( [ 'do' ] ) - ->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' ) - ->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' ) - ->setDescription( 'Run a command workflow.' ) - ->setHelp( 'Run a command workflow.' ); + ->setAliases( [ 'do' ] ) + ->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' ) + ->addArgument( 'extra_args', InputArgument::IS_ARRAY, 'Additional arguments to pass to the workflow.' ) + ->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' ) + ->addOption( 'extra_options', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Additional options to pass to the workflow.' ) + ->setDescription( 'Run a command workflow.' ) + ->setHelp( 'Run a command workflow.' ); } /** @@ -34,6 +36,8 @@ protected function execute( InputInterface $input, OutputInterface $output ) { $config = App::getConfig(); $root = $input->getOption( 'root' ); $workflow_slug = $input->getArgument( 'workflow' ); + $extra_args = $input->getArgument( 'extra_args' ); + $extra_options = $input->getOption( 'extra_options' ); $io = $this->getIO(); $application = $this->getApplication(); if ( ! $application ) { @@ -73,8 +77,16 @@ protected function execute( InputInterface $input, OutputInterface $output ) { $bail_on_failure = false; $step = substr( $step, 1 ); } - $io->section( "> {$step}" ); - system( Env::set( $step ), $result ); + + // Add extra arguments and options to the command. + $extra_args_string = implode( ' ', array_map( 'escapeshellarg', $extra_args ) ); + $extra_options_string = implode( ' ', array_map( static function ( $option ) { + return escapeshellarg( $option ); + }, $extra_options ) ); + $full_command = trim( "{$step} {$extra_args_string} {$extra_options_string}" ); + + $io->section( "> {$full_command}" ); + system( Env::set( $full_command ), $result ); $io->newLine(); if ( $result ) { From ac78eb535435d97d7148b35d2b26d1201e962de2 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 9 Sep 2024 11:17:31 -0600 Subject: [PATCH 2/3] Add tests --- tests/_data/test-workflow-script.sh | 30 +++++++++++++++++++ ..._tbd_check_when_tbds_exist__0.snapshot.txt | 6 ++-- tests/cli/Commands/WorkflowCest.php | 23 ++++++++++++++ ...options_to_workflow_script__0.snapshot.txt | 11 +++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100755 tests/_data/test-workflow-script.sh create mode 100644 tests/cli/Commands/__snapshots__/WorkflowCest__it_should_pass_additional_arguments_and_options_to_workflow_script__0.snapshot.txt diff --git a/tests/_data/test-workflow-script.sh b/tests/_data/test-workflow-script.sh new file mode 100755 index 0000000..20d75dd --- /dev/null +++ b/tests/_data/test-workflow-script.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# A test workflow run via the test suite + +# Loop through all the arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --*=*) # Option in --option=value format + option="${1%%=*}" # Extract the option + value="${1#*=}" # Extract the value + echo "Option: $option, Value: $value" + shift + ;; + --*) # Option in --option format (expecting a separate value) + option=$1 + shift + if [[ "$1" && ! "$1" =~ ^-- ]]; then + value=$1 + echo "Option: $option, Value: $value" + shift + else + echo "Option: $option, No value provided" + fi + ;; + *) # Regular argument + echo "Argument: $1" + shift + ;; + esac +done diff --git a/tests/cli/Commands/Checks/__snapshots__/TbdCest__it_should_run_fail_tbd_check_when_tbds_exist__0.snapshot.txt b/tests/cli/Commands/Checks/__snapshots__/TbdCest__it_should_run_fail_tbd_check_when_tbds_exist__0.snapshot.txt index 6dfa347..695f409 100644 --- a/tests/cli/Commands/Checks/__snapshots__/TbdCest__it_should_run_fail_tbd_check_when_tbds_exist__0.snapshot.txt +++ b/tests/cli/Commands/Checks/__snapshots__/TbdCest__it_should_run_fail_tbd_check_when_tbds_exist__0.snapshot.txt @@ -2,9 +2,6 @@ Checking for TBDs... -------------------- -./src/Plugin.php -9: * @since TBD - ./src/Thing/AnotherFile.php 5: _deprecated_file( __FILE__, 'TBD' ); 9: * @since TBD @@ -14,5 +11,8 @@ Checking for TBDs... 28: * @deprecated TBD 32: _deprecated_function( __METHOD__, 'TBD' ); +./src/Plugin.php +9: * @since TBD + TBDs have been found! \ No newline at end of file diff --git a/tests/cli/Commands/WorkflowCest.php b/tests/cli/Commands/WorkflowCest.php index 729f2ac..8a7124c 100644 --- a/tests/cli/Commands/WorkflowCest.php +++ b/tests/cli/Commands/WorkflowCest.php @@ -170,4 +170,27 @@ public function it_should_run_build_with_custom_env_vars( CliTester $I ) { $output = $I->grabShellOutput(); $this->assertMatchesStringSnapshot( $output ); } + + /** + * @test + */ + public function it_should_pass_additional_arguments_and_options_to_workflow_script( CliTester $I ) { + $puprc = $this->get_puprc(); + $puprc['workflows'] = []; + $puprc['workflows']['test-workflow'] = []; + $puprc['workflows']['test-workflow'][] = codecept_data_dir( 'test-workflow-script.sh' ); + $this->write_puprc( $puprc ); + + chdir( $this->tests_root . '/_data/fake-project' ); + + $I->runShellCommand( "php {$this->pup} do test-workflow -- arg1 arg2 --option-one=one --option-two=two" ); + $I->seeResultCodeIs( 0 ); + $I->seeInShellOutput( 'Argument: arg1' ); + $I->seeInShellOutput( 'Argument: arg2' ); + $I->seeInShellOutput( 'Option: --option-one, Value: one' ); + $I->seeInShellOutput( 'Option: --option-two, Value: two' ); + + $output = $I->grabShellOutput(); + $this->assertMatchesStringSnapshot( $output ); + } } diff --git a/tests/cli/Commands/__snapshots__/WorkflowCest__it_should_pass_additional_arguments_and_options_to_workflow_script__0.snapshot.txt b/tests/cli/Commands/__snapshots__/WorkflowCest__it_should_pass_additional_arguments_and_options_to_workflow_script__0.snapshot.txt new file mode 100644 index 0000000..6a023a0 --- /dev/null +++ b/tests/cli/Commands/__snapshots__/WorkflowCest__it_should_pass_additional_arguments_and_options_to_workflow_script__0.snapshot.txt @@ -0,0 +1,11 @@ +Running test-workflow workflow steps... + +> /var/www/html/wp-content/plugins/pup/tests/_data/test-workflow-script.sh 'arg1' 'arg2' '--option-one=one' '--option-two=two' +------------------------------------------------------------------------------------------------------------------------------ + +Argument: arg1 +Argument: arg2 +Option: --option-one, Value: one +Option: --option-two, Value: two + +Workflow complete. From 607311472f0c82c1970f70a92fcda90e7e9de4dc Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 9 Sep 2024 11:19:26 -0600 Subject: [PATCH 3/3] Use codecept_data_dir --- tests/cli/Commands/WorkflowCest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/Commands/WorkflowCest.php b/tests/cli/Commands/WorkflowCest.php index 8a7124c..5f9357a 100644 --- a/tests/cli/Commands/WorkflowCest.php +++ b/tests/cli/Commands/WorkflowCest.php @@ -181,7 +181,7 @@ public function it_should_pass_additional_arguments_and_options_to_workflow_scri $puprc['workflows']['test-workflow'][] = codecept_data_dir( 'test-workflow-script.sh' ); $this->write_puprc( $puprc ); - chdir( $this->tests_root . '/_data/fake-project' ); + chdir( codecept_data_dir( 'fake-project' ) ); $I->runShellCommand( "php {$this->pup} do test-workflow -- arg1 arg2 --option-one=one --option-two=two" ); $I->seeResultCodeIs( 0 );