Last active
September 19, 2017 16:34
-
-
Save manchuck/6538826 to your computer and use it in GitHub Desktop.
This is a ZF2 precommit hook that runs Lint, Mess Detector, Code sniffer then classmap builder on commit
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
#!/bin/sh | |
echo | |
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1' | |
exit |
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
############################################################################### | |
# | |
# PHP Syntax Check for Git pre-commit hook for Windows PowerShell | |
# | |
# Author: Vojtech Kusy <[email protected]> | |
# Author: Chuck "MANCHUCK" Reeves <[email protected]> | |
# | |
############################################################################### | |
### INSTRUCTIONS ### | |
# Place the code to file "pre-commit" (no extension) and add it to the one of | |
# the following locations: | |
# 1) Repository hooks folder - C:\Path\To\Repository\.git\hooks | |
# 2) User profile template - C:\Users\<USER>\.git\templates\hooks | |
# 3) Global shared templates - C:\Program Files (x86)\Git\share\git-core\templates\hooks | |
# | |
# The hooks from user profile or from shared templates are copied from there | |
# each time you create or clone new repository. | |
### SETTINGS ### | |
# Path to the php.exe | |
$php_exe = "C:\php\php.exe"; | |
# Path to the phpcs | |
$php_cs = "vendor\bin\phpcs.bat"; | |
# Path to the phpmd | |
$php_md = "vendor\bin\phpmd.bat"; | |
# Path to Zend Framework classmap generator | |
$zend_classmap = "vendor\zendframework\zendframework\bin\classmap_generator.php"; | |
# Extensions of the PHP files | |
$php_ext = "php|phtml" | |
# Flag, if set to $true git will unstage all files with errors, set to $false to disable | |
$unstage_on_error = $false; | |
### FUNCTIONS ### | |
## PHP Lint | |
function php_syntax_check { | |
param([string]$php_bin, [string]$extensions, [bool]$reset) | |
$err_counter = 0; | |
$file_counter = 0; | |
write-host "PHP syntax check:" -foregroundcolor "white" -backgroundcolor "black" | |
# loop through all commited files | |
git diff-index --name-only --cached --diff-filter=AM HEAD -- | foreach { | |
# only match php files | |
if ($_ -match ".*\.($extensions)$") { | |
$file_counter++; | |
$file = $matches[0]; | |
$errors = & $php_bin -l $file | |
write-host $file ": " -foregroundcolor "gray" -backgroundcolor "black" -NoNewline | |
if ($errors -match "No syntax errors detected in $file") { | |
write-host "OK!" -foregroundcolor "green" -backgroundcolor "black" | |
} else { | |
write-host "ERROR! " $errors -foregroundcolor "red" -backgroundcolor "black" | |
if ($reset) { | |
git reset -q HEAD $file | |
write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black" | |
} | |
$err_counter++ | |
} | |
} | |
} | |
# output report | |
write-host "Checked" $file_counter "File(s)" -foregroundcolor "gray" -backgroundcolor "black" | |
if ($err_counter -gt 0) { | |
write-host "Some File(s) have syntax errors. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black" | |
exit 1 | |
} | |
} | |
# PHP Code Sniffer Check | |
function php_cs_check { | |
param([string]$php_cs, [string]$extensions, [bool]$reset) | |
$err_counter = 0; | |
$file_counter = 0; | |
write-host "PHP codesniffer check:" -foregroundcolor "white" -backgroundcolor "black" | |
# Loop through all commited files | |
git diff-index --name-only --cached --diff-filter=AM HEAD -- | foreach { | |
# only run lint if file extensions match | |
if ($_ -match ".*\.($extensions)$") { | |
$file_counter++; | |
$file = $matches[0]; | |
write-host $file ": " -foregroundcolor "gray" -backgroundcolor "black" -NoNewline | |
# skip test files | |
if ($file -match "test\/") { | |
write-host "SKIPPED! (test file)" -foregroundcolor "darkGreen" -backgroundcolor "black" | |
} else { | |
$errors = & $php_cs --standard=PSR2 $file | |
# Outputs the error | |
if ($LastExitCode) { | |
write-host "FAILED! (contains errors)" -foregroundcolor "red" -backgroundcolor "black" | |
write-host | |
write-host $errors | |
if ($reset) { | |
git reset -q HEAD $file | |
write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black" | |
} | |
$err_counter++ | |
} else { | |
write-host "PASSED!" -foregroundcolor "green" -backgroundcolor "black" | |
} | |
} | |
} | |
} | |
# output report | |
write-host "Checked" $file_counter "File(s)" -foregroundcolor "gray" -backgroundcolor "black" | |
if ($err_counter -gt 0) { | |
write-host "Some File(s) are not following proper codeing standards. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black" | |
exit 1 | |
} | |
} | |
# php mess detector | |
function php_md_check { | |
param([string]$php_cs, [string]$extensions, [bool]$reset) | |
$err_counter = 0; | |
$file_counter = 0; | |
write-host "PHP Mess Detector check:" -foregroundcolor "white" -backgroundcolor "black" | |
# loop throuh all commited files | |
git diff-index --name-only --cached --diff-filter=AM HEAD -- | foreach { | |
# only match php files | |
if ($_ -match ".*\.($extensions)$") { | |
$file_counter++; | |
$file = $matches[0]; | |
write-host $file ": " -foregroundcolor "gray" -backgroundcolor "black" -NoNewline | |
# forget test files | |
if ($file -match "test\/") { | |
write-host "SKIPPED! (test file)" -foregroundcolor "darkGreen" -backgroundcolor "black" | |
} else { | |
$errors = & $php_md $file text "codesize,naming,unusedcode" | |
# output errors | |
if ($LastExitCode) { | |
write-host "FAILED! " $errors -foregroundcolor "red" -backgroundcolor "black" | |
write-host | |
write-host $errors | |
if ($reset) { | |
git reset -q HEAD $file | |
write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black" | |
} | |
$err_counter++ | |
} else { | |
write-host "PASSED!" -foregroundcolor "green" -backgroundcolor "black" | |
} | |
} | |
} | |
} | |
# output report | |
write-host "Checked" $file_counter "File(s)" -foregroundcolor "gray" -backgroundcolor "black" | |
if ($err_counter -gt 0) { | |
write-host "Some File(s) are messy. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black" | |
exit 1 | |
} | |
} | |
# zend framework 2 class map generator | |
function zf_autoload { | |
param([string]$php_exe, [string]$zend_classmap, [string]$extensions, [bool]$reset) | |
write-host "Zend Classmap:" -foregroundcolor "white" -backgroundcolor "black" | |
ls -d src -- | foreach { | |
$dir = "src\" + $_ | |
$alFile = "autoload_classmap.php" | |
write-host "Executing: " $php_exe $zend_classmap --overwrite --output=$alFile -l $dir -foregroundcolor "gray" -backgroundcolor "black" -NoNewline | |
write-host | |
$errors = & $php_exe $zend_classmap --overwrite --output=$alFile -l $dir | |
write-host "Adding: " $alFile -foregroundcolor "gray" -backgroundcolor "black" -NoNewline | |
write-host | |
$errors = & git add $alFile | |
} | |
} | |
### MAIN ### | |
php_syntax_check $php_exe "php|phtml" $unstage_on_error | |
write-host | |
php_cs_check $php_cs "php" $unstage_on_error | |
write-host | |
php_md_check $php_md "php" $unstage_on_error | |
write-host | |
zf_autoload $php_exe $zend_classmap "php" $unstage_on_error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment