Last active
May 10, 2016 14:53
-
-
Save Gisleburt/5d02fcc4fada140e9d4b to your computer and use it in GitHub Desktop.
A pre-commit script for my PHP project for Git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
// Based on code by raphaelstolt and buddhamagnet https://gist.github.com/raphaelstolt/588697 | |
class PreCommitHook { | |
protected $green = "\033[0;32m"; | |
protected $red = "\033[0;31m"; | |
protected $reset = "\033[0m"; | |
protected $binDir = 'bin'; | |
protected function red($string) { | |
return $this->red . $string . $this->reset; | |
} | |
protected function green($string) { | |
return $this->green . $string . $this->reset; | |
} | |
protected function echoLine($string, $endLine = true) { | |
echo $string . ($endLine ? PHP_EOL : ''); | |
} | |
public function phpUnit() { | |
$this->echoLine('Running PHPUnit... ', false); | |
exec($this->binDir.'/phpunit', $output, $returnCode); | |
if ($returnCode !== 0) { | |
$this->echoLine($this->red('Failed')); | |
$this->echoLine('PhpUnit Output: '); | |
$this->echoLine(implode(PHP_EOL, $output)); | |
$this->echoLine($this->red('COMMIT FAILED')); | |
exit(1); | |
} | |
$this->echoLine($this->green('Success')); | |
} | |
public function phpBehat() { | |
$this->echoLine('Running Behat... ', false); | |
exec($this->binDir.'/behat -p daniel', $output, $returnCode); | |
if ($returnCode !== 0) { | |
$this->echoLine($this->red('Failed')); | |
$this->echoLine('Behat Output: '); | |
$this->echoLine(implode(PHP_EOL, $output)); | |
$this->echoLine($this->red('COMMIT FAILED')); | |
exit(1); | |
} | |
$this->echoLine($this->green('Success')); | |
} | |
} | |
$hook = new PreCommitHook(); | |
$hook->phpUnit(); | |
$hook->phpBehat(); | |
exit(0); |
Update #2:
- Added credit to original code even though there's now little in common 👅
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update #1:
$binDir
property as most people probably actually usevendor/bin
rather than mybin