9
9
use Toolkit \Stdlib \Str ;
10
10
use function array_merge ;
11
11
use function is_int ;
12
- use function printf ;
13
12
use function println ;
13
+ use function str_replace ;
14
14
use function trim ;
15
15
16
16
/**
@@ -58,6 +58,13 @@ class FileTreeBuilder extends AbstractObj
58
58
*/
59
59
public string $ tplDir = '' ;
60
60
61
+ /**
62
+ * Callable on after file copied.
63
+ *
64
+ * @var callable(string $newFile): void
65
+ */
66
+ public $ afterCopy ;
67
+
61
68
/**
62
69
* @param string $dir
63
70
*
@@ -85,15 +92,41 @@ public function copy(string $srcFile, string $dstFile, ?callable $afterFn = null
85
92
if ($ afterFn !== null ) {
86
93
$ afterFn ($ dstFile );
87
94
}
95
+
96
+ if ($ fn = $ this ->afterCopy ) {
97
+ $ fn ($ dstFile );
98
+ }
99
+
88
100
return $ this ;
89
101
}
90
102
91
103
/**
104
+ * Copy all files in dir to dst dir
105
+ *
106
+ * ### Exclude files:
107
+ *
108
+ * ```php
109
+ * $ftb->copyDir('path/to/template dir', './', [
110
+ * 'exclude' => ['*.tpl'],
111
+ * ])
112
+ * ```
113
+ *
114
+ * ### Adv Usage:
115
+ *
116
+ * ```php
117
+ * $ftb->copyDir('path/to/template dir', './', [
118
+ * 'afterFn' => function (string $newFile) use ($ftb) {
119
+ * // render vars in the match file
120
+ * $ftb->renderOnMatch($newFile, ['*.java']);
121
+ * },
122
+ * ])
123
+ * ```
124
+ *
92
125
* @param string $srcDir source dir path.
93
126
* @param string $dstDir dst dir path, default relative the workDir.
94
127
* @param array $options = [
95
- * 'include' => [], //
96
- * 'exclude' => [], //
128
+ * 'include' => [], // limit copy files
129
+ * 'exclude' => [], // can exclude files on copy
97
130
* 'afterFn' => function(string $newFile) {},
98
131
* ]
99
132
*
@@ -123,7 +156,15 @@ public function copyDir(string $srcDir, string $dstDir, array $options = []): se
123
156
124
157
return !File::isExclude ($ oldFile , $ options ['exclude ' ]);
125
158
},
126
- 'afterFn ' => $ options ['afterFn ' ],
159
+ 'afterFn ' => function (string $ newFile ) use ($ options ) {
160
+ if ($ fn = $ options ['afterFn ' ]) {
161
+ $ fn ($ newFile );
162
+ }
163
+
164
+ if ($ fn = $ this ->afterCopy ) {
165
+ $ fn ($ newFile );
166
+ }
167
+ },
127
168
]);
128
169
129
170
return $ this ;
@@ -150,6 +191,8 @@ public function file(string $name, string $contents = ''): self
150
191
}
151
192
152
193
/**
194
+ * Create multi files at once.
195
+ *
153
196
* @param array $files file paths, default relative the workDir.
154
197
* @param string $contents
155
198
*
@@ -261,39 +304,84 @@ public function dirFiles(string $name, string ...$files): self
261
304
*
262
305
* @return $this
263
306
*/
264
- public function globRender (string $ pattern , array $ tplVars = []): self
307
+ public function renderByGlob (string $ pattern , array $ tplVars = []): self
265
308
{
266
309
foreach (glob ($ pattern ) as $ tplFile ) {
267
310
$ this ->tplFile ($ tplFile , '' , $ tplVars );
268
311
}
269
312
return $ this ;
270
313
}
271
314
315
+ /**
316
+ * Render give file on match patterns.
317
+ *
318
+ * @param string $tplFile
319
+ * @param array $patterns
320
+ * @param array $tplVars
321
+ *
322
+ * @return $this
323
+ */
324
+ public function renderOnMatch (string $ tplFile , array $ patterns , array $ tplVars = []): self
325
+ {
326
+ if (File::isInclude ($ tplFile , $ patterns )) {
327
+ $ this ->tplFile ($ tplFile , '' , $ tplVars );
328
+ }
329
+ return $ this ;
330
+ }
331
+
332
+ /**
333
+ * Render template vars in the give file, will update file contents to rendered.
334
+ *
335
+ * @param string $tplFile
336
+ * @param array $tplVars
337
+ *
338
+ * @return $this
339
+ */
340
+ public function renderFile (string $ tplFile , array $ tplVars = []): self
341
+ {
342
+ return $ this ->tplFile ($ tplFile , '' , $ tplVars );
343
+ }
344
+
272
345
/**
273
346
* Create file from a template file
274
347
*
275
348
* @param string $tplFile tpl file path, relative the tplDir.
276
- * @param string $dstFile Dst file path, relative the workdir. If empty, use $tplFile
349
+ * @param string $dstFile Dst file path, relative the workdir. If empty, use $tplFile for update.
277
350
* @param array $tplVars
278
351
*
279
352
* @return $this
280
353
*/
281
354
public function tplFile (string $ tplFile , string $ dstFile = '' , array $ tplVars = []): self
282
355
{
283
356
Assert::notBlank ($ tplFile );
284
-
285
357
$ dstFile = $ this ->getRealpath ($ dstFile ?: $ tplFile );
358
+
286
359
if (!File::isAbsPath ($ tplFile )) {
287
- $ tplFile = $ this ->tplDir . '/ ' . $ tplFile ;
360
+ $ tplFile = $ this ->tplDir . '/ ' . $ tplFile ;
288
361
}
289
362
290
363
$ this ->printMsgf ('render file: %s ' , $ tplFile );
291
364
if ($ this ->tplVars ) {
292
365
$ tplVars = array_merge ($ this ->tplVars , $ tplVars );
293
366
}
294
367
295
- $ content = Str::renderTemplate (File::readAll ($ tplFile ), $ tplVars );
368
+ return $ this ->doRender ($ tplFile , $ dstFile , $ tplVars );
369
+ }
370
+
371
+ /**
372
+ * Do render template file
373
+ *
374
+ * @param string $tplFile
375
+ * @param string $dstFile
376
+ * @param array $tplVars
377
+ *
378
+ * @return $this
379
+ */
380
+ protected function doRender (string $ tplFile , string $ dstFile , array $ tplVars = []): self
381
+ {
296
382
if (!$ this ->dryRun ) {
383
+ $ content = Str::renderTemplate (File::readAll ($ tplFile ), $ tplVars );
384
+
297
385
File::putContents ($ dstFile , $ content );
298
386
}
299
387
@@ -434,7 +522,7 @@ protected function printMsg(string $msg): void
434
522
$ msg = '[DRY-RUN] ' . $ msg ;
435
523
}
436
524
437
- println ($ msg );
525
+ println (str_replace ( $ this -> baseDir , ' {projectDir} ' , $ msg) );
438
526
}
439
527
}
440
528
@@ -451,7 +539,7 @@ protected function printMsgf(string $tpl, ...$vars): void
451
539
$ tpl = '[DRY-RUN] ' . $ tpl ;
452
540
}
453
541
454
- printf ( $ tpl . "\n" , ...$ vars );
542
+ println ( str_replace ( $ this -> baseDir , ' {projectDir} ' , sprintf ( $ tpl , ...$ vars)) );
455
543
}
456
544
}
457
545
@@ -466,4 +554,15 @@ public function setTplVars(array $tplVars): self
466
554
return $ this ;
467
555
}
468
556
557
+ /**
558
+ * @param callable(string $newFile): void $afterCopy
559
+ *
560
+ * @return FileTreeBuilder
561
+ */
562
+ public function setAfterCopy (callable $ afterCopy ): self
563
+ {
564
+ $ this ->afterCopy = $ afterCopy ;
565
+ return $ this ;
566
+ }
567
+
469
568
}
0 commit comments