9
9
"os"
10
10
"path/filepath"
11
11
"strings"
12
+ "text/template"
12
13
"time"
13
14
14
15
"github.com/zhufuyi/sponge/pkg/gofile"
@@ -28,6 +29,7 @@ type Replacer interface {
28
29
SaveFiles () error
29
30
ReadFile (filename string ) ([]byte , error )
30
31
GetFiles () []string
32
+ SaveTemplateFiles (m map [string ]interface {}, parentDir ... string ) error
31
33
}
32
34
33
35
// replacerInfo replacer information
@@ -287,6 +289,86 @@ func (r *replacerInfo) SaveFiles() error {
287
289
return nil
288
290
}
289
291
292
+ // SaveTemplateFiles save file with setting
293
+ func (r * replacerInfo ) SaveTemplateFiles (m map [string ]interface {}, parentDir ... string ) error {
294
+ refDir := ""
295
+ if len (parentDir ) > 0 {
296
+ refDir = strings .Join (parentDir , gofile .GetPathDelimiter ())
297
+ }
298
+
299
+ writeData := make (map [string ][]byte , len (r .files ))
300
+ for _ , file := range r .files {
301
+ data , err := replaceTemplateData (file , m )
302
+ if err != nil {
303
+ return err
304
+ }
305
+ newFilePath := r .getNewFilePath2 (file , refDir )
306
+ newFilePath = trimExt (newFilePath )
307
+ if gofile .IsExists (newFilePath ) {
308
+ return fmt .Errorf ("file %s already exists, cancel code generation" , newFilePath )
309
+ }
310
+ newFilePath , err = replaceTemplateFilePath (newFilePath , m )
311
+ if err != nil {
312
+ return err
313
+ }
314
+ writeData [newFilePath ] = data
315
+ }
316
+
317
+ for file , data := range writeData {
318
+ err := saveToNewFile (file , data )
319
+ if err != nil {
320
+ return err
321
+ }
322
+ }
323
+
324
+ return nil
325
+ }
326
+
327
+ func replaceTemplateData (file string , m map [string ]interface {}) ([]byte , error ) {
328
+ data , err := os .ReadFile (file )
329
+ if err != nil {
330
+ return nil , fmt .Errorf ("read file failed, err=%s" , err )
331
+ }
332
+ if ! bytes .Contains (data , []byte ("{{" )) {
333
+ return data , nil
334
+ }
335
+
336
+ builder := bytes.Buffer {}
337
+ tmpl , err := template .New (file ).Parse (string (data ))
338
+ if err != nil {
339
+ return nil , fmt .Errorf ("parse data failed, err=%s" , err )
340
+ }
341
+ err = tmpl .Execute (& builder , m )
342
+ if err != nil {
343
+ return nil , fmt .Errorf ("execute data failed, err=%s" , err )
344
+ }
345
+ return builder .Bytes (), nil
346
+ }
347
+
348
+ func replaceTemplateFilePath (file string , m map [string ]interface {}) (string , error ) {
349
+ if ! strings .Contains (file , "{{" ) {
350
+ return file , nil
351
+ }
352
+
353
+ builder := strings.Builder {}
354
+ tmpl , err := template .New ("file: " + file ).Parse (file )
355
+ if err != nil {
356
+ return file , fmt .Errorf ("parse file failed, err=%s" , err )
357
+ }
358
+ err = tmpl .Execute (& builder , m )
359
+ if err != nil {
360
+ return file , fmt .Errorf ("execute file failed, err=%s" , err )
361
+ }
362
+ return builder .String (), nil
363
+ }
364
+
365
+ func trimExt (file string ) string {
366
+ file = strings .TrimSuffix (file , ".tmpl" )
367
+ file = strings .TrimSuffix (file , ".tpl" )
368
+ file = strings .TrimSuffix (file , ".template" )
369
+ return file
370
+ }
371
+
290
372
func (r * replacerInfo ) isIgnoreFile (file string ) bool {
291
373
isIgnore := false
292
374
for _ , v := range r .ignoreFiles {
@@ -334,6 +416,18 @@ func (r *replacerInfo) getNewFilePath(file string) string {
334
416
return newFilePath
335
417
}
336
418
419
+ func (r * replacerInfo ) getNewFilePath2 (file string , refDir string ) string {
420
+ if refDir == "" {
421
+ return r .getNewFilePath (file )
422
+ }
423
+
424
+ newFilePath := r .outPath + gofile .GetPathDelimiter () + refDir + gofile .GetPathDelimiter () + strings .Replace (file , r .path , "" , 1 )
425
+ if gofile .IsWindows () {
426
+ newFilePath = strings .ReplaceAll (newFilePath , "/" , "\\ " )
427
+ }
428
+ return newFilePath
429
+ }
430
+
337
431
// if windows, convert the path splitter
338
432
func (r * replacerInfo ) convertPathDelimiter (filePath string ) string {
339
433
if r .isActual && gofile .IsWindows () {
0 commit comments