Skip to content

Commit

Permalink
Merge pull request #2 from acseo/dev-acseo
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptiklemur authored May 2, 2018
2 parents d1257cf + 4f9e23d commit c451df8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
# git-hook-handler


## Usage
Just create a `git-hooks.yml` file in your projects base directory, and fill it with an array of commands you want to run:

```yml
```yaml
# git-hook.yml
config:
commit-on-error : true # or false
pre-commit:
- bldr run ci
- phpunit
# Simple command
- bin/phpcs
# or more complex command
- phpunit:
description : 'Run PHPUnit'
command : phpunit
exitcode : 0
phpcs-fixer:
description : 'Checking PHP Syntax with PHP-CS-FIXER'
exitcode : 0
command : >4
COMMIT_RANGE='HEAD~..HEAD' &&
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}") &&
if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then IFS=$'\n' EXTRA_ARGS=('--path-mode=intersection' '--' ${CHANGED_FILES[@]}); fi
&& ./vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --using-cache=no "${EXTRA_ARGS[@]}"
post-merge:
- bldr run compileAssets
command_name:
description : 'lorem ipsum'
command : 'mycommand'
exitcode : 0
```
and then place the following in your composer.json, then run `composer install` or `composer update`

```json
# composer.json
"scripts": {
"pre-update-cmd": "Aequasi\\HookHandler\\HookScript::install",
"pre-install-cmd": "Aequasi\\HookHandler\\HookScript::install"
Expand Down
9 changes: 7 additions & 2 deletions scripts/hook
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require_once $baseDir . '/vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;


$config = $baseDir.'/git-hooks.yml';
$commands = [];
if (file_exists($config)) {
Expand All @@ -16,4 +15,10 @@ if (file_exists($config)) {

$handler = new \Aequasi\HookHandler\Handler($commands, basename(__FILE__));

$handler->run();
$exitCode = $handler->run();

if (isset($commands['config']) && isset($command['config']['commit-on-error']) && $command['config']['commit-on-error']) {
exit(0);
}

exit($exitCode);
47 changes: 35 additions & 12 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,42 @@ public function doRun(InputInterface $input, OutputInterface $output)
$this->categories[$this->hook] = [];
}

foreach ($this->categories[$this->hook] as $command) {
$output->writeln("<comment> Running $this->hook hook </comment>");
$output->writeln("<comment>Running $this->hook hook </comment>");
$error = false;
foreach ($this->categories[$this->hook] as $hookItem) {
if (is_array($hookItem)) {
$group = key($hookItem);
$groupData = $hookItem[$group];
}
else {
$group = "Unnamed";
$groupData = [
'description' => 'This hook was not named in git-hooks.yml file.',
'command' => $hookItem
];
}

$process = new Process($command, __DIR__ . '/../../../../');
$output->writeln(['', "<comment> $group hook </comment> : ".$groupData['description']]);

$process = new Process($groupData['command'], __DIR__ . '../../../../');
$process->setTimeout(null);
$output->write($command."......");
if ($process->run() === 1) {
$output->writeln([" <comment>Executed command :</comment>",'']);
$output->writeln(" ".str_replace('&&', "&& \\ \n ", $groupData['command']));
$process->run();

$output->writeln([" <comment>Command Result :</comment>",'']);

$output->writeln(' Failed.');
$output->writeln("<error>{$command} failed</error>");
$output->writeln($process->getOutput());
$output->writeln("<error>{$command} failed</error>");
$output->writeln($process->getOutput());

return 1;
$exitCode = $process->getExitCode();
if (isset($groupData['exitcode']) && $groupData['exitcode'] != $exitCode) {
$output->writeln(" <error>$group : Exit Code for Hook ($exitCode) is different than expected (".$groupData['exitcode'].")</error>");
$error = true;
} else {
$output->writeln(" <info>$group : Success.</info>");
}
$output->writeln(' Success.');
}

}
$output->writeln(
[
'',
Expand All @@ -88,5 +106,10 @@ public function doRun(InputInterface $input, OutputInterface $output)
''
]
);

if ($error) {
return -1;
}
return 0;
}
}

0 comments on commit c451df8

Please sign in to comment.