captainhook
captainhook copied to clipboard
Allow running only actions specified on the command line
This PR adds 3 new hook command options:
--list-actions--action--disable-plugins
--list-actions
This option allows you to list all the configured actions for a given hook:
$ ./bin/captainhook hook:pre-commit --list-actions
Listing pre-commit actions:
- \CaptainHook\App\Hook\PHP\Action\Linting
- tools/phpcs --standard=psr12 src
- \CaptainHook\App\Hook\PHP\Action\TestCoverage
- \CaptainHook\App\Hook\Composer\Action\CheckLockFile
--action
This option allows you to pass one or more actions to execute for a given hook. All other actions configured for the hook are skipped.
$ ./bin/captainhook hook:pre-commit --action="tools/phpcs --standard=psr12 src" --action="\CaptainHook\App\Hook\Composer\Action\CheckLockFile"
pre-commit:
- \CaptainHook\App\Hook\PHP\Action\Linting : skipped
- tools/phpcs --standard=psr12 src : done
- \CaptainHook\App\Hook\PHP\Action\TestCoverage : skipped
- \CaptainHook\App\Hook\Composer\Action\CheckLockFile : done
--disable-plugins
This option is useful for combining with the --action option to disable any plugins while executing only the specified actions.
$ ./bin/captainhook hook:pre-commit --disable-plugins --action="tools/phpcs --standard=psr12 src" --action="\CaptainHook\App\Hook\Composer\Action\CheckLockFile"
pre-commit:
Running with plugins disabled
- \CaptainHook\App\Hook\PHP\Action\Linting : skipped
- tools/phpcs --standard=psr12 src : done
- \CaptainHook\App\Hook\PHP\Action\TestCoverage : skipped
- \CaptainHook\App\Hook\Composer\Action\CheckLockFile : done
Rationale
As discussed in #122, I'm working on a DisallowActionChanges plugin that will extend the PreserveWorkingTree plugin to check whether any actions make changes and, if so, fail. As part of the failure message, I want to list the actions that made changes and instruct the user with a message like this:
Use the following to run this action and inspect the changes it makes:
captainhook hook:pre-commit --disable-plugins --action="tools/phpcs --standard=psr12 src"
The --list-actions option is a helper option that I thought to include so a user can quickly list all actions configured for a hook. The names of the actions it lists are the same names that you would pass to the --action option.
IO::getOptions() and IO::getOption()
I've added these two methods to allow retrieving options from IO, just like you can retrieve arguments. However, I did not add these to the IO interface, since that would likely require a major version bump. For now, they're only on Console\IO\DefaultIO and Console\IO\Base. They should also be added to the Console\IO interface in version 6.0.0.