13
13
namespace App \Commands ;
14
14
15
15
use App \ConfigManager ;
16
- use App \Exceptions \TaskException ;
16
+ use App \Exceptions \RuntimeException ;
17
17
use App \GeneratorManager ;
18
18
use App \Support \JsonFixer ;
19
19
use Illuminate \Console \Scheduling \Schedule ;
@@ -59,92 +59,94 @@ public function __construct(GeneratorManager $generatorManager)
59
59
60
60
/**
61
61
* @psalm-suppress InvalidArgument
62
+ *
63
+ * @throws \Exception
62
64
*/
63
65
public function handle (): void
64
66
{
65
- // Ensure git is installed and the current directory is a git repository.
66
- $ this ->createProcess (['git ' , 'rev-parse ' , '--is-inside-work-tree ' ])->mustRun ();
67
-
68
- $ cachedDiff = $ this ->option ('diff ' ) ?: $ this ->createProcess ($ this ->diffCommand ())->mustRun ()->getOutput ();
69
- if ('' === $ cachedDiff ) {
70
- throw new TaskException ('There are no cached files to commit. Try running `git add` to cache some files. ' );
71
- }
72
-
73
- $ type = $ this ->choice (
74
- 'Please choice commit type ' ,
75
- $ types = $ this ->configManager ->get ('types ' ),
76
- array_key_first ($ types )
77
- );
78
-
79
- $ message = retry (
80
- $ this ->option ('retry-times ' ),
81
- function ($ attempts ) use ($ cachedDiff , $ type ): string {
82
- if ($ attempts > 1 ) {
83
- $ this ->output ->note ('retrying... ' );
67
+ collect ()
68
+ ->tap (function () use (&$ message ): void {
69
+ // Ensure git is installed and the current directory is a git repository.
70
+ $ this ->createProcess (['git ' , 'rev-parse ' , '--is-inside-work-tree ' ])->mustRun ();
71
+
72
+ $ cachedDiff = $ this ->option ('diff ' ) ?: $ this ->createProcess ($ this ->diffCommand ())->mustRun ()->getOutput ();
73
+ if (empty ($ cachedDiff )) {
74
+ throw new RuntimeException ('There are no cached files to commit. Try running `git add` to cache some files. ' );
84
75
}
85
76
86
- $ originalMessage = $ this ->generatorManager
87
- ->driver ($ this ->option ('generator ' ))
88
- ->generate ($ this ->promptFor ($ cachedDiff , $ type ));
89
- $ message = $ this ->tryFixMessage ($ originalMessage );
90
- if (! str ($ message )->jsonValidate ()) {
91
- throw new TaskException (sprintf (
92
- 'The generated commit message(%s) is an invalid JSON. ' ,
93
- var_export ($ originalMessage , true )
94
- ));
95
- }
77
+ $ type = $ this ->choice (
78
+ 'Please choice commit type ' ,
79
+ $ types = $ this ->configManager ->get ('types ' ),
80
+ array_key_first ($ types )
81
+ );
82
+
83
+ $ message = retry (
84
+ $ this ->option ('retry-times ' ),
85
+ function ($ attempts ) use ($ cachedDiff , $ type ): string {
86
+ if ($ attempts > 1 ) {
87
+ $ this ->output ->note ('retrying... ' );
88
+ }
89
+
90
+ $ originalMessage = $ this ->generatorManager
91
+ ->driver ($ this ->option ('generator ' ))
92
+ ->generate ($ this ->promptFor ($ cachedDiff , $ type ));
93
+ $ message = $ this ->tryFixMessage ($ originalMessage );
94
+ if (! str ($ message )->jsonValidate ()) {
95
+ throw new RuntimeException (sprintf (
96
+ 'The generated commit message(%s) is an invalid JSON. ' ,
97
+ var_export ($ originalMessage , true )
98
+ ));
99
+ }
100
+
101
+ return $ message ;
102
+ },
103
+ $ this ->option ('retry-sleep ' ),
104
+ $ this ->configManager ->get ('retry.when ' )
105
+ );
106
+ })
107
+ ->tap (function () use (&$ message ): void {
108
+ $ message = collect (json_decode ($ message , true , 512 , JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR ))
109
+ ->map (static function ($ content ) {
110
+ if (\is_array ($ content )) {
111
+ return collect ($ content )
112
+ ->transform (static function (string $ line ): string {
113
+ return (string ) str ($ line )->trim (" \t\n\r\x0B" )->start ('- ' );
114
+ })
115
+ ->implode (PHP_EOL );
116
+ }
117
+
118
+ return $ content ;
119
+ })
120
+ ->tap (function (Collection $ message ): void {
121
+ $ message = $ message ->put ('' , '' )->sortKeysUsing (static function (string $ a , string $ b ): int {
122
+ $ rules = ['subject ' , '' , 'body ' ];
123
+
124
+ return array_search ($ a , $ rules , true ) <=> array_search ($ b , $ rules , true );
125
+ });
126
+ // $this->table($message->keys()->all(), [$message->all()]);
127
+ $ this ->output ->horizontalTable ($ message ->keys ()->all (), [$ message ->all ()]);
128
+ })
129
+ ->tap (function (): void {
130
+ if (! $ this ->confirm ('Do you want to commit this message? ' , true )) {
131
+ $ this ->output ->note ('regenerating... ' );
132
+ $ this ->handle ();
133
+ }
134
+ });
135
+ })
136
+ ->tap (function () use ($ message ): void {
137
+ if ($ this ->option ('dry-run ' )) {
138
+ $ this ->info ($ this ->hydrateMessage ($ message ));
96
139
97
- return $ message ;
98
- },
99
- $ this ->option ('retry-sleep ' ),
100
- $ this ->configManager ->get ('retry.when ' )
101
- );
102
- // $this->task('1. Generating commit message', function () use (&$message): void {
103
- // }, 'generating...'.PHP_EOL);
104
-
105
- $ message = collect (json_decode ($ message , true , 512 , JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR ))
106
- ->map (static function ($ content ) {
107
- if (\is_array ($ content )) {
108
- return collect ($ content )
109
- ->transform (static function (string $ line ): string {
110
- return (string ) str ($ line )->trim (" \t\n\r\x0B" )->start ('- ' );
111
- })
112
- ->implode (PHP_EOL );
140
+ return ;
113
141
}
114
142
115
- return $ content ;
116
- })
117
- ->tap (function (Collection $ message ): void {
118
- $ message = $ message ->put ('' , '' )->sortKeysUsing (static function (string $ a , string $ b ): int {
119
- $ rules = ['subject ' , '' , 'body ' ];
120
-
121
- return array_search ($ a , $ rules , true ) <=> array_search ($ b , $ rules , true );
122
- });
123
- // $this->table($message->keys()->all(), [$message->all()]);
124
- $ this ->output ->horizontalTable ($ message ->keys ()->all (), [$ message ->all ()]);
143
+ tap ($ this ->createProcess ($ this ->commitCommandFor ($ message )), function (Process $ process ): void {
144
+ $ this ->shouldEdit () and $ process ->setTty (true );
145
+ })->setTimeout (null )->mustRun ();
125
146
})
126
147
->tap (function (): void {
127
- if (! $ this ->confirm ('Do you want to commit this message? ' , true )) {
128
- $ this ->output ->note ('regenerating... ' );
129
- $ this ->handle ();
130
- }
148
+ $ this ->output ->success ('Successfully generated and committed message. ' );
131
149
});
132
- // $this->task(PHP_EOL.'2. Confirming commit message', function () use (&$message): void {
133
- // }, 'confirming...'.PHP_EOL);
134
-
135
- if ($ this ->option ('dry-run ' )) {
136
- $ this ->info ($ this ->hydrateMessage ($ message ));
137
-
138
- return ;
139
- }
140
-
141
- tap ($ this ->createProcess ($ this ->commitCommandFor ($ message )), function (Process $ process ): void {
142
- $ this ->shouldEdit () and $ process ->setTty (true );
143
- })->setTimeout (null )->mustRun ();
144
- // $this->task(PHP_EOL.'3. Committing message', function (): void {
145
- // }, 'committing...'.PHP_EOL);
146
-
147
- $ this ->output ->success ('Successfully generated and committed message. ' );
148
150
}
149
151
150
152
public function schedule (Schedule $ schedule ): void
@@ -384,6 +386,8 @@ private function hydrateMessage(Collection $message): string
384
386
* @param null|mixed $input
385
387
*
386
388
* @noinspection CallableParameterUseCaseInTypeContextInspection
389
+ * @noinspection PhpSameParameterValueInspection
390
+ * @noinspection MissingParameterTypeDeclarationInspection
387
391
*/
388
392
private function createProcess (
389
393
array $ command ,
@@ -392,12 +396,10 @@ private function createProcess(
392
396
$ input = null ,
393
397
?float $ timeout = 60
394
398
): Process {
395
- if (null === $ cwd ) {
396
- $ cwd = $ this ->argument ('path ' );
397
- }
399
+ null === $ cwd and $ cwd = $ this ->argument ('path ' );
398
400
399
401
return tap (new Process ($ command , $ cwd , $ env , $ input , $ timeout ), function (Process $ process ): void {
400
- if ($ this ->option ( ' verbose ' )) {
402
+ if ($ this ->output -> isDebug ( )) {
401
403
$ this ->output ->note ($ process ->getCommandLine ());
402
404
}
403
405
});
0 commit comments