Skip to content

Commit

Permalink
Allow to configure which hooks to run
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed May 21, 2024
1 parent 39091f2 commit ee7707e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
- '%hooks_dir%'
- '%hooks_preset%'
- '%git_hook_variables%'
- '%git_hooks%'
GrumPHP\Configuration\Model\ParallelConfig:
factory: ['GrumPHP\Configuration\Model\ParallelConfig', 'fromArray']
arguments:
Expand Down
9 changes: 9 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Configuration;

use GrumPHP\Console\Command\Git\InitCommand;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -70,6 +71,14 @@ public function getConfigTreeBuilder(): TreeBuilder
$ascii->children()->variableNode('failed')->defaultValue('grumphp-grumpy.txt');
$ascii->children()->variableNode('succeeded')->defaultValue('grumphp-happy.txt');

$rootNode->children()->arrayNode('git_hooks')->defaultValue(InitCommand::$hooks)
->prototype('scalar')
->validate()
->ifNotInArray(InitCommand::$hooks)
->thenInvalid('Invalid item %s')
->end()
->end();

// parallel
$parallel = $rootNode->children()->arrayNode('parallel');
$parallel->canBeDisabled();
Expand Down
8 changes: 7 additions & 1 deletion src/Configuration/Model/HooksConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class HooksConfig
public function __construct(
?string $dir,
string $preset,
array $variables
array $variables,
private array $gitHooks,
) {
$this->dir = $dir;
$this->preset = $preset;
Expand All @@ -48,4 +49,9 @@ public function getVariables(): array
{
return $this->variables;
}

public function getGitHooks(): array
{
return $this->gitHooks;
}
}
1 change: 1 addition & 0 deletions src/Console/Command/Git/DeInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DeInitCommand extends Command
*/
protected static $hooks = [
'pre-commit',
'pre-push',
'commit-msg',
];

Expand Down
4 changes: 4 additions & 0 deletions src/Console/Command/Git/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class InitCommand extends Command
*/
public static $hooks = [
'pre-commit',
'pre-push',
'commit-msg',
];

Expand Down Expand Up @@ -100,6 +101,9 @@ public function execute(InputInterface $input, OutputInterface $output): int
}

foreach (self::$hooks as $hook) {
if (!in_array($hook, $this->hooksConfig->getGitHooks(), true)) {
continue;
}
$gitHook = $this->filesystem->buildPath($gitHooksPath, $hook);
$hookTemplate = $this->filesystem->guessFile(
[
Expand Down

0 comments on commit ee7707e

Please sign in to comment.