Skip to content

Commit f57bc78

Browse files
authored
Merge pull request #16 from binafy/add-generateForce-method
[1.x] Add `generateForce` method
2 parents 230137c + a430daa commit f57bc78

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [`conditions`](#conditions)
2525
- [`download`](#download)
2626
- [`generate`](#generate)
27+
- [`generateForce`](#generate-force)
2728
- [Contributors](#contributors)
2829
- [Security](#security)
2930
- [Changelog](#changelog)
@@ -275,6 +276,23 @@ class Milwad
275276
}
276277
```
277278

279+
<a name="generate-force"></a>
280+
### `generateForce`
281+
282+
If you want to generate a stub file and overwrite it if it exists, you can use the `generateForce` method:
283+
284+
```php
285+
LaravelStub::from(__DIR__ . 'model.stub')
286+
->to(__DIR__ . '/App')
287+
->name('new-model')
288+
->ext('php')
289+
->replaces([
290+
'NAMESPACE' => 'App',
291+
'CLASS' => 'Milwad'
292+
])
293+
->generateForce();
294+
```
295+
278296
<a name="contributors"></a>
279297
## Contributors
280298

src/LaravelStub.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,18 @@ public function download()
153153
return Response::download($this->getPath());
154154
}
155155

156+
/**
157+
* Set stub file move without any copy.
158+
*/
159+
public function generateForce(): bool
160+
{
161+
return $this->generate(true);
162+
}
163+
156164
/**
157165
* Generate stub file.
158166
*/
159-
public function generate(): bool
167+
public function generate(bool $force = false): bool
160168
{
161169
// Check path is valid
162170
if (! File::exists($this->from)) {
@@ -168,6 +176,11 @@ public function generate(): bool
168176
throw new RuntimeException('The given folder path is not valid.');
169177
}
170178

179+
// Check if files exists and it not force throw exception
180+
if (! File::exists($this->to) && !$force) {
181+
throw new RuntimeException('The destination file does not exist, please enter a valid path.');
182+
}
183+
171184
// Get file content
172185
$content = File::get($this->from);
173186

tests/Feature/LaravelStubTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,47 @@
147147
assertFileExists(__DIR__ . '/../App/conditional-test-false.php');
148148

149149
$content = File::get(__DIR__ . '/../App/conditional-test-false.php');
150-
expect($content)->not->toContain('public function handle(): void');
151-
expect($content)->not->toContain('public function users(): void');
152-
expect($content)->not->toContain('public function roles(): void');
150+
expect($content)
151+
->not->toContain('public function handle(): void')
152+
->and($content)
153+
->not->toContain('public function users(): void')
154+
->and($content)
155+
->not->toContain('public function roles(): void');
156+
});
157+
158+
test('generate stub successfully when force is true', function () {
159+
$stub = __DIR__ . '/test.stub';
160+
161+
LaravelStub::from($stub)
162+
->to(__DIR__ . '/../App')
163+
->replaces([
164+
'CLASS' => 'Milwad',
165+
'NAMESPACE' => 'App\Models'
166+
])
167+
->name('new-test')
168+
->ext('php')
169+
->moveStub()
170+
->generate();
171+
172+
$this->createStubFile();
173+
174+
$generate = LaravelStub::from($stub)
175+
->to(__DIR__ . '/../App')
176+
->replaces([
177+
'CLASS' => 'Binafy',
178+
'NAMESPACE' => 'App\Models'
179+
])
180+
->name('new-test')
181+
->ext('php')
182+
->moveStub()
183+
->generateForce();
184+
185+
expect(file_get_contents(__DIR__ . '/../App/new-test.php'))
186+
->toContain('Binafy')
187+
->and(file_get_contents(__DIR__ . '/../App/new-test.php'))
188+
->not->toContain('Milwad');
189+
190+
assertTrue($generate);
191+
assertFileExists(__DIR__ . '/../App/new-test.php');
192+
assertFileDoesNotExist(__DIR__ . '/../App/test.stub');
153193
});

tests/TestCase.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
abstract class TestCase extends BaseTestCase
1010
{
1111
/**
12-
* Set up.
12+
* Create stub file.
1313
*/
14-
protected function setUp(): void
14+
public function createStubFile(): void
1515
{
16-
parent::setUp();
17-
1816
File::put(__DIR__ . '/Feature/test.stub', <<<EOL
1917
<?php
2018
@@ -49,6 +47,16 @@ public function roles(): void
4947
);
5048
}
5149

50+
/**
51+
* Set up.
52+
*/
53+
protected function setUp(): void
54+
{
55+
parent::setUp();
56+
57+
$this->createStubFile();
58+
}
59+
5260
/**
5361
* Get package providers.
5462
*/

0 commit comments

Comments
 (0)