Skip to content

Commit 06beaf8

Browse files
Added file name question
1 parent f53e976 commit 06beaf8

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"illuminate/container": "^10.0 || ^11.0",
5151
"illuminate/database": "^10.0 || ^11.0",
5252
"illuminate/support": "^10.0 || ^11.0",
53+
"laravel/prompts": "^0.1.23",
5354
"symfony/console": "^6.0 || ^7.0"
5455
},
5556
"require-dev": {

docs/guide/creating.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ The new operation's file will be placed in your `operations` directory in the ba
1010

1111
Each operation file name contains a timestamp, which allows Laravel to determine the order of the operations.
1212

13+
## Asks For File Name
14+
15+
> The question will not be asked when calling a console command passing the `--quiet` parameter.
16+
17+
When calling the `operations` console command without passing a name in the `name` parameter,
18+
you will be asked for a name for the file.
19+
20+
```bash
21+
$ php artisan make:operation
22+
Creating an operation
23+
24+
┌ What should the operation be named? ─────────────────────────┐
25+
│ E.g. activate articles │
26+
└──────────────────────────────────────────────────────────────┘
27+
Press Enter to autodetect
28+
```
29+
30+
You can enter your own or simply press `Enter` to continue.
31+
In this case, automatic file name generation will be applied.
1332

1433
## Automatically Generate A File Name
1534

@@ -66,7 +85,8 @@ php artisan make:operation foo/bar/QweRty.php
6685

6786
## Invokable Method
6887

69-
By default, the new operation class will contain the `__invoke` method, but you can easily replace it with public `up` name.
88+
By default, the new operation class will contain the `__invoke` method, but you can easily replace it with public `up`
89+
name.
7090

7191
```php
7292
use DragonCode\LaravelDeployOperations\Operation;
@@ -83,7 +103,8 @@ return new class extends Operation
83103
> Note that the `__invoke` method has been added as a single call.
84104
> This means that when the operation is running, it will be called, but not when it is rolled back.
85105
>
86-
> You should also pay attention to the fact that if there is an `__invoke` method in the class, the `down` method will not be called.
106+
> You should also pay attention to the fact that if there is an `__invoke` method in the class, the `down` method will
107+
> not be called.
87108
88109
```php
89110
use DragonCode\LaravelDeployOperations\Operation;

src/Processors/Make.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use function base_path;
1212
use function date;
13+
use function Laravel\Prompts\text;
1314
use function realpath;
1415

1516
class Make extends Processor
@@ -53,12 +54,37 @@ protected function getFilename(string $branch): string
5354
$directory = Path::dirname($branch);
5455
$filename = Path::filename($branch);
5556

56-
return Str::of($filename)->prepend($this->getTime())->finish('.php')->prepend($directory . '/')->toString();
57+
return Str::of($filename)
58+
->snake()
59+
->prepend($this->getTime())
60+
->finish('.php')
61+
->prepend($directory . '/')
62+
->toString();
5763
}
5864

5965
protected function getBranchName(): string
6066
{
61-
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallback;
67+
if ($name = trim((string) $this->options->name)) {
68+
return $name;
69+
}
70+
71+
if ($name = $this->askForName()) {
72+
return $name;
73+
}
74+
75+
return $this->git->currentBranch() ?? $this->fallback;
76+
}
77+
78+
protected function askForName(): string
79+
{
80+
$prompt = $this->promptForName();
81+
82+
return text($prompt[0], $prompt[1], hint: $prompt[2]);
83+
}
84+
85+
protected function promptForName(): array
86+
{
87+
return ['What should the operation be named?', 'E.g. activate articles', 'Press Enter to autodetect'];
6288
}
6389

6490
protected function getTime(): string

tests/Commands/MakeTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,28 @@ public function testMakingFiles()
2626
);
2727
}
2828

29+
public function testAskedName()
30+
{
31+
$path = $this->getOperationsPath() . '/' . date('Y_m_d_His') . '_some_name.php';
32+
33+
$this->assertFileDoesNotExist($path);
34+
35+
$this->artisan(Names::Make)
36+
->expectsQuestion('What should the operation be named?', 'Some Name')
37+
->assertExitCode(0);
38+
39+
$this->assertFileExists($path);
40+
}
41+
2942
public function testAutoName()
3043
{
3144
$path = $this->getOperationsPath() . '/' . date('Y_m_d_His') . '_auto.php';
3245

3346
$this->assertFileDoesNotExist($path);
3447

35-
$this->artisan(Names::Make)->assertExitCode(0);
48+
$this->artisan(Names::Make)
49+
->expectsQuestion('What should the operation be named?', '')
50+
->assertExitCode(0);
3651

3752
$this->assertFileExists($path);
3853
}

0 commit comments

Comments
 (0)