8
8
use Igorsgm \GitHooks \Facades \GitHooks ;
9
9
use Igorsgm \GitHooks \Git \ChangedFiles ;
10
10
use Igorsgm \GitHooks \Traits \ConsoleHelper ;
11
+ use Symfony \Component \Console \Terminal ;
11
12
12
13
class PintPreCommitHook implements PreCommitHook
13
14
{
14
15
use ConsoleHelper;
15
16
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
+
16
32
public function getName (): ?string
17
33
{
18
34
return 'Laravel Pint ' ;
19
35
}
20
36
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
+
21
51
public function handle (ChangedFiles $ files , Closure $ next )
22
52
{
23
53
$ stagedFilePaths = $ files ->getStaged ()->map (function ($ file ) {
@@ -28,36 +58,28 @@ public function handle(ChangedFiles $files, Closure $next)
28
58
return $ next ($ files );
29
59
}
30
60
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 ();
40
62
41
- $ errorFiles = [];
42
63
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 ),
44
66
[
45
67
'cwd ' => base_path (),
46
68
])->isSuccessful ();
47
69
48
70
if (! $ isPintProperlyFormatted ) {
49
- if (empty ($ errorFiles )) {
71
+ if (empty ($ this -> filesBadlyFormatted )) {
50
72
$ this ->newLine ();
51
73
}
52
74
53
75
$ this ->output ->writeln (
54
76
sprintf ('<fg=red> Pint Failed:</> %s ' , "$ stagedFilePath " )
55
77
);
56
- $ errorFiles [] = $ stagedFilePath ;
78
+ $ this -> filesBadlyFormatted [] = $ stagedFilePath ;
57
79
}
58
80
}
59
81
60
- if (empty ($ errorFiles )) {
82
+ if (empty ($ this -> filesBadlyFormatted )) {
61
83
return $ next ($ files );
62
84
}
63
85
@@ -67,11 +89,45 @@ public function handle(ChangedFiles $files, Closure $next)
67
89
'Your commit contains files that should pass Pint but do not. Please fix the Pint errors in the files above and try again. ' )
68
90
);
69
91
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 );
72
128
$ this ->runCommands (
73
129
[
74
- ' ./vendor/bin/pint --config ./pint.json ' . $ errorFilesString ,
130
+ sprintf ( ' %s --config %s %s ' , $ this -> pintExecutable , $ this -> pintConfig , $ errorFilesString) ,
75
131
'git add ' .$ errorFilesString ,
76
132
],
77
133
['cwd ' => base_path ()]
@@ -80,9 +136,4 @@ public function handle(ChangedFiles $files, Closure $next)
80
136
throw new HookFailException ();
81
137
}
82
138
}
83
-
84
- public function isPintInstalled ()
85
- {
86
- return file_exists (base_path ('vendor/bin/pint ' ));
87
- }
88
139
}
0 commit comments