Skip to content

Commit 6ac1e81

Browse files
committed
wip: PintPreCommitHook command tweaks
1 parent d64e3b4 commit 6ac1e81

File tree

4 files changed

+97
-23
lines changed

4 files changed

+97
-23
lines changed

config/git-hooks.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,23 @@
155155
'pre-push' => [
156156

157157
],
158+
159+
/*
160+
|--------------------------------------------------------------------------
161+
| Code Analyzers Configuration
162+
|--------------------------------------------------------------------------
163+
|
164+
| The following variables are used to store the paths to bin executables for
165+
| various code analyzer dependencies used in your Laravel application.
166+
| This configuration node allows you to set up the paths for these executables.
167+
| Here you can also specify the path to the configuration files they use to customize their behavior
168+
|
169+
*/
170+
'code_analyzers' => [
171+
'laravel_pint' => [
172+
'path' => env('LARAVEL_PINT_PATH', 'vendor/bin/pint'),
173+
'config' => env('LARAVEL_PINT_CONFIG', 'pint.json'),
174+
],
175+
],
176+
158177
];

src/Console/Commands/Hooks/PintPreCommitHook.php

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,46 @@
88
use Igorsgm\GitHooks\Facades\GitHooks;
99
use Igorsgm\GitHooks\Git\ChangedFiles;
1010
use Igorsgm\GitHooks\Traits\ConsoleHelper;
11+
use Symfony\Component\Console\Terminal;
1112

1213
class PintPreCommitHook implements PreCommitHook
1314
{
1415
use ConsoleHelper;
1516

17+
/**
18+
* @var string
19+
*/
20+
private $pintExecutable;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $pintConfig;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $filesBadlyFormatted;
31+
1632
public function getName(): ?string
1733
{
1834
return 'Laravel Pint';
1935
}
2036

37+
/**
38+
* Create a new console command instance.
39+
*
40+
* @return void
41+
*/
42+
public function __construct($argInput = '')
43+
{
44+
$this->initConsole($argInput);
45+
46+
$this->pintExecutable = './'.trim(config('git-hooks.code_analyzers.laravel_pint.path'), '/');
47+
$this->pintConfig = './'.trim(config('git-hooks.code_analyzers.laravel_pint.config'), '/');
48+
$this->filesBadlyFormatted = [];
49+
}
50+
2151
public function handle(ChangedFiles $files, Closure $next)
2252
{
2353
$stagedFilePaths = $files->getStaged()->map(function ($file) {
@@ -28,36 +58,28 @@ public function handle(ChangedFiles $files, Closure $next)
2858
return $next($files);
2959
}
3060

31-
if (! $this->isPintInstalled()) {
32-
$this->newLine(2);
33-
$this->output->writeln(
34-
sprintf('<bg=red;fg=white> ERROR </> %s',
35-
'Pint is not installed. Please run <info>composer require laravel/pint --dev</info> to install it.')
36-
);
37-
$this->newLine();
38-
throw new HookFailException();
39-
}
61+
$this->validatePintInstallation();
4062

41-
$errorFiles = [];
4263
foreach ($stagedFilePaths as $stagedFilePath) {
43-
$isPintProperlyFormatted = $this->runCommands('./vendor/bin/pint --test --config ./pint.json '.$stagedFilePath,
64+
$isPintProperlyFormatted = $this->runCommands(
65+
sprintf('%s --test --config %s %s', $this->pintExecutable, $this->pintConfig, $stagedFilePath),
4466
[
4567
'cwd' => base_path(),
4668
])->isSuccessful();
4769

4870
if (! $isPintProperlyFormatted) {
49-
if (empty($errorFiles)) {
71+
if (empty($this->filesBadlyFormatted)) {
5072
$this->newLine();
5173
}
5274

5375
$this->output->writeln(
5476
sprintf('<fg=red> Pint Failed:</> %s', "$stagedFilePath")
5577
);
56-
$errorFiles[] = $stagedFilePath;
78+
$this->filesBadlyFormatted[] = $stagedFilePath;
5779
}
5880
}
5981

60-
if (empty($errorFiles)) {
82+
if (empty($this->filesBadlyFormatted)) {
6183
return $next($files);
6284
}
6385

@@ -67,11 +89,45 @@ public function handle(ChangedFiles $files, Closure $next)
6789
'Your commit contains files that should pass Pint but do not. Please fix the Pint errors in the files above and try again.')
6890
);
6991

70-
if ($this->confirm('Would you like to attempt to correct files automagically?', false)) {
71-
$errorFilesString = implode(' ', $errorFiles);
92+
$this->suggestAutoFixOrExit();
93+
}
94+
95+
/**
96+
* @return void
97+
*
98+
* @throws HookFailException
99+
*/
100+
private function validatePintInstallation()
101+
{
102+
$isPintInstalled = file_exists(base_path(config('git-hooks.code_analyzers.laravel_pint.path')));
103+
104+
if ($isPintInstalled) {
105+
return;
106+
}
107+
108+
$this->newLine(2);
109+
$this->output->writeln(
110+
sprintf('<bg=red;fg=white> ERROR </> %s',
111+
'Pint is not installed. Please run <info>composer require laravel/pint --dev</info> to install it.')
112+
);
113+
$this->newLine();
114+
throw new HookFailException();
115+
}
116+
117+
/**
118+
* @return void
119+
*
120+
* @throws HookFailException
121+
*/
122+
private function suggestAutoFixOrExit()
123+
{
124+
if (Terminal::hasSttyAvailable() &&
125+
$this->confirm('Would you like to attempt to correct files automagically?', false)
126+
) {
127+
$errorFilesString = implode(' ', $this->filesBadlyFormatted);
72128
$this->runCommands(
73129
[
74-
'./vendor/bin/pint --config ./pint.json '.$errorFilesString,
130+
sprintf('%s --config %s %s', $this->pintExecutable, $this->pintConfig, $errorFilesString),
75131
'git add '.$errorFilesString,
76132
],
77133
['cwd' => base_path()]
@@ -80,9 +136,4 @@ public function handle(ChangedFiles $files, Closure $next)
80136
throw new HookFailException();
81137
}
82138
}
83-
84-
public function isPintInstalled()
85-
{
86-
return file_exists(base_path('vendor/bin/pint'));
87-
}
88139
}

src/Console/Commands/stubs/hook

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/bin/sh
22

3+
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
4+
exec < /dev/tty
5+
fi
6+
37
php {path}/artisan {command} $@ >&2

src/Traits/ConsoleHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait ConsoleHelper
3333
*
3434
* @return void
3535
*/
36-
public function __construct($argInput = '')
36+
public function initConsole($argInput = '')
3737
{
3838
$this->input = new StringInput($argInput);
3939
$this->consoleOutput = new ConsoleOutput();

0 commit comments

Comments
 (0)