Skip to content

Added file name question #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"illuminate/container": "^10.0 || ^11.0",
"illuminate/database": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"laravel/prompts": "^0.1.23",
"symfony/console": "^6.0 || ^7.0"
},
"require-dev": {
Expand Down
25 changes: 23 additions & 2 deletions docs/guide/creating.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ The new operation's file will be placed in your `operations` directory in the ba

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

## Asks For File Name

> The question will not be asked when calling a console command passing the `--quiet` parameter.

When calling the `operations` console command without passing a name in the `name` parameter,
you will be asked for a name for the file.

```bash
$ php artisan make:operation
Creating an operation

┌ What should the operation be named? ─────────────────────────┐
│ E.g. activate articles │
└──────────────────────────────────────────────────────────────┘
Press Enter to autodetect
```

You can enter your own or simply press `Enter` to continue.
In this case, automatic file name generation will be applied.

## Automatically Generate A File Name

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

## Invokable Method

By default, the new operation class will contain the `__invoke` method, but you can easily replace it with public `up` name.
By default, the new operation class will contain the `__invoke` method, but you can easily replace it with public `up`
name.

```php
use DragonCode\LaravelDeployOperations\Operation;
Expand All @@ -83,7 +103,8 @@ return new class extends Operation
> Note that the `__invoke` method has been added as a single call.
> This means that when the operation is running, it will be called, but not when it is rolled back.
>
> 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.
> 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.

```php
use DragonCode\LaravelDeployOperations\Operation;
Expand Down
30 changes: 28 additions & 2 deletions src/Processors/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function base_path;
use function date;
use function Laravel\Prompts\text;
use function realpath;

class Make extends Processor
Expand Down Expand Up @@ -53,12 +54,37 @@ protected function getFilename(string $branch): string
$directory = Path::dirname($branch);
$filename = Path::filename($branch);

return Str::of($filename)->prepend($this->getTime())->finish('.php')->prepend($directory . '/')->toString();
return Str::of($filename)
->snake()
->prepend($this->getTime())
->finish('.php')
->prepend($directory . '/')
->toString();
}

protected function getBranchName(): string
{
return $this->options->name ?? $this->git->currentBranch() ?? $this->fallback;
if ($name = trim((string) $this->options->name)) {
return $name;
}

if ($name = $this->askForName()) {
return $name;
}

return $this->git->currentBranch() ?? $this->fallback;
}

protected function askForName(): string
{
$prompt = $this->promptForName();

return text($prompt[0], $prompt[1], hint: $prompt[2]);
}

protected function promptForName(): array
{
return ['What should the operation be named?', 'E.g. activate articles', 'Press Enter to autodetect'];
}

protected function getTime(): string
Expand Down
17 changes: 16 additions & 1 deletion tests/Commands/MakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,28 @@ public function testMakingFiles()
);
}

public function testAskedName()
{
$path = $this->getOperationsPath() . '/' . date('Y_m_d_His') . '_some_name.php';

$this->assertFileDoesNotExist($path);

$this->artisan(Names::Make)
->expectsQuestion('What should the operation be named?', 'Some Name')
->assertExitCode(0);

$this->assertFileExists($path);
}

public function testAutoName()
{
$path = $this->getOperationsPath() . '/' . date('Y_m_d_His') . '_auto.php';

$this->assertFileDoesNotExist($path);

$this->artisan(Names::Make)->assertExitCode(0);
$this->artisan(Names::Make)
->expectsQuestion('What should the operation be named?', '')
->assertExitCode(0);

$this->assertFileExists($path);
}
Expand Down