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