|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Igorsgm\GitHooks\Console\Commands\Hooks; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Igorsgm\GitHooks\Contracts\PreCommitHook; |
| 7 | +use Igorsgm\GitHooks\Exceptions\HookFailException; |
| 8 | +use Igorsgm\GitHooks\Facades\GitHooks; |
| 9 | +use Igorsgm\GitHooks\Git\ChangedFiles; |
| 10 | +use Igorsgm\GitHooks\Traits\ProcessHelper; |
| 11 | +use Illuminate\Console\Command; |
| 12 | +use Symfony\Component\Console\Terminal; |
| 13 | + |
| 14 | +class PintPreCommitHook implements PreCommitHook |
| 15 | +{ |
| 16 | + use ProcessHelper; |
| 17 | + |
| 18 | + /** |
| 19 | + * Command instance that is bound automatically by Hooks Pipeline, so it can be used inside the Hook. |
| 20 | + * |
| 21 | + * @var Command |
| 22 | + */ |
| 23 | + public $command; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + private $pintExecutable; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $filesBadlyFormatted = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Create a new console command instance. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function __construct() |
| 41 | + { |
| 42 | + $this->pintExecutable = './'.trim(config('git-hooks.code_analyzers.laravel_pint.path'), '/'); |
| 43 | + } |
| 44 | + |
| 45 | + public function getName(): ?string |
| 46 | + { |
| 47 | + return 'Laravel Pint'; |
| 48 | + } |
| 49 | + |
| 50 | + public function handle(ChangedFiles $files, Closure $next) |
| 51 | + { |
| 52 | + $stagedFilePaths = $files->getStaged()->map(function ($file) { |
| 53 | + return $file->getFilePath(); |
| 54 | + })->toArray(); |
| 55 | + |
| 56 | + if (empty($stagedFilePaths) || GitHooks::isMergeInProgress()) { |
| 57 | + return $next($files); |
| 58 | + } |
| 59 | + |
| 60 | + $this->validatePintInstallation(); |
| 61 | + |
| 62 | + foreach ($stagedFilePaths as $stagedFilePath) { |
| 63 | + $isPintProperlyFormatted = $this->runCommands( |
| 64 | + implode(' ', [ |
| 65 | + $this->pintExecutable, |
| 66 | + '--test', |
| 67 | + $this->getPintConfigParam(), |
| 68 | + $stagedFilePath, |
| 69 | + ]), |
| 70 | + [ |
| 71 | + 'cwd' => base_path(), |
| 72 | + ])->isSuccessful(); |
| 73 | + |
| 74 | + if (! $isPintProperlyFormatted) { |
| 75 | + if (empty($this->filesBadlyFormatted)) { |
| 76 | + $this->command->newLine(); |
| 77 | + } |
| 78 | + |
| 79 | + $this->command->getOutput()->writeln( |
| 80 | + sprintf('<fg=red> Pint Failed:</> %s', "$stagedFilePath") |
| 81 | + ); |
| 82 | + $this->filesBadlyFormatted[] = $stagedFilePath; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (empty($this->filesBadlyFormatted)) { |
| 87 | + return $next($files); |
| 88 | + } |
| 89 | + |
| 90 | + $this->command->newLine(); |
| 91 | + $this->command->getOutput()->writeln( |
| 92 | + sprintf('<bg=red;fg=white> COMMIT FAILED </> %s', |
| 93 | + 'Your commit contains files that should pass Pint but do not. Please fix the Pint errors in the files above and try again.') |
| 94 | + ); |
| 95 | + |
| 96 | + $this->suggestAutoFixOrExit(); |
| 97 | + } |
| 98 | + |
| 99 | + private function getPintConfigParam(): string |
| 100 | + { |
| 101 | + $pintConfigFile = trim(config('git-hooks.code_analyzers.laravel_pint.config'), '/'); |
| 102 | + return empty($pintConfigFile) ? '' : '--config ./'.$pintConfigFile; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return void |
| 107 | + * |
| 108 | + * @throws HookFailException |
| 109 | + */ |
| 110 | + private function validatePintInstallation() |
| 111 | + { |
| 112 | + $isPintInstalled = file_exists(base_path(config('git-hooks.code_analyzers.laravel_pint.path'))); |
| 113 | + |
| 114 | + if ($isPintInstalled) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $this->command->newLine(2); |
| 119 | + $this->command->getOutput()->writeln( |
| 120 | + sprintf('<bg=red;fg=white> ERROR </> %s', |
| 121 | + 'Pint is not installed. Please run <info>composer require laravel/pint --dev</info> to install it.') |
| 122 | + ); |
| 123 | + $this->command->newLine(); |
| 124 | + throw new HookFailException(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return void |
| 129 | + * |
| 130 | + * @throws HookFailException |
| 131 | + */ |
| 132 | + private function suggestAutoFixOrExit() |
| 133 | + { |
| 134 | + if (Terminal::hasSttyAvailable() && |
| 135 | + $this->command->confirm('Would you like to attempt to correct files automagically?', false) |
| 136 | + ) { |
| 137 | + $errorFilesString = implode(' ', $this->filesBadlyFormatted); |
| 138 | + $this->runCommands( |
| 139 | + [ |
| 140 | + implode(' ', [ |
| 141 | + $this->pintExecutable, |
| 142 | + $this->getPintConfigParam(), |
| 143 | + $errorFilesString, |
| 144 | + ]), |
| 145 | + 'git add '.$errorFilesString, |
| 146 | + ], |
| 147 | + ['cwd' => base_path()] |
| 148 | + ); |
| 149 | + } else { |
| 150 | + throw new HookFailException(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments