File tree 4 files changed +87
-8
lines changed 4 files changed +87
-8
lines changed Original file line number Diff line number Diff line change 24
24
- [ ` conditions ` ] ( #conditions )
25
25
- [ ` download ` ] ( #download )
26
26
- [ ` generate ` ] ( #generate )
27
+ - [ ` generateForce ` ] ( #generate-force )
27
28
- [ Contributors] ( #contributors )
28
29
- [ Security] ( #security )
29
30
- [ Changelog] ( #changelog )
@@ -275,6 +276,23 @@ class Milwad
275
276
}
276
277
```
277
278
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
+
278
296
<a name =" contributors " ></a >
279
297
## Contributors
280
298
Original file line number Diff line number Diff line change @@ -153,10 +153,18 @@ public function download()
153
153
return Response::download ($ this ->getPath ());
154
154
}
155
155
156
+ /**
157
+ * Set stub file move without any copy.
158
+ */
159
+ public function generateForce (): bool
160
+ {
161
+ return $ this ->generate (true );
162
+ }
163
+
156
164
/**
157
165
* Generate stub file.
158
166
*/
159
- public function generate (): bool
167
+ public function generate (bool $ force = false ): bool
160
168
{
161
169
// Check path is valid
162
170
if (! File::exists ($ this ->from )) {
@@ -168,6 +176,11 @@ public function generate(): bool
168
176
throw new RuntimeException ('The given folder path is not valid. ' );
169
177
}
170
178
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
+
171
184
// Get file content
172
185
$ content = File::get ($ this ->from );
173
186
Original file line number Diff line number Diff line change 147
147
assertFileExists (__DIR__ . '/../App/conditional-test-false.php ' );
148
148
149
149
$ 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 ' );
153
193
});
Original file line number Diff line number Diff line change 9
9
abstract class TestCase extends BaseTestCase
10
10
{
11
11
/**
12
- * Set up .
12
+ * Create stub file .
13
13
*/
14
- protected function setUp (): void
14
+ public function createStubFile (): void
15
15
{
16
- parent ::setUp ();
17
-
18
16
File::put (__DIR__ . '/Feature/test.stub ' , <<<EOL
19
17
<?php
20
18
@@ -49,6 +47,16 @@ public function roles(): void
49
47
);
50
48
}
51
49
50
+ /**
51
+ * Set up.
52
+ */
53
+ protected function setUp (): void
54
+ {
55
+ parent ::setUp ();
56
+
57
+ $ this ->createStubFile ();
58
+ }
59
+
52
60
/**
53
61
* Get package providers.
54
62
*/
You can’t perform that action at this time.
0 commit comments