Skip to content

Commit

Permalink
Merge pull request #16 from binafy/add-generateForce-method
Browse files Browse the repository at this point in the history
[1.x] Add `generateForce` method
  • Loading branch information
milwad-dev authored Nov 22, 2024
2 parents 230137c + a430daa commit f57bc78
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [`conditions`](#conditions)
- [`download`](#download)
- [`generate`](#generate)
- [`generateForce`](#generate-force)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
Expand Down Expand Up @@ -275,6 +276,23 @@ class Milwad
}
```

<a name="generate-force"></a>
### `generateForce`

If you want to generate a stub file and overwrite it if it exists, you can use the `generateForce` method:

```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateForce();
```

<a name="contributors"></a>
## Contributors

Expand Down
15 changes: 14 additions & 1 deletion src/LaravelStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,18 @@ public function download()
return Response::download($this->getPath());
}

/**
* Set stub file move without any copy.
*/
public function generateForce(): bool
{
return $this->generate(true);
}

/**
* Generate stub file.
*/
public function generate(): bool
public function generate(bool $force = false): bool
{
// Check path is valid
if (! File::exists($this->from)) {
Expand All @@ -168,6 +176,11 @@ public function generate(): bool
throw new RuntimeException('The given folder path is not valid.');
}

// Check if files exists and it not force throw exception
if (! File::exists($this->to) && !$force) {
throw new RuntimeException('The destination file does not exist, please enter a valid path.');
}

// Get file content
$content = File::get($this->from);

Expand Down
46 changes: 43 additions & 3 deletions tests/Feature/LaravelStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,47 @@
assertFileExists(__DIR__ . '/../App/conditional-test-false.php');

$content = File::get(__DIR__ . '/../App/conditional-test-false.php');
expect($content)->not->toContain('public function handle(): void');
expect($content)->not->toContain('public function users(): void');
expect($content)->not->toContain('public function roles(): void');
expect($content)
->not->toContain('public function handle(): void')
->and($content)
->not->toContain('public function users(): void')
->and($content)
->not->toContain('public function roles(): void');
});

test('generate stub successfully when force is true', function () {
$stub = __DIR__ . '/test.stub';

LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Milwad',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->moveStub()
->generate();

$this->createStubFile();

$generate = LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Binafy',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->moveStub()
->generateForce();

expect(file_get_contents(__DIR__ . '/../App/new-test.php'))
->toContain('Binafy')
->and(file_get_contents(__DIR__ . '/../App/new-test.php'))
->not->toContain('Milwad');

assertTrue($generate);
assertFileExists(__DIR__ . '/../App/new-test.php');
assertFileDoesNotExist(__DIR__ . '/../App/test.stub');
});
16 changes: 12 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
abstract class TestCase extends BaseTestCase
{
/**
* Set up.
* Create stub file.
*/
protected function setUp(): void
public function createStubFile(): void
{
parent::setUp();

File::put(__DIR__ . '/Feature/test.stub', <<<EOL
<?php
Expand Down Expand Up @@ -49,6 +47,16 @@ public function roles(): void
);
}

/**
* Set up.
*/
protected function setUp(): void
{
parent::setUp();

$this->createStubFile();
}

/**
* Get package providers.
*/
Expand Down

0 comments on commit f57bc78

Please sign in to comment.