File tree Expand file tree Collapse file tree 6 files changed +122
-3
lines changed Expand file tree Collapse file tree 6 files changed +122
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace DragonCode \LaravelActions \Concerns ;
4
4
5
+ use DragonCode \LaravelActions \Facades \Git ;
6
+
5
7
/** @mixin \Illuminate\Console\Command */
6
8
trait Argumentable
7
9
{
8
10
protected function argumentName (): string
9
11
{
10
- $ value = (string ) $ this ->argument ('name ' );
12
+ if ($ name = (string ) $ this ->argument ('name ' )) {
13
+ return trim ($ name );
14
+ }
15
+
16
+ return $ this ->getNamePrefix () . '_ ' . time ();
17
+ }
11
18
12
- return trim ($ value );
19
+ protected function getNamePrefix (): string
20
+ {
21
+ return $ this ->getGitBranchName () ?: 'auto ' ;
22
+ }
23
+
24
+ protected function getGitBranchName (): ?string
25
+ {
26
+ return Git::currentBranch (base_path ('.git ' ));
13
27
}
14
28
}
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class Make extends BaseCommand
23
23
* @var string
24
24
*/
25
25
protected $ signature = Names::MAKE
26
- . ' {name : The name of the action} ' ;
26
+ . ' {name? : The name of the action} ' ;
27
27
28
28
/**
29
29
* The console command description.
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace DragonCode \LaravelActions \Facades ;
6
+
7
+ use DragonCode \LaravelActions \Support \Git as Support ;
8
+ use Illuminate \Support \Facades \Facade ;
9
+
10
+ /**
11
+ * @method static string|null currentBranch(?string $path)
12
+ */
13
+ class Git extends Facade
14
+ {
15
+ protected static function getFacadeAccessor (): string
16
+ {
17
+ return Support::class;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace DragonCode \LaravelActions \Support ;
6
+
7
+ use Illuminate \Support \Str ;
8
+
9
+ class Git
10
+ {
11
+ public function currentBranch (?string $ path ): ?string
12
+ {
13
+ if ($ path = $ this ->getGitPath ($ path )) {
14
+ return $ this ->exec ($ path , 'rev-parse --abbrev-ref HEAD ' );
15
+ }
16
+
17
+ return null ;
18
+ }
19
+
20
+ protected function exec (string $ path , string $ command ): ?string
21
+ {
22
+ return exec (sprintf ('git --git-dir %s %s ' , $ path , $ command ));
23
+ }
24
+
25
+ protected function resolvePath (string $ path ): ?string
26
+ {
27
+ return realpath ($ path ) ?: null ;
28
+ }
29
+
30
+ protected function getGitPath (?string $ path ): ?string
31
+ {
32
+ if ($ path = $ this ->resolvePath ($ path )) {
33
+ if ($ this ->isGitDir ($ path )) {
34
+ return $ path ;
35
+ }
36
+ }
37
+
38
+ return null ;
39
+ }
40
+
41
+ protected function isGitDir (?string $ path ): bool
42
+ {
43
+ if ($ path = rtrim ($ path , '/ \\' )) {
44
+ return Str::endsWith ($ path , '.git ' );
45
+ }
46
+
47
+ return false ;
48
+ }
49
+ }
Original file line number Diff line number Diff line change @@ -26,4 +26,17 @@ public function testMakingFiles()
26
26
27
27
$ this ->assertEquals (file_get_contents ($ expected ), file_get_contents ($ path ));
28
28
}
29
+
30
+ public function testAutoName ()
31
+ {
32
+ $ filename = date ('Y_m_d_His ' ) . '_auto_ ' . time () . '.php ' ;
33
+
34
+ $ path = database_path ('actions/ ' . $ filename );
35
+
36
+ $ this ->assertFileDoesNotExist ($ path );
37
+
38
+ $ this ->artisan ('make:migration:action ' )->run ();
39
+
40
+ $ this ->assertFileExists ($ path );
41
+ }
29
42
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Tests \Services ;
6
+
7
+ use DragonCode \LaravelActions \Facades \Git ;
8
+ use Illuminate \Support \Str ;
9
+ use Tests \TestCase ;
10
+
11
+ class GitTest extends TestCase
12
+ {
13
+ public function testCurrentBranchNull ()
14
+ {
15
+ $ this ->assertNull (Git::currentBranch (__DIR__ ));
16
+ }
17
+
18
+ public function testCurrentBranch ()
19
+ {
20
+ $ branch = Git::currentBranch (__DIR__ . '/../../.git ' );
21
+
22
+ $ this ->assertTrue (Str::contains ($ branch , ['main ' , '2.x ' ]));
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments