Skip to content

Added actions:stub console command #123

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
Feb 6, 2023
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
4 changes: 2 additions & 2 deletions docs/getting-started/installation/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Installation

To get the latest version of Laravel Actions, simply require the project using [Composer](https://getcomposer.org):
To get the latest version of `Laravel Actions`, simply require the project using [Composer](https://getcomposer.org):

```bash
composer require dragon-code/laravel-actions
Expand All @@ -11,7 +11,7 @@ Or manually update `require` block of `composer.json` and run `composer update`
```json
{
"require": {
"dragon-code/laravel-actions": "^3.0"
"dragon-code/laravel-actions": "^4.0"
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion docs/helpers/execution-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ You can also override the `success` and `failed` methods, which are called on su
## If Success

```php
use DragonCode\LaravelActions\Action;use Illuminate\Support\Facade\Log;
use DragonCode\LaravelActions\Action;
use Illuminate\Support\Facade\Log;

return new class () extends Action
{
Expand Down
9 changes: 9 additions & 0 deletions docs/how-to-use/customize-stub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Customize Stub

In order to use your action template, you first need to publish it using the console command:

```
php artisan actions:stub
```

As a result, the file `stubs/action.stub` will be created in the root of the project, which you can change for yourself.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

* Prologue
* [Upgrade Guide](prologue/upgrade-guide/index.md)
* [To 4.x from 3.x](prologue/upgrade-guide/4.x.md)
* [To 3.x from 2.x](prologue/upgrade-guide/3.x.md)
* [To 4.x from 3.x](prologue/upgrade-guide/4.x.md)
* [To 3.x from 2.x](prologue/upgrade-guide/3.x.md)
* Getting Started
* [Installation](getting-started/installation/index.md)
* How to use
Expand Down
25 changes: 25 additions & 0 deletions src/Console/StubPublish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Console;

use DragonCode\LaravelActions\Constants\Names;
use DragonCode\LaravelActions\Constants\Options;
use DragonCode\LaravelActions\Processors\Processor;
use DragonCode\LaravelActions\Processors\StubPublish as StubProcessor;

class StubPublish extends Command
{
protected $name = Names::STUB_PUBLISH;

protected $description = 'Publish stub that are available for customization';

protected Processor|string $processor = StubProcessor::class;

protected bool $secure = false;

protected array $options = [
Options::FORCE,
];
}
6 changes: 4 additions & 2 deletions src/Constants/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class Names
{
public const ACTIONS = 'actions';

public const FRESH = 'actions:fresh';

public const INSTALL = 'actions:install';

public const MAKE = 'make:action';

public const ACTIONS = 'actions';

public const REFRESH = 'actions:refresh';

public const RESET = 'actions:reset';
Expand All @@ -20,5 +20,7 @@ class Names

public const STATUS = 'actions:status';

public const STUB_PUBLISH = 'actions:stub';

public const UPGRADE = 'actions:upgrade';
}
13 changes: 11 additions & 2 deletions src/Processors/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Make extends Processor
{
protected string $fallback = 'auto';

protected string $stub = __DIR__ . '/../../resources/stubs/action.stub';
protected string $defaultStub = __DIR__ . '/../../resources/stubs/action.stub';

public function handle(): void
{
Expand All @@ -29,7 +29,7 @@ protected function run(): void

protected function create(string $path): void
{
File::copy($this->stub, $path);
File::copy($this->stubPath(), $path);
}

protected function getName(): string
Expand Down Expand Up @@ -61,4 +61,13 @@ protected function getTime(): string
{
return date('Y_m_d_His_');
}

protected function stubPath(): string
{
if ($path = realpath(base_path('stubs/action.stub'))) {
return $path;
}

return $this->defaultStub;
}
}
39 changes: 39 additions & 0 deletions src/Processors/StubPublish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Processors;

use DragonCode\Support\Facades\Filesystem\File;

class StubPublish extends Processor
{
protected string $stub = __DIR__ . '/../../resources/stubs/action.stub';

public function handle(): void
{
$this->allow()
? $this->notification->task('Publishing', fn () => $this->publish())
: $this->notification->info('Nothing to publish');
}

protected function publish(): void
{
File::copy($this->stub, $this->path());
}

protected function allow(): bool
{
return $this->options->force || $this->doesntExist();
}

protected function doesntExist(): bool
{
return ! File::exists($this->path());
}

protected function path(): string
{
return base_path('stubs/action.stub');
}
}
3 changes: 2 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public function register(): void
protected function registerCommands(): void
{
$this->commands([
Console\Actions::class,
Console\Fresh::class,
Console\Install::class,
Console\Make::class,
Console\Actions::class,
Console\Refresh::class,
Console\Reset::class,
Console\Rollback::class,
Console\Status::class,
Console\StubPublish::class,
Console\Upgrade::class,
]);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Commands/MakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Commands;

use DragonCode\LaravelActions\Constants\Names;
use DragonCode\Support\Facades\Filesystem\File;
use Tests\TestCase;

class MakeTest extends TestCase
Expand Down Expand Up @@ -107,4 +108,34 @@ public function testNestedLeftSlashWithExtension()
file_get_contents($path)
);
}

public function testFromCustomizedStub()
{
$name = 'MakeExample';

$stubPath = base_path('stubs/action.stub');

$actionPath = $this->getActionsPath() . '/' . date('Y_m_d_His') . '_make_example.php';

$this->assertFileDoesNotExist($stubPath);

File::copy(__DIR__ . '/../fixtures/app/stubs/customized.stub', $stubPath);

$this->assertFileExists($stubPath);
$this->assertFileDoesNotExist($actionPath);

$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);

$this->assertFileExists($actionPath);

$content = file_get_contents($actionPath);

$this->assertStringContainsString('Foo\\Bar\\Some', $content);
$this->assertStringContainsString('extends Some', $content);

$this->assertStringNotContainsString('DragonCode\\LaravelActions\\Action', $content);
$this->assertStringNotContainsString('extends Action', $content);
$this->assertStringNotContainsString('Run the actions.', $content);
$this->assertStringNotContainsString('@return void', $content);
}
}
88 changes: 88 additions & 0 deletions tests/Commands/StubPublishTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use DragonCode\LaravelActions\Constants\Names;
use DragonCode\Support\Facades\Filesystem\File;
use Tests\TestCase;

class StubPublishTest extends TestCase
{
public function testFirst(): void
{
$this->assertFileDoesNotExist($this->path());

$this->artisan(Names::STUB_PUBLISH)->assertExitCode(0);

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

public function testSkip(): void
{
File::copy(__DIR__ . '/../fixtures/app/stubs/customized.stub', $this->path());

$this->assertFileIsReadable($this->path());
$this->assertFileExists($this->path());

$content = file_get_contents($this->path());

$this->assertStringContainsString('Foo\\Bar\\Some', $content);
$this->assertStringContainsString('extends Some', $content);

$this->assertStringNotContainsString('DragonCode\\LaravelActions\\Action', $content);
$this->assertStringNotContainsString('extends Action', $content);
$this->assertStringNotContainsString('Run the actions.', $content);
$this->assertStringNotContainsString('@return void', $content);

$this->artisan(Names::STUB_PUBLISH)->assertExitCode(0);

$content = file_get_contents($this->path());

$this->assertStringContainsString('Foo\\Bar\\Some', $content);
$this->assertStringContainsString('extends Some', $content);

$this->assertStringNotContainsString('DragonCode\\LaravelActions\\Action', $content);
$this->assertStringNotContainsString('extends Action', $content);
$this->assertStringNotContainsString('Run the actions.', $content);
$this->assertStringNotContainsString('@return void', $content);
}

public function testForce(): void
{
File::copy(__DIR__ . '/../fixtures/app/stubs/customized.stub', $this->path());

$this->assertFileIsReadable($this->path());
$this->assertFileExists($this->path());

$content = file_get_contents($this->path());

$this->assertStringContainsString('Foo\\Bar\\Some', $content);
$this->assertStringContainsString('extends Some', $content);

$this->assertStringNotContainsString('DragonCode\\LaravelActions\\Action', $content);
$this->assertStringNotContainsString('extends Action', $content);
$this->assertStringNotContainsString('Run the actions.', $content);
$this->assertStringNotContainsString('@return void', $content);

$this->artisan(Names::STUB_PUBLISH, [
'--force' => true,
])->assertExitCode(0);

$content = file_get_contents($this->path());

$this->assertStringNotContainsString('Foo\\Bar\\Some', $content);
$this->assertStringNotContainsString('extends Some', $content);

$this->assertStringContainsString('DragonCode\\LaravelActions\\Action', $content);
$this->assertStringContainsString('extends Action', $content);
$this->assertStringContainsString('Run the actions.', $content);
$this->assertStringContainsString('@return void', $content);
}

protected function path(): string
{
return base_path('stubs/action.stub');
}
}
13 changes: 10 additions & 3 deletions tests/Concerns/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace Tests\Concerns;

use DragonCode\Support\Facades\Filesystem\Directory;
use Illuminate\Support\Facades\File;

/** @mixin \Tests\TestCase */
trait Files
{
protected function freshFiles(): void
{
File::deleteDirectory(
$this->targetDirectory()
);
Directory::ensureDelete([
$this->targetDirectory(),
$this->stubsDirectory(),
]);
}

protected function copyFiles(): void
Expand Down Expand Up @@ -79,6 +81,11 @@ protected function targetDirectory(?string $path = null): string
return rtrim($dir, '/\\') . '/' . ltrim($path, '/\\');
}

protected function stubsDirectory(): string
{
return base_path('stubs');
}

protected function getActionsPath(): string
{
return $this->app['config']->get('actions.path');
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/app/stubs/customized.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Foo\Bar\Some;

return new class () extends Some
{
public function __invoke(): void
{
// some code
}
};