Skip to content

Commit 391b6f8

Browse files
author
Andrey Helldar
authored
Merge pull request #40 from TheDragonCode/2.x
Add the ability to automatically generate a file name when creating
2 parents 2639764 + 035d592 commit 391b6f8

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed

src/Concerns/Argumentable.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
namespace DragonCode\LaravelActions\Concerns;
44

5+
use DragonCode\LaravelActions\Facades\Git;
6+
57
/** @mixin \Illuminate\Console\Command */
68
trait Argumentable
79
{
810
protected function argumentName(): string
911
{
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+
}
1118

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'));
1327
}
1428
}

src/Console/Make.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Make extends BaseCommand
2323
* @var string
2424
*/
2525
protected $signature = Names::MAKE
26-
. ' {name : The name of the action}';
26+
. ' {name? : The name of the action}';
2727

2828
/**
2929
* The console command description.

src/Facades/Git.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

src/Support/Git.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

tests/Commands/MakeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@ public function testMakingFiles()
2626

2727
$this->assertEquals(file_get_contents($expected), file_get_contents($path));
2828
}
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+
}
2942
}

tests/Services/GitTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)