5
5
use Illuminate \Console \Command ;
6
6
use Laravel \Prompts \Concerns \Colors ;
7
7
use Symfony \Component \Process \Process ;
8
+
8
9
use function Laravel \Prompts \confirm ;
9
10
use function Laravel \Prompts \multiSelect ;
10
11
use function Laravel \Prompts \spin ;
@@ -15,11 +16,12 @@ class CommitvelCommand extends Command
15
16
use Colors;
16
17
17
18
protected $ signature = 'cw:commitvel ' ;
19
+
18
20
protected $ description = 'Like a pre-commit hook for running Pint, PHPPest, and managing Git operations written with laravel prompt ' ;
19
21
20
22
public function handle (): void
21
23
{
22
- if (!$ this ->hasChanges ()) {
24
+ if (! $ this ->hasChanges ()) {
23
25
$ this ->tellJokeAndExit ();
24
26
}
25
27
@@ -34,25 +36,26 @@ public function handle(): void
34
36
protected function hasChanges (): bool
35
37
{
36
38
$ changes = shell_exec ('git status --porcelain ' );
37
- $ statusMessage = "Initial git status changes: \n" . $ this ->formatGitStatus ($ changes );
39
+ $ statusMessage = "Initial git status changes: \n" . $ this ->formatGitStatus ($ changes );
38
40
39
41
if (preg_match ('/^\?\?/ ' , $ changes )) {
40
42
$ this ->info ($ this ->red ($ statusMessage ));
41
43
} else {
42
44
$ this ->info ($ statusMessage );
43
45
}
44
46
45
- return !empty (trim ($ changes ));
47
+ return ! empty (trim ($ changes ));
46
48
}
47
49
48
50
protected function formatGitStatus (string $ status ): string
49
51
{
50
52
$ lines = array_filter (explode ("\n" , $ status ));
51
53
foreach ($ lines as &$ line ) {
52
54
if (str_starts_with (trim ($ line ), '?? ' )) {
53
- $ line = $ this ->bgRed ($ this ->white ('NEW ' )) . ' ' . ltrim ($ line , '?? ' );
55
+ $ line = $ this ->bgRed ($ this ->white ('NEW ' )). ' ' . ltrim ($ line , '?? ' );
54
56
}
55
57
}
58
+
56
59
return implode ("\n" , $ lines );
57
60
}
58
61
@@ -81,11 +84,11 @@ protected function tellJokeAndExit(): void
81
84
'Attempting an empty commit? It’s as productive as a screen door on a submarine! 🛳️🚪 ' ,
82
85
'Committing no changes? That’s like trying to tune a guitar with no strings! 🎸❌ ' ,
83
86
'No changes? That’s like sending a love letter to an empty mailbox! 💌📪 ' ,
84
- " Trying to commit nothing? That’s as useful as a waterproof teabag! ☕🚫 " ,
87
+ ' Trying to commit nothing? That’s as useful as a waterproof teabag! ☕🚫 ' ,
85
88
'No changes to commit? Even ghosts leave more trace! 👻🕵️♂️ ' ,
86
89
'Attempting an empty commit? That’s like cooking with imaginary ingredients! 🍳🥄 ' ,
87
90
'No changes in your commit? That’s like racing in a stationary car! 🚗🛑 ' ,
88
- " Committing air? That’s like showing up to a concert with earplugs in! 🎤👂 " ,
91
+ ' Committing air? That’s like showing up to a concert with earplugs in! 🎤👂 ' ,
89
92
'Trying to commit with zero changes? That’s like playing soccer with an invisible ball! ⚽🕵️♀️ ' ,
90
93
];
91
94
@@ -111,9 +114,8 @@ private function ensureToolInstalled(
111
114
string $ successMessage ,
112
115
string $ preCommand = '' ,
113
116
string $ preMessage = ''
114
- ): void
115
- {
116
- if (!file_exists (base_path ($ path ))) {
117
+ ): void {
118
+ if (! file_exists (base_path ($ path ))) {
117
119
if (confirm ($ confirmMessage , true )) {
118
120
if ($ preCommand ) {
119
121
$ this ->info ($ preMessage );
@@ -133,8 +135,9 @@ protected function runPint(): void
133
135
$ outputFiles = $ this ->runTool ('Laravel Pint ' , './vendor/bin/pint --dirty ' , '/^\s+√ (\S+)/m ' );
134
136
135
137
foreach ($ outputFiles as $ file ) {
136
- if (!is_writable ($ file )) {
138
+ if (! is_writable ($ file )) {
137
139
$ this ->warn ("File $ file is not writable. " );
140
+
138
141
continue ;
139
142
}
140
143
@@ -153,8 +156,7 @@ private function runTool(
153
156
string $ regexPattern = '' ,
154
157
string $ errorKeyword1 = 'FAIL ' ,
155
158
string $ errorKeyword2 = 'ERROR '
156
- ): array
157
- {
159
+ ): array {
158
160
$ outputFiles = [];
159
161
if (confirm ("Would you like to run $ toolName? " , true )) {
160
162
spin (
@@ -185,10 +187,10 @@ protected function stageFixedFiles(): void
185
187
{
186
188
$ untrackedFiles = $ this ->getUntrackedFiles ();
187
189
188
- if (!empty ($ untrackedFiles )) {
190
+ if (! empty ($ untrackedFiles )) {
189
191
$ this ->info ($ this ->red ('Untracked files found: ' ));
190
192
foreach ($ untrackedFiles as $ file ) {
191
- $ this ->info ($ this ->red (' ' . $ file ));
193
+ $ this ->info ($ this ->red (' ' . $ file ));
192
194
}
193
195
194
196
array_unshift ($ untrackedFiles , 'Select All ' , 'Select None ' );
@@ -209,7 +211,7 @@ protected function stageFixedFiles(): void
209
211
}
210
212
211
213
$ filesToDelete = array_diff ($ untrackedFiles , $ selectedFiles , ['Select All ' , 'Select None ' ]);
212
- if (!empty ($ filesToDelete ) && confirm ('Would you like to delete the unselected new files? You can delete it manually if you choose no. ' , false )) {
214
+ if (! empty ($ filesToDelete ) && confirm ('Would you like to delete the unselected new files? You can delete it manually if you choose no. ' , false )) {
213
215
foreach ($ filesToDelete as $ file ) {
214
216
unlink ($ file );
215
217
$ this ->info ("Deleted file: $ file " );
@@ -228,16 +230,17 @@ protected function stageFixedFiles(): void
228
230
229
231
protected function stageFile (string $ file ): void
230
232
{
231
- shell_exec ('git add ' . escapeshellarg ($ file ));
232
- $ stagedStatus = shell_exec ('git status --porcelain ' . escapeshellarg ($ file ));
233
- if (!empty ($ stagedStatus )) {
234
- $ this ->info ("Staging status for $ file: \n" . $ stagedStatus );
233
+ shell_exec ('git add ' . escapeshellarg ($ file ));
234
+ $ stagedStatus = shell_exec ('git status --porcelain ' . escapeshellarg ($ file ));
235
+ if (! empty ($ stagedStatus )) {
236
+ $ this ->info ("Staging status for $ file: \n" . $ stagedStatus );
235
237
}
236
238
}
237
239
238
240
protected function getUntrackedFiles (): array
239
241
{
240
242
$ output = shell_exec ('git ls-files --others --exclude-standard ' );
243
+
241
244
return array_filter (explode ("\n" , $ output ));
242
245
}
243
246
@@ -250,28 +253,27 @@ protected function commitChanges(): void
250
253
}
251
254
252
255
$ commitMessage = text ('Enter the commit message ' );
253
- if (!$ commitMessage ) {
256
+ if (! $ commitMessage ) {
254
257
$ this ->error ('Commit message cannot be empty. ' );
255
258
exit (1 );
256
259
}
257
260
258
- shell_exec ('git commit -m ' . escapeshellarg ($ commitMessage ));
261
+ shell_exec ('git commit -m ' . escapeshellarg ($ commitMessage ));
259
262
$ this ->info ('Changes committed. ' );
260
263
261
264
$ currentBranch = $ this ->getCurrentBranch ();
262
265
263
-
264
266
if (confirm ("We will push this to branch [ $ currentBranch]. " , true )) {
265
267
$ this ->info ("Pushing code to branch $ currentBranch... " );
266
268
267
- spin (fn () => $ this ->pushToBranch ($ currentBranch ), "Pushing code to $ currentBranch... " );
269
+ spin (fn () => $ this ->pushToBranch ($ currentBranch ), "Pushing code to $ currentBranch... " );
268
270
269
271
$ commitHash = trim (shell_exec ('git log -1 --format="%H" ' ));
270
272
$ gitUserName = trim (shell_exec ('git config user.name ' ));
271
273
$ this ->info ("Commit hash: $ commitHash " );
272
274
$ this ->info ("Pushed by: $ gitUserName " );
273
275
} else {
274
- $ this ->info (" Exiting. No changes will be pushed to the server, but changes have been committed locally. " );
276
+ $ this ->info (' Exiting. No changes will be pushed to the server, but changes have been committed locally. ' );
275
277
}
276
278
}
277
279
0 commit comments